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