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.
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)
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
);
37 #if defined(CLOCK_MONOTONIC_RAW) && !defined(TARGET_ANDROID)
38 clock_gettime(CLOCK_MONOTONIC_RAW
, &now
);
40 clock_gettime(CLOCK_MONOTONIC
, &now
);
41 #endif // CLOCK_MONOTONIC_RAW && !TARGET_ANDROID
42 return( ((int64_t)now
.tv_sec
* 1000000000L) + now
.tv_nsec
);
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
);
55 return( (int64_t)1000000000L );
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
77 unsigned int CTimeUtils::GetFrameTime()
82 CDateTime
CTimeUtils::GetLocalTime(time_t time
)
87 #ifdef HAVE_LOCALTIME_R
89 local
= localtime_r(&time
, &res
); // Conversion to local time
91 local
= localtime(&time
); // Conversion to local time
94 * Microsoft implementation of localtime returns NULL if on or before epoch.
95 * http://msdn.microsoft.com/en-us/library/bf12f0hc(VS.80).aspx
100 result
= time
; // Use the original time as close enough.
105 std::string
CTimeUtils::WithoutSeconds(const std::string
& hhmmss
)
107 return hhmmss
.substr(0, 5);