This source file includes following definitions.
- initialize
- gettimeofday
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22
23 #include <sys/time.h>
24
25 #include <time.h>
26
27 #if defined _WIN32 && ! defined __CYGWIN__
28 # define WINDOWS_NATIVE
29 # include <windows.h>
30 #endif
31
32 #ifdef WINDOWS_NATIVE
33
34
35 # undef LoadLibrary
36 # define LoadLibrary LoadLibraryA
37
38 # if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
39
40
41 # define GetProcAddress \
42 (void *) GetProcAddress
43
44
45 typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME *lpTime);
46 static GetSystemTimePreciseAsFileTimeFuncType GetSystemTimePreciseAsFileTimeFunc = NULL;
47 static BOOL initialized = FALSE;
48
49 static void
50 initialize (void)
51 {
52 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
53 if (kernel32 != NULL)
54 {
55 GetSystemTimePreciseAsFileTimeFunc =
56 (GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime");
57 }
58 initialized = TRUE;
59 }
60
61 # else
62
63 # define GetSystemTimePreciseAsFileTimeFunc GetSystemTimePreciseAsFileTime
64
65 # endif
66
67 #endif
68
69
70
71
72
73
74
75
76
77 int
78 gettimeofday (struct timeval *restrict tv, void *restrict tz)
79 {
80 #undef gettimeofday
81 #ifdef WINDOWS_NATIVE
82
83
84
85
86
87
88
89
90
91
92
93
94
95 FILETIME current_time;
96
97 # if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)
98 if (!initialized)
99 initialize ();
100 # endif
101 if (GetSystemTimePreciseAsFileTimeFunc != NULL)
102 GetSystemTimePreciseAsFileTimeFunc (¤t_time);
103 else
104 GetSystemTimeAsFileTime (¤t_time);
105
106
107
108 ULONGLONG since_1601 =
109 ((ULONGLONG) current_time.dwHighDateTime << 32)
110 | (ULONGLONG) current_time.dwLowDateTime;
111
112
113 ULONGLONG since_1970 =
114 since_1601 - (ULONGLONG) 134774 * (ULONGLONG) 86400 * (ULONGLONG) 10000000;
115 ULONGLONG microseconds_since_1970 = since_1970 / (ULONGLONG) 10;
116 tv->tv_sec = microseconds_since_1970 / (ULONGLONG) 1000000;
117 tv->tv_usec = microseconds_since_1970 % (ULONGLONG) 1000000;
118
119 return 0;
120
121 #else
122
123 # if HAVE_GETTIMEOFDAY
124
125 # if defined timeval
126 # undef timeval
127 struct timeval otv;
128 int result = gettimeofday (&otv, (struct timezone *) tz);
129 if (result == 0)
130 {
131 tv->tv_sec = otv.tv_sec;
132 tv->tv_usec = otv.tv_usec;
133 }
134 # else
135 int result = gettimeofday (tv, (struct timezone *) tz);
136 # endif
137
138 return result;
139
140 # else
141
142 # if !defined OK_TO_USE_1S_CLOCK
143 # error "Only 1-second nominal clock resolution found. Is that intended?" \
144 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
145 # endif
146 tv->tv_sec = time (NULL);
147 tv->tv_usec = 0;
148
149 return 0;
150
151 # endif
152 #endif
153 }