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