Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/curlx/timeval.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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
#include "curlx/timeval.h"
25
26
#ifdef _WIN32
27
28
#include "system_win32.h"
29
30
LARGE_INTEGER Curl_freq;
31
32
/* For tool or tests, we must initialize before calling curlx_now().
33
   Providing this function here is wrong. */
34
void curlx_now_init(void)
35
{
36
  QueryPerformanceFrequency(&Curl_freq);
37
}
38
39
/* In case of bug fix this function has a counterpart in tool_util.c */
40
void curlx_pnow(struct curltime *pnow)
41
{
42
  LARGE_INTEGER count;
43
  DEBUGASSERT(Curl_freq.QuadPart);
44
  QueryPerformanceCounter(&count);
45
  pnow->tv_sec = (time_t)(count.QuadPart / Curl_freq.QuadPart);
46
  pnow->tv_usec = (int)((count.QuadPart % Curl_freq.QuadPart) * 1000000 /
47
                        Curl_freq.QuadPart);
48
}
49
50
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) || \
51
  defined(HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
52
53
void curlx_pnow(struct curltime *pnow)
54
0
{
55
  /*
56
   * clock_gettime() is granted to be increased monotonically when the
57
   * monotonic clock is queried. Time starting point is unspecified, it
58
   * could be the system start-up time, the Epoch, or something else,
59
   * in any case the time starting point does not change once that the
60
   * system has started up.
61
   */
62
0
  struct timespec tsnow;
63
64
  /*
65
   * clock_gettime() may be defined by Apple's SDK as weak symbol thus
66
   * code compiles but fails during runtime if clock_gettime() is
67
   * called on unsupported OS version.
68
   */
69
#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
70
  (HAVE_BUILTIN_AVAILABLE == 1)
71
  bool have_clock_gettime = FALSE;
72
  if(__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *))
73
    have_clock_gettime = TRUE;
74
#endif
75
76
0
#ifdef HAVE_CLOCK_GETTIME_MONOTONIC_RAW
77
0
  if(
78
#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
79
  (HAVE_BUILTIN_AVAILABLE == 1)
80
    have_clock_gettime &&
81
#endif
82
0
    (clock_gettime(CLOCK_MONOTONIC_RAW, &tsnow) == 0)) {
83
0
    pnow->tv_sec = tsnow.tv_sec;
84
0
    pnow->tv_usec = (int)(tsnow.tv_nsec / 1000);
85
0
  }
86
0
  else
87
0
#endif
88
89
0
  if(
90
#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
91
  (HAVE_BUILTIN_AVAILABLE == 1)
92
    have_clock_gettime &&
93
#endif
94
0
    (clock_gettime(CLOCK_MONOTONIC, &tsnow) == 0)) {
95
0
    pnow->tv_sec = tsnow.tv_sec;
96
0
    pnow->tv_usec = (int)(tsnow.tv_nsec / 1000);
97
0
  }
98
  /*
99
   * Even when the configure process has truly detected monotonic clock
100
   * availability, it might happen that it is not actually available at
101
   * runtime. When this occurs, fallback to other time source.
102
   */
103
0
#ifdef HAVE_GETTIMEOFDAY
104
0
  else {
105
0
    struct timeval now;
106
0
    (void)gettimeofday(&now, NULL);
107
0
    pnow->tv_sec = now.tv_sec;
108
0
    pnow->tv_usec = (int)now.tv_usec;
109
0
  }
110
#else
111
  else {
112
    pnow->tv_sec = time(NULL);
113
    pnow->tv_usec = 0;
114
  }
115
#endif
116
0
}
117
118
#elif defined(HAVE_MACH_ABSOLUTE_TIME)
119
120
#include <mach/mach_time.h>
121
122
void curlx_pnow(struct curltime *pnow)
123
{
124
  /*
125
   * Monotonic timer on macOS is provided by mach_absolute_time(), which
126
   * returns time in Mach "absolute time units," which are platform-dependent.
127
   * To convert to nanoseconds, one must use conversion factors specified by
128
   * mach_timebase_info().
129
   */
130
  static mach_timebase_info_data_t timebase;
131
  uint64_t usecs;
132
133
  if(timebase.denom == 0)
134
    (void)mach_timebase_info(&timebase);
135
136
  usecs = mach_absolute_time();
137
  usecs *= timebase.numer; /* spellchecker:disable-line */
138
  usecs /= timebase.denom;
139
  usecs /= 1000;
140
141
  pnow->tv_sec = usecs / 1000000;
142
  pnow->tv_usec = (int)(usecs % 1000000);
143
}
144
145
#elif defined(HAVE_GETTIMEOFDAY)
146
147
void curlx_pnow(struct curltime *pnow)
148
{
149
  /*
150
   * gettimeofday() is not granted to be increased monotonically, due to
151
   * clock drifting and external source time synchronization it can jump
152
   * forward or backward in time.
153
   */
154
  struct timeval now;
155
  (void)gettimeofday(&now, NULL);
156
  pnow->tv_sec = now.tv_sec;
157
  pnow->tv_usec = (int)now.tv_usec;
158
}
159
160
#else
161
162
void curlx_pnow(struct curltime *pnow)
163
{
164
  /*
165
   * time() returns the value of time in seconds since the Epoch.
166
   */
167
  pnow->tv_sec = time(NULL);
168
  pnow->tv_usec = 0;
169
}
170
171
#endif
172
173
struct curltime curlx_now(void)
174
0
{
175
0
  struct curltime now;
176
0
  curlx_pnow(&now);
177
0
  return now;
178
0
}
179
180
/*
181
 * Returns: time difference in number of milliseconds. For too large diffs it
182
 * returns max value.
183
 *
184
 * @unittest: 1323
185
 */
186
timediff_t curlx_ptimediff_ms(const struct curltime *newer,
187
                              const struct curltime *older)
188
0
{
189
0
  timediff_t diff = (timediff_t)newer->tv_sec - older->tv_sec;
190
0
  if(diff >= (TIMEDIFF_T_MAX / 1000))
191
0
    return TIMEDIFF_T_MAX;
192
0
  else if(diff <= (TIMEDIFF_T_MIN / 1000))
193
0
    return TIMEDIFF_T_MIN;
194
0
  return (diff * 1000) + ((newer->tv_usec - older->tv_usec) / 1000);
195
0
}
196
197
timediff_t curlx_timediff_ms(struct curltime newer, struct curltime older)
198
0
{
199
0
  return curlx_ptimediff_ms(&newer, &older);
200
0
}
201
202
/*
203
 * Returns: time difference in number of milliseconds, rounded up.
204
 * For too large diffs it returns max value.
205
 */
206
timediff_t curlx_timediff_ceil_ms(struct curltime newer,
207
                                  struct curltime older)
208
0
{
209
0
  timediff_t diff = (timediff_t)newer.tv_sec - older.tv_sec;
210
0
  if(diff >= (TIMEDIFF_T_MAX / 1000))
211
0
    return TIMEDIFF_T_MAX;
212
0
  else if(diff <= (TIMEDIFF_T_MIN / 1000))
213
0
    return TIMEDIFF_T_MIN;
214
0
  return (diff * 1000) + ((newer.tv_usec - older.tv_usec + 999) / 1000);
215
0
}
216
217
/*
218
 * Returns: time difference in number of microseconds. For too large diffs it
219
 * returns max value.
220
 */
221
timediff_t curlx_ptimediff_us(const struct curltime *newer,
222
                              const struct curltime *older)
223
0
{
224
0
  timediff_t diff = (timediff_t)newer->tv_sec - older->tv_sec;
225
0
  if(diff >= (TIMEDIFF_T_MAX / 1000000))
226
0
    return TIMEDIFF_T_MAX;
227
0
  else if(diff <= (TIMEDIFF_T_MIN / 1000000))
228
0
    return TIMEDIFF_T_MIN;
229
0
  return (diff * 1000000) + newer->tv_usec - older->tv_usec;
230
0
}
231
232
timediff_t curlx_timediff_us(struct curltime newer, struct curltime older)
233
0
{
234
0
  return curlx_ptimediff_us(&newer, &older);
235
0
}
236
237
#if defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR <= 3)
238
#include <sec_api/time_s.h>  /* for _gmtime32_s(), _gmtime64_s() */
239
#ifdef _USE_32BIT_TIME_T
240
#define gmtime_s _gmtime32_s
241
#else
242
#define gmtime_s _gmtime64_s
243
#endif
244
#endif
245
246
/*
247
 * curlx_gmtime() is a gmtime() replacement for portability. Do not use
248
 * the gmtime_s(), gmtime_r() or gmtime() functions anywhere else but here.
249
 */
250
CURLcode curlx_gmtime(time_t intime, struct tm *store)
251
0
{
252
#ifdef _WIN32
253
  if(gmtime_s(store, &intime)) /* thread-safe */
254
    return CURLE_BAD_FUNCTION_ARGUMENT;
255
#elif defined(HAVE_GMTIME_R)
256
  const struct tm *tm;
257
0
  tm = gmtime_r(&intime, store); /* thread-safe */
258
0
  if(!tm)
259
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
260
#else
261
  const struct tm *tm;
262
  /* !checksrc! disable BANNEDFUNC 1 */
263
  tm = gmtime(&intime); /* not thread-safe */
264
  if(tm)
265
    *store = *tm; /* copy the pointed struct to the local copy */
266
  else
267
    return CURLE_BAD_FUNCTION_ARGUMENT;
268
#endif
269
270
0
  return CURLE_OK;
271
0
}