[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / utils / TimeUtils.cpp
blob95c5069c1160549c06bf12373d0d4b06b9b91e9c
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "TimeUtils.h"
10 #include "XBDateTime.h"
11 #include "windowing/GraphicContext.h"
13 #if defined(TARGET_DARWIN)
14 #include <mach/mach_time.h>
15 #include <CoreVideo/CVHostTime.h>
16 #elif defined(TARGET_WINDOWS)
17 #include <windows.h>
18 #else
19 #include <time.h>
20 #endif
22 namespace
24 auto startTime = std::chrono::steady_clock::now();
27 int64_t CurrentHostCounter(void)
29 #if defined(TARGET_DARWIN)
30 return( (int64_t)CVGetCurrentHostTime() );
31 #elif defined(TARGET_WINDOWS)
32 LARGE_INTEGER PerformanceCount;
33 QueryPerformanceCounter(&PerformanceCount);
34 return( (int64_t)PerformanceCount.QuadPart );
35 #else
36 struct timespec now;
37 #if defined(CLOCK_MONOTONIC_RAW) && !defined(TARGET_ANDROID)
38 clock_gettime(CLOCK_MONOTONIC_RAW, &now);
39 #else
40 clock_gettime(CLOCK_MONOTONIC, &now);
41 #endif // CLOCK_MONOTONIC_RAW && !TARGET_ANDROID
42 return( ((int64_t)now.tv_sec * 1000000000L) + now.tv_nsec );
43 #endif
46 int64_t CurrentHostFrequency(void)
48 #if defined(TARGET_DARWIN)
49 return( (int64_t)CVGetHostClockFrequency() );
50 #elif defined(TARGET_WINDOWS)
51 LARGE_INTEGER Frequency;
52 QueryPerformanceFrequency(&Frequency);
53 return( (int64_t)Frequency.QuadPart );
54 #else
55 return( (int64_t)1000000000L );
56 #endif
59 unsigned int CTimeUtils::frameTime = 0;
61 void CTimeUtils::UpdateFrameTime(bool flip)
63 auto now = std::chrono::steady_clock::now();
64 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime);
66 unsigned int currentTime = duration.count();
67 unsigned int last = frameTime;
68 while (frameTime < currentTime)
70 frameTime += (unsigned int)(1000 / CServiceBroker::GetWinSystem()->GetGfxContext().GetFPS());
71 // observe wrap around
72 if (frameTime < last)
73 break;
77 unsigned int CTimeUtils::GetFrameTime()
79 return frameTime;
82 CDateTime CTimeUtils::GetLocalTime(time_t time)
84 CDateTime result;
86 tm *local;
87 #ifdef HAVE_LOCALTIME_R
88 tm res = {};
89 local = localtime_r(&time, &res); // Conversion to local time
90 #else
91 local = localtime(&time); // Conversion to local time
92 #endif
94 * Microsoft implementation of localtime returns NULL if on or before epoch.
95 * http://msdn.microsoft.com/en-us/library/bf12f0hc(VS.80).aspx
97 if (local)
98 result = *local;
99 else
100 result = time; // Use the original time as close enough.
102 return result;
105 std::string CTimeUtils::WithoutSeconds(const std::string& hhmmss)
107 return hhmmss.substr(0, 5);