; Add a comment to emacs-lisp-intro.texi
[emacs.git] / lib / gettimeofday.c
blob3957565826407650f49c8f48b0b1ebde3886e0eb
1 /* Provide gettimeofday for systems that don't have it or for which it's broken.
3 Copyright (C) 2001-2003, 2005-2007, 2009-2018 Free Software Foundation, Inc.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
18 /* written by Jim Meyering */
20 #include <config.h>
22 /* Specification. */
23 #include <sys/time.h>
25 #include <time.h>
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
28 # define WINDOWS_NATIVE
29 # include <windows.h>
30 #endif
32 #include "localtime-buffer.h"
34 #ifdef WINDOWS_NATIVE
36 /* GetSystemTimePreciseAsFileTime was introduced only in Windows 8. */
37 typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME *lpTime);
38 static GetSystemTimePreciseAsFileTimeFuncType GetSystemTimePreciseAsFileTimeFunc = NULL;
39 static BOOL initialized = FALSE;
41 static void
42 initialize (void)
44 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
45 if (kernel32 != NULL)
47 GetSystemTimePreciseAsFileTimeFunc =
48 (GetSystemTimePreciseAsFileTimeFuncType) GetProcAddress (kernel32, "GetSystemTimePreciseAsFileTime");
50 initialized = TRUE;
53 #endif
55 /* This is a wrapper for gettimeofday. It is used only on systems
56 that lack this function, or whose implementation of this function
57 causes problems.
58 Work around the bug in some systems whereby gettimeofday clobbers
59 the static buffer that localtime uses for its return value. The
60 gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
61 this problem. */
63 int
64 gettimeofday (struct timeval *restrict tv, void *restrict tz)
66 #undef gettimeofday
67 #ifdef WINDOWS_NATIVE
69 /* On native Windows, there are two ways to get the current time:
70 GetSystemTimeAsFileTime
71 <https://msdn.microsoft.com/en-us/library/ms724397.aspx>
73 GetSystemTimePreciseAsFileTime
74 <https://msdn.microsoft.com/en-us/library/hh706895.aspx>.
75 GetSystemTimeAsFileTime produces values that jump by increments of
76 15.627 milliseconds (!) on average.
77 Whereas GetSystemTimePreciseAsFileTime values usually jump by 1 or 2
78 microseconds.
79 More discussion on this topic:
80 <http://www.windowstimestamp.com/description>. */
81 FILETIME current_time;
83 if (!initialized)
84 initialize ();
85 if (GetSystemTimePreciseAsFileTimeFunc != NULL)
86 GetSystemTimePreciseAsFileTimeFunc (&current_time);
87 else
88 GetSystemTimeAsFileTime (&current_time);
90 /* Convert from FILETIME to 'struct timeval'. */
91 /* FILETIME: <https://msdn.microsoft.com/en-us/library/ms724284.aspx> */
92 ULONGLONG since_1601 =
93 ((ULONGLONG) current_time.dwHighDateTime << 32)
94 | (ULONGLONG) current_time.dwLowDateTime;
95 /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89 leap
96 years, in total 134774 days. */
97 ULONGLONG since_1970 =
98 since_1601 - (ULONGLONG) 134774 * (ULONGLONG) 86400 * (ULONGLONG) 10000000;
99 ULONGLONG microseconds_since_1970 = since_1970 / (ULONGLONG) 10;
100 tv->tv_sec = microseconds_since_1970 / (ULONGLONG) 1000000;
101 tv->tv_usec = microseconds_since_1970 % (ULONGLONG) 1000000;
103 return 0;
105 #else
107 # if HAVE_GETTIMEOFDAY
108 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
109 /* Save and restore the contents of the buffer used for localtime's
110 result around the call to gettimeofday. */
111 struct tm save = *localtime_buffer_addr;
112 # endif
114 # if defined timeval /* 'struct timeval' overridden by gnulib? */
115 # undef timeval
116 struct timeval otv;
117 int result = gettimeofday (&otv, (struct timezone *) tz);
118 if (result == 0)
120 tv->tv_sec = otv.tv_sec;
121 tv->tv_usec = otv.tv_usec;
123 # else
124 int result = gettimeofday (tv, (struct timezone *) tz);
125 # endif
127 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
128 *localtime_buffer_addr = save;
129 # endif
131 return result;
133 # else
135 # if !defined OK_TO_USE_1S_CLOCK
136 # error "Only 1-second nominal clock resolution found. Is that intended?" \
137 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
138 # endif
139 tv->tv_sec = time (NULL);
140 tv->tv_usec = 0;
142 return 0;
144 # endif
145 #endif