Commit | Line | Data |
---|---|---|
4a626d0a PA |
1 | /* Provide gettimeofday for systems that don't have it or for which it's broken. |
2 | ||
5df4cba6 | 3 | Copyright (C) 2001-2003, 2005-2007, 2009-2020 Free Software Foundation, Inc. |
4a626d0a PA |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3, or (at your option) | |
8 | any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
c0c3707f | 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 | #include "localtime-buffer.h" |
4a626d0a | 33 | |
c0c3707f | 34 | #ifdef WINDOWS_NATIVE |
4a626d0a | 35 | |
c0c3707f CB |
36 | /* Avoid warnings from gcc -Wcast-function-type. */ |
37 | # define GetProcAddress \ | |
38 | (void *) GetProcAddress | |
4a626d0a | 39 | |
c0c3707f CB |
40 | /* GetSystemTimePreciseAsFileTime was introduced only in Windows 8. */ |
41 | typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME *lpTime); | |
42 | static GetSystemTimePreciseAsFileTimeFuncType GetSystemTimePreciseAsFileTimeFunc = NULL; | |
43 | static BOOL initialized = FALSE; | |
4a626d0a | 44 | |
c0c3707f CB |
45 | static void |
46 | initialize (void) | |
4a626d0a | 47 | { |
c0c3707f CB |
48 | HMODULE kernel32 = LoadLibrary ("kernel32.dll"); |
49 | if (kernel32 != NULL) | |
50 | { | |
51 | GetSystemTimePreciseAsFileTimeFunc = | |
52 | (GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime"); | |
53 | } | |
54 | initialized = TRUE; | |
4a626d0a PA |
55 | } |
56 | ||
4a626d0a PA |
57 | #endif |
58 | ||
59 | /* This is a wrapper for gettimeofday. It is used only on systems | |
60 | that lack this function, or whose implementation of this function | |
c0c3707f CB |
61 | causes problems. |
62 | Work around the bug in some systems whereby gettimeofday clobbers | |
63 | the static buffer that localtime uses for its return value. The | |
64 | gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has | |
65 | this problem. */ | |
4a626d0a PA |
66 | |
67 | int | |
68 | gettimeofday (struct timeval *restrict tv, void *restrict tz) | |
69 | { | |
70 | #undef gettimeofday | |
c0c3707f CB |
71 | #ifdef WINDOWS_NATIVE |
72 | ||
73 | /* On native Windows, there are two ways to get the current time: | |
74 | GetSystemTimeAsFileTime | |
75 | <https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime> | |
76 | or | |
77 | GetSystemTimePreciseAsFileTime | |
78 | <https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime>. | |
79 | GetSystemTimeAsFileTime produces values that jump by increments of | |
80 | 15.627 milliseconds (!) on average. | |
81 | Whereas GetSystemTimePreciseAsFileTime values usually jump by 1 or 2 | |
82 | microseconds. | |
83 | More discussion on this topic: | |
84 | <http://www.windowstimestamp.com/description>. */ | |
85 | FILETIME current_time; | |
86 | ||
87 | if (!initialized) | |
88 | initialize (); | |
89 | if (GetSystemTimePreciseAsFileTimeFunc != NULL) | |
90 | GetSystemTimePreciseAsFileTimeFunc (¤t_time); | |
91 | else | |
92 | GetSystemTimeAsFileTime (¤t_time); | |
93 | ||
94 | /* Convert from FILETIME to 'struct timeval'. */ | |
95 | /* FILETIME: <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime> */ | |
96 | ULONGLONG since_1601 = | |
97 | ((ULONGLONG) current_time.dwHighDateTime << 32) | |
98 | | (ULONGLONG) current_time.dwLowDateTime; | |
99 | /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89 leap | |
100 | years, in total 134774 days. */ | |
101 | ULONGLONG since_1970 = | |
102 | since_1601 - (ULONGLONG) 134774 * (ULONGLONG) 86400 * (ULONGLONG) 10000000; | |
103 | ULONGLONG microseconds_since_1970 = since_1970 / (ULONGLONG) 10; | |
104 | tv->tv_sec = microseconds_since_1970 / (ULONGLONG) 1000000; | |
105 | tv->tv_usec = microseconds_since_1970 % (ULONGLONG) 1000000; | |
106 | ||
107 | return 0; | |
108 | ||
109 | #else | |
110 | ||
111 | # if HAVE_GETTIMEOFDAY | |
112 | # if GETTIMEOFDAY_CLOBBERS_LOCALTIME | |
4a626d0a PA |
113 | /* Save and restore the contents of the buffer used for localtime's |
114 | result around the call to gettimeofday. */ | |
115 | struct tm save = *localtime_buffer_addr; | |
c0c3707f | 116 | # endif |
4a626d0a | 117 | |
c0c3707f CB |
118 | # if defined timeval /* 'struct timeval' overridden by gnulib? */ |
119 | # undef timeval | |
4a626d0a PA |
120 | struct timeval otv; |
121 | int result = gettimeofday (&otv, (struct timezone *) tz); | |
122 | if (result == 0) | |
123 | { | |
124 | tv->tv_sec = otv.tv_sec; | |
125 | tv->tv_usec = otv.tv_usec; | |
126 | } | |
c0c3707f | 127 | # else |
4a626d0a | 128 | int result = gettimeofday (tv, (struct timezone *) tz); |
c0c3707f | 129 | # endif |
4a626d0a | 130 | |
c0c3707f | 131 | # if GETTIMEOFDAY_CLOBBERS_LOCALTIME |
4a626d0a | 132 | *localtime_buffer_addr = save; |
c0c3707f | 133 | # endif |
4a626d0a PA |
134 | |
135 | return result; | |
136 | ||
4a626d0a PA |
137 | # else |
138 | ||
139 | # if !defined OK_TO_USE_1S_CLOCK | |
140 | # error "Only 1-second nominal clock resolution found. Is that intended?" \ | |
141 | "If so, compile with the -DOK_TO_USE_1S_CLOCK option." | |
142 | # endif | |
143 | tv->tv_sec = time (NULL); | |
144 | tv->tv_usec = 0; | |
145 | ||
4a626d0a PA |
146 | return 0; |
147 | ||
c0c3707f | 148 | # endif |
4a626d0a PA |
149 | #endif |
150 | } |