blob: a46e0f9fc1340081b47cf7958094c0d4935ec526 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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 "curl_setup.h"
25#include <curl/curl.h>
26#include "testutil.h"
27#include "memdebug.h"
28
29#if defined(WIN32) && !defined(MSDOS)
30
31struct timeval tutil_tvnow(void)
32{
33 /*
34 ** GetTickCount() is available on _all_ Windows versions from W95 up
35 ** to nowadays. Returns milliseconds elapsed since last system boot,
36 ** increases monotonically and wraps once 49.7 days have elapsed.
37 */
38 struct timeval now;
39 DWORD milliseconds = GetTickCount();
40 now.tv_sec = milliseconds / 1000;
41 now.tv_usec = (milliseconds % 1000) * 1000;
42 return now;
43}
44
45#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
46
47struct timeval tutil_tvnow(void)
48{
49 /*
50 ** clock_gettime() is granted to be increased monotonically when the
51 ** monotonic clock is queried. Time starting point is unspecified, it
52 ** could be the system start-up time, the Epoch, or something else,
53 ** in any case the time starting point does not change once that the
54 ** system has started up.
55 */
56 struct timeval now;
57 struct timespec tsnow;
58 if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
59 now.tv_sec = tsnow.tv_sec;
60 now.tv_usec = (int)(tsnow.tv_nsec / 1000);
61 }
62 /*
63 ** Even when the configure process has truly detected monotonic clock
64 ** availability, it might happen that it is not actually available at
65 ** run-time. When this occurs simply fallback to other time source.
66 */
67#ifdef HAVE_GETTIMEOFDAY
68 else
69 (void)gettimeofday(&now, NULL);
70#else
71 else {
72 now.tv_sec = time(NULL);
73 now.tv_usec = 0;
74 }
75#endif
76 return now;
77}
78
79#elif defined(HAVE_GETTIMEOFDAY)
80
81struct timeval tutil_tvnow(void)
82{
83 /*
84 ** gettimeofday() is not granted to be increased monotonically, due to
85 ** clock drifting and external source time synchronization it can jump
86 ** forward or backward in time.
87 */
88 struct timeval now;
89 (void)gettimeofday(&now, NULL);
90 return now;
91}
92
93#else
94
95struct timeval tutil_tvnow(void)
96{
97 /*
98 ** time() returns the value of time in seconds since the Epoch.
99 */
100 struct timeval now;
101 now.tv_sec = time(NULL);
102 now.tv_usec = 0;
103 return now;
104}
105
106#endif
107
108/*
109 * Make sure that the first argument is the more recent time, as otherwise
110 * we'll get a weird negative time-diff back...
111 *
112 * Returns: the time difference in number of milliseconds.
113 */
114long tutil_tvdiff(struct timeval newer, struct timeval older)
115{
116 return (long)(newer.tv_sec-older.tv_sec)*1000+
117 (long)(newer.tv_usec-older.tv_usec)/1000;
118}
119
120
121/*
122 * Same as tutil_tvdiff but with full usec resolution.
123 *
124 * Returns: the time difference in seconds with subsecond resolution.
125 */
126double tutil_tvdiff_secs(struct timeval newer, struct timeval older)
127{
128 if(newer.tv_sec != older.tv_sec)
129 return (double)(newer.tv_sec-older.tv_sec)+
130 (double)(newer.tv_usec-older.tv_usec)/1000000.0;
131 return (double)(newer.tv_usec-older.tv_usec)/1000000.0;
132}