blob: f4bf83531f35628749d98718323b3c26da61cb93 [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Elliott Hughes82be86d2017-09-20 17:00:17 -07008 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymo8f1a2142016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
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 ***************************************************************************/
22
23#include "timeval.h"
24
25#if defined(WIN32) && !defined(MSDOS)
26
Alex Deymo486467e2017-12-19 19:04:07 +010027struct curltime Curl_now(void)
Kristian Monsen5ab50182010-05-14 18:53:44 +010028{
29 /*
30 ** GetTickCount() is available on _all_ Windows versions from W95 up
31 ** to nowadays. Returns milliseconds elapsed since last system boot,
32 ** increases monotonically and wraps once 49.7 days have elapsed.
33 */
Elliott Hughes82be86d2017-09-20 17:00:17 -070034 struct curltime now;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070035#if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
36 (_WIN32_WINNT < _WIN32_WINNT_VISTA)
Kristian Monsen5ab50182010-05-14 18:53:44 +010037 DWORD milliseconds = GetTickCount();
38 now.tv_sec = milliseconds / 1000;
39 now.tv_usec = (milliseconds % 1000) * 1000;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070040#else
41 ULONGLONG milliseconds = GetTickCount64();
Elliott Hughes82be86d2017-09-20 17:00:17 -070042 now.tv_sec = (time_t) (milliseconds / 1000);
43 now.tv_usec = (unsigned int) (milliseconds % 1000) * 1000;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070044#endif
45
Kristian Monsen5ab50182010-05-14 18:53:44 +010046 return now;
47}
48
49#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
50
Alex Deymo486467e2017-12-19 19:04:07 +010051struct curltime Curl_now(void)
Kristian Monsen5ab50182010-05-14 18:53:44 +010052{
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 struct timeval now;
Elliott Hughes82be86d2017-09-20 17:00:17 -070061 struct curltime cnow;
Kristian Monsen5ab50182010-05-14 18:53:44 +010062 struct timespec tsnow;
63 if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
Elliott Hughes82be86d2017-09-20 17:00:17 -070064 cnow.tv_sec = tsnow.tv_sec;
65 cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
Kristian Monsen5ab50182010-05-14 18:53:44 +010066 }
67 /*
68 ** Even when the configure process has truly detected monotonic clock
69 ** availability, it might happen that it is not actually available at
70 ** run-time. When this occurs simply fallback to other time source.
71 */
72#ifdef HAVE_GETTIMEOFDAY
Elliott Hughes82be86d2017-09-20 17:00:17 -070073 else {
Kristian Monsen5ab50182010-05-14 18:53:44 +010074 (void)gettimeofday(&now, NULL);
Elliott Hughes82be86d2017-09-20 17:00:17 -070075 cnow.tv_sec = now.tv_sec;
76 cnow.tv_usec = (unsigned int)now.tv_usec;
77 }
Kristian Monsen5ab50182010-05-14 18:53:44 +010078#else
79 else {
Elliott Hughes82be86d2017-09-20 17:00:17 -070080 cnow.tv_sec = time(NULL);
81 cnow.tv_usec = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +010082 }
83#endif
Elliott Hughes82be86d2017-09-20 17:00:17 -070084 return cnow;
Kristian Monsen5ab50182010-05-14 18:53:44 +010085}
86
Alex Deymo486467e2017-12-19 19:04:07 +010087#elif defined(HAVE_MACH_ABSOLUTE_TIME)
88
89#include <stdint.h>
90#include <mach/mach_time.h>
91
92struct curltime Curl_now(void)
93{
94 /*
95 ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which
96 ** returns time in Mach "absolute time units," which are platform-dependent.
97 ** To convert to nanoseconds, one must use conversion factors specified by
98 ** mach_timebase_info().
99 */
100 static mach_timebase_info_data_t timebase;
101 struct curltime cnow;
102 uint64_t usecs;
103
104 if(0 == timebase.denom)
105 (void) mach_timebase_info(&timebase);
106
107 usecs = mach_absolute_time();
108 usecs *= timebase.numer;
109 usecs /= timebase.denom;
110 usecs /= 1000;
111
112 cnow.tv_sec = usecs / 1000000;
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700113 cnow.tv_usec = (int)(usecs % 1000000);
Alex Deymo486467e2017-12-19 19:04:07 +0100114
115 return cnow;
116}
117
Kristian Monsen5ab50182010-05-14 18:53:44 +0100118#elif defined(HAVE_GETTIMEOFDAY)
119
Alex Deymo486467e2017-12-19 19:04:07 +0100120struct curltime Curl_now(void)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100121{
122 /*
123 ** gettimeofday() is not granted to be increased monotonically, due to
124 ** clock drifting and external source time synchronization it can jump
125 ** forward or backward in time.
126 */
127 struct timeval now;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700128 struct curltime ret;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100129 (void)gettimeofday(&now, NULL);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700130 ret.tv_sec = now.tv_sec;
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700131 ret.tv_usec = (int)now.tv_usec;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700132 return ret;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100133}
134
135#else
136
Alex Deymo486467e2017-12-19 19:04:07 +0100137struct curltime Curl_now(void)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100138{
139 /*
140 ** time() returns the value of time in seconds since the Epoch.
141 */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700142 struct curltime now;
143 now.tv_sec = time(NULL);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100144 now.tv_usec = 0;
145 return now;
146}
147
148#endif
149
Alex Deymo486467e2017-12-19 19:04:07 +0100150#if SIZEOF_TIME_T < 8
151#define TIME_MAX INT_MAX
152#define TIME_MIN INT_MIN
153#else
154#define TIME_MAX 9223372036854775807LL
155#define TIME_MIN -9223372036854775807LL
156#endif
157
Kristian Monsen5ab50182010-05-14 18:53:44 +0100158/*
Alex Deymo486467e2017-12-19 19:04:07 +0100159 * Returns: time difference in number of milliseconds. For too large diffs it
160 * returns max value.
Elliott Hughes82be86d2017-09-20 17:00:17 -0700161 *
162 * @unittest: 1323
Kristian Monsen5ab50182010-05-14 18:53:44 +0100163 */
Alex Deymo486467e2017-12-19 19:04:07 +0100164timediff_t Curl_timediff(struct curltime newer, struct curltime older)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100165{
Alex Deymo486467e2017-12-19 19:04:07 +0100166 timediff_t diff = newer.tv_sec-older.tv_sec;
167 if(diff >= (TIME_MAX/1000))
168 return TIME_MAX;
169 else if(diff <= (TIME_MIN/1000))
170 return TIME_MIN;
171 return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100172}
173
174/*
Alex Deymo486467e2017-12-19 19:04:07 +0100175 * Returns: time difference in number of microseconds. For too large diffs it
176 * returns max value.
Kristian Monsen5ab50182010-05-14 18:53:44 +0100177 */
Alex Deymo486467e2017-12-19 19:04:07 +0100178timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100179{
Alex Deymo486467e2017-12-19 19:04:07 +0100180 timediff_t diff = newer.tv_sec-older.tv_sec;
181 if(diff >= (TIME_MAX/1000000))
182 return TIME_MAX;
183 else if(diff <= (TIME_MIN/1000000))
184 return TIME_MIN;
185 return diff * 1000000 + newer.tv_usec-older.tv_usec;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100186}