|
2 | 2 |
|
3 | 3 | #include <sys/time.h> |
4 | 4 |
|
5 | | -// precise gettimeofday on Windows |
6 | 5 | #if defined(_WIN32) |
| 6 | +// precise gettimeofday on Windows |
7 | 7 |
|
8 | | - |
9 | | -// a) use GetSystemTimePreciseAsFileTime |
| 8 | +// a) use GetSystemTimePreciseAsFileTime (not available on Windows 7) |
10 | 9 | #include <windows.h> |
| 10 | +#include <stdint.h> |
| 11 | +#include <stdio.h> |
| 12 | + |
| 13 | +#define FILETIME2US(FT) ((((uint64_t)FT.dwHighDateTime) << 32 | ((uint64_t)FT.dwLowDateTime)) / 10 - (int64_t)11644473600000000) |
| 14 | +#define PERFCOUNT2US(PC, FREQ) (PC.QuadPart / 1000.0 / FREQ) |
| 15 | + |
11 | 16 | int compat_gettimeofday(struct timeval *tv, struct timezone* tz) |
12 | 17 | { |
13 | | - FILETIME ft; |
14 | | - unsigned __int64 tmpres = 0; |
| 18 | + uint64_t tmpres = 0; |
| 19 | + FILETIME ft; // 100-nanosecond intervals since January 1, 1601 |
| 20 | + |
| 21 | +#ifndef USE_GETSYSTEMTIMEPRECISEASFILETIME |
| 22 | + // Workaround for missing GetSystemTimePreciseAsFileTime |
| 23 | + // This should work on Windows Xp and above |
| 24 | + static uint64_t tmref = 0; // reference time (microseconds) |
| 25 | + static int64_t pcref = -3600000000L; // reference counter (microseconds) |
| 26 | + static double frequency = -1.0; // performance frequency (GHz) |
| 27 | + |
| 28 | + // Obtain performance frequency |
| 29 | + if (frequency == -1.0) |
| 30 | + { |
| 31 | + LARGE_INTEGER freq; |
| 32 | + if (!QueryPerformanceFrequency(&freq)) |
| 33 | + { |
| 34 | + // Cannot use QueryPerformanceCounter |
| 35 | + frequency = 0.0; |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + frequency = (double) freq.QuadPart / 1000000000.0; // GHz |
| 40 | + } |
| 41 | + } |
| 42 | +#endif |
15 | 43 |
|
16 | 44 | if (tv != NULL) |
17 | 45 | { |
18 | | -#if !defined(_UCRT) |
19 | | - GetSystemTimeAsFileTime(&ft); // low precision (16ms) |
| 46 | +#ifdef USE_GETSYSTEMTIMEPRECISEASFILETIME |
| 47 | + GetSystemTimePreciseAsFileTime(&ft); // high precision wall time (<1 ms) |
| 48 | + tmpres = FILETIME2US(ft); |
20 | 49 | #else |
21 | | - GetSystemTimePreciseAsFileTime(&ft); // high precision (1<ms) |
| 50 | + if (frequency == 0.0) |
| 51 | + { |
| 52 | + // Cannot use QueryPerformanceCounter, only low precision time is available |
| 53 | + GetSystemTimeAsFileTime(&ft); // low precision wall time |
| 54 | + tmpres = FILETIME2US(ft); // to us |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + // Combine low precision wall time with high precision counter |
| 59 | + int64_t pcpres; |
| 60 | + int64_t pcdelta; |
| 61 | + LARGE_INTEGER pc; |
| 62 | + QueryPerformanceCounter(&pc); // high precision counter |
| 63 | + pcpres = PERFCOUNT2US(pc, frequency); // to us |
| 64 | + pcdelta = pcpres - pcref; |
| 65 | + if (pcdelta >= 3600000000L) // sync low precision time at start and then once per hour |
| 66 | + { |
| 67 | + printf("time sync\n"); |
| 68 | + // obtain new reference time |
| 69 | + GetSystemTimeAsFileTime(&ft); // low precision wall time |
| 70 | + tmref = FILETIME2US(ft); // to us |
| 71 | + pcref = pcpres; |
| 72 | + pcdelta = 0; |
| 73 | + } |
| 74 | + // Current time as low precision reference time + high precision counter delta |
| 75 | + tmpres = tmref + pcdelta; |
| 76 | + } |
22 | 77 | #endif |
23 | | - tmpres |= ft.dwHighDateTime; |
24 | | - tmpres <<= 32; |
25 | | - tmpres |= ft.dwLowDateTime; |
26 | | - tmpres /= 10; // convert into microseconds |
27 | | - tmpres -= (__int64) 11644473600000000; |
28 | 78 | tv->tv_sec = (long) (tmpres / 1000000UL); |
29 | 79 | tv->tv_usec = (long) (tmpres % 1000000UL); |
30 | 80 | } |
31 | 81 | (void) tz; |
32 | 82 | return 0; |
33 | 83 | } |
34 | 84 |
|
35 | | - |
36 | | -// // b) use C++ chrono (portable, slower) (rename file to .cpp) |
| 85 | +// // b) use C++ chrono (portable, slower, MSYS2/CLANG64: fatal error: 'chrono' file not found) |
37 | 86 | // #include <chrono> |
38 | 87 | // int compat_gettimeofday(struct timeval* tp, struct timezone* tzp) |
39 | 88 | // { |
|
0 commit comments