Coverage Report

Created: 2026-09-01 06:58

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