]> www.ginac.de Git - cln.git/blob - src/timing/cl_t_current.cc
[build] Rename cl_asm files to make CMake happy
[cln.git] / src / timing / cl_t_current.cc
1 // cl_current_time().
2
3 #if defined(_WIN32) && !defined(__CYGWIN__)
4 #include <windows.h> // For GetSystemTimeAsFileTime(), must be included first, sorry.
5 #endif
6
7 // General includes.
8 #include "base/cl_sysdep.h"
9
10 // Specification.
11 #include "cln/timing.h"
12
13
14 // Implementation.
15
16 #include "timing/cl_t_config.h"
17
18
19 #if defined(HAVE_GETTIMEOFDAY)
20   #include <sys/time.h>
21 #elif defined(_WIN32) && !defined(__CYGWIN__)
22   /* <windows.h> included above. */
23 #else
24   #include <ctime>
25 #endif
26 #include <cerrno>
27 #include <cstdio>
28
29 namespace cln {
30
31 const cl_timespec cl_current_time ()
32 {
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;
38         }
39         return cl_timespec(tv.tv_sec,
40                            tv.tv_usec * (1000000000/1000000)
41                           );
42 #elif defined(_WIN32) && !defined(__CYGWIN__)
43
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;
48
49         if (!initialized) {
50                 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
51                 if (kernel32 != NULL) {
52                         GetSystemTimePreciseAsFileTimeFunc =
53                                 (GetSystemTimePreciseAsFileTimeFuncType) (void *) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime");
54                 }
55                 initialized = TRUE;
56         }
57
58         FILETIME current_time;
59
60         if (GetSystemTimePreciseAsFileTimeFunc != NULL)
61                 GetSystemTimePreciseAsFileTimeFunc (&current_time);
62         else
63                 GetSystemTimeAsFileTime (&current_time);
64
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)
77                           );
78
79 #else
80         return cl_timespec(time(NULL),0);
81 #endif
82 }
83
84 }  // namespace cln