]>
Commit | Line | Data |
---|---|---|
4a626d0a PA |
1 | /* Provide gettimeofday for systems that don't have it or for which it's broken. |
2 | ||
dc6c21da | 3 | Copyright (C) 2001-2003, 2005-2007, 2009-2022 Free Software Foundation, Inc. |
4a626d0a | 4 | |
dc6c21da TT |
5 | This file is free software: you can redistribute it and/or modify |
6 | it under the terms of the GNU Lesser General Public License as | |
7 | published by the Free Software Foundation; either version 2.1 of the | |
8 | License, or (at your option) any later version. | |
4a626d0a | 9 | |
dc6c21da | 10 | This file is distributed in the hope that it will be useful, |
4a626d0a PA |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
dc6c21da | 13 | GNU Lesser General Public License for more details. |
4a626d0a | 14 | |
dc6c21da TT |
15 | You should have received a copy of the GNU Lesser General Public License |
16 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | |
4a626d0a PA |
17 | |
18 | /* written by Jim Meyering */ | |
19 | ||
20 | #include <config.h> | |
21 | ||
22 | /* Specification. */ | |
23 | #include <sys/time.h> | |
24 | ||
25 | #include <time.h> | |
26 | ||
c0c3707f CB |
27 | #if defined _WIN32 && ! defined __CYGWIN__ |
28 | # define WINDOWS_NATIVE | |
29 | # include <windows.h> | |
4a626d0a PA |
30 | #endif |
31 | ||
c0c3707f | 32 | #ifdef WINDOWS_NATIVE |
4a626d0a | 33 | |
698be2d8 CB |
34 | /* Don't assume that UNICODE is not defined. */ |
35 | # undef LoadLibrary | |
36 | # define LoadLibrary LoadLibraryA | |
37 | ||
38 | # if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8) | |
39 | ||
c0c3707f | 40 | /* Avoid warnings from gcc -Wcast-function-type. */ |
698be2d8 CB |
41 | # define GetProcAddress \ |
42 | (void *) GetProcAddress | |
4a626d0a | 43 | |
c0c3707f CB |
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; | |
4a626d0a | 48 | |
c0c3707f CB |
49 | static void |
50 | initialize (void) | |
4a626d0a | 51 | { |
c0c3707f CB |
52 | HMODULE kernel32 = LoadLibrary ("kernel32.dll"); |
53 | if (kernel32 != NULL) | |
54 | { | |
55 | GetSystemTimePreciseAsFileTimeFunc = | |
56 | (GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime"); | |
57 | } | |
58 | initialized = TRUE; | |
4a626d0a PA |
59 | } |
60 | ||
698be2d8 CB |
61 | # else |
62 | ||
63 | # define GetSystemTimePreciseAsFileTimeFunc GetSystemTimePreciseAsFileTime | |
64 | ||
65 | # endif | |
66 | ||
4a626d0a PA |
67 | #endif |
68 | ||
69 | /* This is a wrapper for gettimeofday. It is used only on systems | |
70 | that lack this function, or whose implementation of this function | |
c0c3707f CB |
71 | causes problems. |
72 | Work around the bug in some systems whereby gettimeofday clobbers | |
73 | the static buffer that localtime uses for its return value. The | |
74 | gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has | |
75 | this problem. */ | |
4a626d0a PA |
76 | |
77 | int | |
78 | gettimeofday (struct timeval *restrict tv, void *restrict tz) | |
79 | { | |
80 | #undef gettimeofday | |
c0c3707f CB |
81 | #ifdef WINDOWS_NATIVE |
82 | ||
83 | /* On native Windows, there are two ways to get the current time: | |
84 | GetSystemTimeAsFileTime | |
85 | <https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime> | |
86 | or | |
87 | GetSystemTimePreciseAsFileTime | |
88 | <https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime>. | |
89 | GetSystemTimeAsFileTime produces values that jump by increments of | |
90 | 15.627 milliseconds (!) on average. | |
91 | Whereas GetSystemTimePreciseAsFileTime values usually jump by 1 or 2 | |
92 | microseconds. | |
93 | More discussion on this topic: | |
94 | <http://www.windowstimestamp.com/description>. */ | |
95 | FILETIME current_time; | |
96 | ||
698be2d8 | 97 | # if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8) |
c0c3707f CB |
98 | if (!initialized) |
99 | initialize (); | |
698be2d8 | 100 | # endif |
c0c3707f CB |
101 | if (GetSystemTimePreciseAsFileTimeFunc != NULL) |
102 | GetSystemTimePreciseAsFileTimeFunc (¤t_time); | |
103 | else | |
104 | GetSystemTimeAsFileTime (¤t_time); | |
105 | ||
106 | /* Convert from FILETIME to 'struct timeval'. */ | |
107 | /* FILETIME: <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime> */ | |
108 | ULONGLONG since_1601 = | |
109 | ((ULONGLONG) current_time.dwHighDateTime << 32) | |
110 | | (ULONGLONG) current_time.dwLowDateTime; | |
111 | /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89 leap | |
112 | years, in total 134774 days. */ | |
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 | |
4a626d0a | 124 | |
c0c3707f CB |
125 | # if defined timeval /* 'struct timeval' overridden by gnulib? */ |
126 | # undef timeval | |
4a626d0a PA |
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 | } | |
c0c3707f | 134 | # else |
4a626d0a | 135 | int result = gettimeofday (tv, (struct timezone *) tz); |
c0c3707f | 136 | # endif |
4a626d0a | 137 | |
4a626d0a PA |
138 | return result; |
139 | ||
4a626d0a PA |
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 | ||
4a626d0a PA |
149 | return 0; |
150 | ||
c0c3707f | 151 | # endif |
4a626d0a PA |
152 | #endif |
153 | } |