1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24
25#include "timeval.h"
26
27#if defined(WIN32) && !defined(MSDOS)
28
29/* set in win32_init() */
30extern LARGE_INTEGER Curl_freq;
31extern bool Curl_isVistaOrGreater;
32
33/* In case of bug fix this function has a counterpart in tool_util.c */
34struct curltime Curl_now(void)
35{
36 struct curltime now;
37 if(Curl_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
38 LARGE_INTEGER count;
39 QueryPerformanceCounter(&count);
40 now.tv_sec = (time_t)(count.QuadPart / Curl_freq.QuadPart);
41 now.tv_usec = (int)((count.QuadPart % Curl_freq.QuadPart) * 1000000 /
42 Curl_freq.QuadPart);
43 }
44 else {
45 /* Disable /analyze warning that GetTickCount64 is preferred */
46#if defined(_MSC_VER)
47#pragma warning(push)
48#pragma warning(disable:28159)
49#endif
50 DWORD milliseconds = GetTickCount();
51#if defined(_MSC_VER)
52#pragma warning(pop)
53#endif
54
55 now.tv_sec = milliseconds / 1000;
56 now.tv_usec = (milliseconds % 1000) * 1000;
57 }
58 return now;
59}
60
61#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
62
63struct curltime Curl_now(void)
64{
65 /*
66 ** clock_gettime() is granted to be increased monotonically when the
67 ** monotonic clock is queried. Time starting point is unspecified, it
68 ** could be the system start-up time, the Epoch, or something else,
69 ** in any case the time starting point does not change once that the
70 ** system has started up.
71 */
72#ifdef HAVE_GETTIMEOFDAY
73 struct timeval now;
74#endif
75 struct curltime cnow;
76 struct timespec tsnow;
77
78 /*
79 ** clock_gettime() may be defined by Apple's SDK as weak symbol thus
80 ** code compiles but fails during run-time if clock_gettime() is
81 ** called on unsupported OS version.
82 */
83#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
84 (HAVE_BUILTIN_AVAILABLE == 1)
85 bool have_clock_gettime = FALSE;
86 if(__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *))
87 have_clock_gettime = TRUE;
88#endif
89
90 if(
91#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
92 (HAVE_BUILTIN_AVAILABLE == 1)
93 have_clock_gettime &&
94#endif
95 (0 == clock_gettime(CLOCK_MONOTONIC, &tsnow))) {
96 cnow.tv_sec = tsnow.tv_sec;
97 cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
98 }
99 /*
100 ** Even when the configure process has truly detected monotonic clock
101 ** availability, it might happen that it is not actually available at
102 ** run-time. When this occurs simply fallback to other time source.
103 */
104#ifdef HAVE_GETTIMEOFDAY
105 else {
106 (void)gettimeofday(&now, NULL);
107 cnow.tv_sec = now.tv_sec;
108 cnow.tv_usec = (unsigned int)now.tv_usec;
109 }
110#else
111 else {
112 cnow.tv_sec = time(NULL);
113 cnow.tv_usec = 0;
114 }
115#endif
116 return cnow;
117}
118
119#elif defined(HAVE_MACH_ABSOLUTE_TIME)
120
121#include <stdint.h>
122#include <mach/mach_time.h>
123
124struct curltime Curl_now(void)
125{
126 /*
127 ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which
128 ** returns time in Mach "absolute time units," which are platform-dependent.
129 ** To convert to nanoseconds, one must use conversion factors specified by
130 ** mach_timebase_info().
131 */
132 static mach_timebase_info_data_t timebase;
133 struct curltime cnow;
134 uint64_t usecs;
135
136 if(0 == timebase.denom)
137 (void) mach_timebase_info(&timebase);
138
139 usecs = mach_absolute_time();
140 usecs *= timebase.numer;
141 usecs /= timebase.denom;
142 usecs /= 1000;
143
144 cnow.tv_sec = usecs / 1000000;
145 cnow.tv_usec = (int)(usecs % 1000000);
146
147 return cnow;
148}
149
150#elif defined(HAVE_GETTIMEOFDAY)
151
152struct curltime Curl_now(void)
153{
154 /*
155 ** gettimeofday() is not granted to be increased monotonically, due to
156 ** clock drifting and external source time synchronization it can jump
157 ** forward or backward in time.
158 */
159 struct timeval now;
160 struct curltime ret;
161 (void)gettimeofday(&now, NULL);
162 ret.tv_sec = now.tv_sec;
163 ret.tv_usec = (int)now.tv_usec;
164 return ret;
165}
166
167#else
168
169struct curltime Curl_now(void)
170{
171 /*
172 ** time() returns the value of time in seconds since the Epoch.
173 */
174 struct curltime now;
175 now.tv_sec = time(NULL);
176 now.tv_usec = 0;
177 return now;
178}
179
180#endif
181
182/*
183 * Returns: time difference in number of milliseconds. For too large diffs it
184 * returns max value.
185 *
186 * @unittest: 1323
187 */
188timediff_t Curl_timediff(struct curltime newer, struct curltime older)
189{
190 timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
191 if(diff >= (TIMEDIFF_T_MAX/1000))
192 return TIMEDIFF_T_MAX;
193 else if(diff <= (TIMEDIFF_T_MIN/1000))
194 return TIMEDIFF_T_MIN;
195 return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
196}
197
198/*
199 * Returns: time difference in number of microseconds. For too large diffs it
200 * returns max value.
201 */
202timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
203{
204 timediff_t diff = (timediff_t)newer.tv_sec-older.tv_sec;
205 if(diff >= (TIMEDIFF_T_MAX/1000000))
206 return TIMEDIFF_T_MAX;
207 else if(diff <= (TIMEDIFF_T_MIN/1000000))
208 return TIMEDIFF_T_MIN;
209 return diff * 1000000 + newer.tv_usec-older.tv_usec;
210}
211