1 /* Provide gettimeofday for systems that don't have it or for which it's broken.
3 Copyright (C) 2001-2003, 2005-2007, 2009-2019 Free Software
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <https://www.gnu.org/licenses/>. */
19 /* written by Jim Meyering */
28 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
29 # define WINDOWS_NATIVE
33 #include "localtime-buffer.h"
37 /* GetSystemTimePreciseAsFileTime was introduced only in Windows 8. */
38 typedef void (WINAPI
* GetSystemTimePreciseAsFileTimeFuncType
) (FILETIME
*lpTime
);
39 static GetSystemTimePreciseAsFileTimeFuncType GetSystemTimePreciseAsFileTimeFunc
= NULL
;
40 static BOOL initialized
= FALSE
;
45 HMODULE kernel32
= LoadLibrary ("kernel32.dll");
48 GetSystemTimePreciseAsFileTimeFunc
=
49 (GetSystemTimePreciseAsFileTimeFuncType
) GetProcAddress (kernel32
, "GetSystemTimePreciseAsFileTime");
56 /* This is a wrapper for gettimeofday. It is used only on systems
57 that lack this function, or whose implementation of this function
59 Work around the bug in some systems whereby gettimeofday clobbers
60 the static buffer that localtime uses for its return value. The
61 gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
65 gettimeofday (struct timeval
*restrict tv
, void *restrict tz
)
70 /* On native Windows, there are two ways to get the current time:
71 GetSystemTimeAsFileTime
72 <https://msdn.microsoft.com/en-us/library/ms724397.aspx>
74 GetSystemTimePreciseAsFileTime
75 <https://msdn.microsoft.com/en-us/library/hh706895.aspx>.
76 GetSystemTimeAsFileTime produces values that jump by increments of
77 15.627 milliseconds (!) on average.
78 Whereas GetSystemTimePreciseAsFileTime values usually jump by 1 or 2
80 More discussion on this topic:
81 <http://www.windowstimestamp.com/description>. */
82 FILETIME current_time
;
86 if (GetSystemTimePreciseAsFileTimeFunc
!= NULL
)
87 GetSystemTimePreciseAsFileTimeFunc (¤t_time
);
89 GetSystemTimeAsFileTime (¤t_time
);
91 /* Convert from FILETIME to 'struct timeval'. */
92 /* FILETIME: <https://msdn.microsoft.com/en-us/library/ms724284.aspx> */
93 ULONGLONG since_1601
=
94 ((ULONGLONG
) current_time
.dwHighDateTime
<< 32)
95 | (ULONGLONG
) current_time
.dwLowDateTime
;
96 /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89 leap
97 years, in total 134774 days. */
98 ULONGLONG since_1970
=
99 since_1601
- (ULONGLONG
) 134774 * (ULONGLONG
) 86400 * (ULONGLONG
) 10000000;
100 ULONGLONG microseconds_since_1970
= since_1970
/ (ULONGLONG
) 10;
101 tv
->tv_sec
= microseconds_since_1970
/ (ULONGLONG
) 1000000;
102 tv
->tv_usec
= microseconds_since_1970
% (ULONGLONG
) 1000000;
108 # if HAVE_GETTIMEOFDAY
109 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
110 /* Save and restore the contents of the buffer used for localtime's
111 result around the call to gettimeofday. */
112 struct tm save
= *localtime_buffer_addr
;
115 # if defined timeval /* 'struct timeval' overridden by gnulib? */
118 int result
= gettimeofday (&otv
, (struct timezone
*) tz
);
121 tv
->tv_sec
= otv
.tv_sec
;
122 tv
->tv_usec
= otv
.tv_usec
;
125 int result
= gettimeofday (tv
, (struct timezone
*) tz
);
128 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
129 *localtime_buffer_addr
= save
;
136 # if !defined OK_TO_USE_1S_CLOCK
137 # error "Only 1-second nominal clock resolution found. Is that intended?" \
138 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
140 tv
->tv_sec
= time (NULL
);