; * lisp/ldefs-boot.el: Update.
[emacs.git] / lib / gettimeofday.c
blobdb4e8f48891990a95323907cd7ceebf5c4be88fc
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
4 Foundation, Inc.
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)
9 any later version.
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 */
21 #include <config.h>
23 /* Specification. */
24 #include <sys/time.h>
26 #include <time.h>
28 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
29 # define WINDOWS_NATIVE
30 # include <windows.h>
31 #endif
33 #include "localtime-buffer.h"
35 #ifdef WINDOWS_NATIVE
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;
42 static void
43 initialize (void)
45 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
46 if (kernel32 != NULL)
48 GetSystemTimePreciseAsFileTimeFunc =
49 (GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime");
51 initialized = TRUE;
54 #endif
56 /* This is a wrapper for gettimeofday. It is used only on systems
57 that lack this function, or whose implementation of this function
58 causes problems.
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
62 this problem. */
64 int
65 gettimeofday (struct timeval *restrict tv, void *restrict tz)
67 #undef gettimeofday
68 #ifdef WINDOWS_NATIVE
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
79 microseconds.
80 More discussion on this topic:
81 <http://www.windowstimestamp.com/description>. */
82 FILETIME current_time;
84 if (!initialized)
85 initialize ();
86 if (GetSystemTimePreciseAsFileTimeFunc != NULL)
87 GetSystemTimePreciseAsFileTimeFunc (&current_time);
88 else
89 GetSystemTimeAsFileTime (&current_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;
104 return 0;
106 #else
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;
113 # endif
115 # if defined timeval /* 'struct timeval' overridden by gnulib? */
116 # undef timeval
117 struct timeval otv;
118 int result = gettimeofday (&otv, (struct timezone *) tz);
119 if (result == 0)
121 tv->tv_sec = otv.tv_sec;
122 tv->tv_usec = otv.tv_usec;
124 # else
125 int result = gettimeofday (tv, (struct timezone *) tz);
126 # endif
128 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
129 *localtime_buffer_addr = save;
130 # endif
132 return result;
134 # else
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."
139 # endif
140 tv->tv_sec = time (NULL);
141 tv->tv_usec = 0;
143 return 0;
145 # endif
146 #endif