3 #if defined(_WIN32) && !defined(__CYGWIN__)
4 #include <windows.h> // For GetSystemTimeAsFileTime(), must be included first, sorry.
8 #include "base/cl_sysdep.h"
11 #include "cln/timing.h"
16 #include "timing/cl_t_config.h"
19 #if defined(HAVE_GETTIMEOFDAY)
21 #elif defined(_WIN32) && !defined(__CYGWIN__)
22 /* <windows.h> included above. */
31 const cl_timespec cl_current_time ()
33 #if defined(HAVE_GETTIMEOFDAY)
34 var struct timeval tv;
35 if (gettimeofday(&tv,NULL) != 0) {
36 perror("gettimeofday");
37 tv.tv_sec = 0; tv.tv_usec = 0;
39 return cl_timespec(tv.tv_sec,
40 tv.tv_usec * (1000000000/1000000)
42 #elif defined(_WIN32) && !defined(__CYGWIN__)
44 /* GetSystemTimePreciseAsFileTime was introduced only in Windows 8. */
45 typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME *lpTime);
46 static GetSystemTimePreciseAsFileTimeFuncType GetSystemTimePreciseAsFileTimeFunc = NULL;
47 static BOOL initialized = FALSE;
50 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
51 if (kernel32 != NULL) {
52 GetSystemTimePreciseAsFileTimeFunc =
53 (GetSystemTimePreciseAsFileTimeFuncType) (void *) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime");
58 FILETIME current_time;
60 if (GetSystemTimePreciseAsFileTimeFunc != NULL)
61 GetSystemTimePreciseAsFileTimeFunc (¤t_time);
63 GetSystemTimeAsFileTime (¤t_time);
65 /* Convert from FILETIME to 'struct timeval'. */
66 /* FILETIME: <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime> */
67 ULONGLONG since_1601 =
68 ((ULONGLONG) current_time.dwHighDateTime << 32)
69 | (ULONGLONG) current_time.dwLowDateTime;
70 /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89 leap
71 years, in total 134774 days. */
72 ULONGLONG since_1970 =
73 since_1601 - (ULONGLONG) 134774 * (ULONGLONG) 86400 * (ULONGLONG) 10000000;
74 ULONGLONG microseconds_since_1970 = since_1970 / (ULONGLONG) 10;
75 return cl_timespec(microseconds_since_1970 / (ULONGLONG) 1000000,
76 (microseconds_since_1970 % (ULONGLONG) 1000000) * (1000000000/1000000)
80 return cl_timespec(time(NULL),0);