1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #if defined(WIN32) && !defined(MSDOS)
27 struct curltime Curl_now(void)
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.
35 #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
36 (_WIN32_WINNT < _WIN32_WINNT_VISTA)
37 DWORD milliseconds = GetTickCount();
38 now.tv_sec = milliseconds / 1000;
39 now.tv_usec = (milliseconds % 1000) * 1000;
41 ULONGLONG milliseconds = GetTickCount64();
42 now.tv_sec = (time_t) (milliseconds / 1000);
43 now.tv_usec = (unsigned int) (milliseconds % 1000) * 1000;
49 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
51 struct curltime Curl_now(void)
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.
62 struct timespec tsnow;
63 if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
64 cnow.tv_sec = tsnow.tv_sec;
65 cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
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.
72 #ifdef HAVE_GETTIMEOFDAY
74 (void)gettimeofday(&now, NULL);
75 cnow.tv_sec = now.tv_sec;
76 cnow.tv_usec = (unsigned int)now.tv_usec;
80 cnow.tv_sec = time(NULL);
87 #elif defined(HAVE_MACH_ABSOLUTE_TIME)
90 #include <mach/mach_time.h>
92 struct curltime Curl_now(void)
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().
100 static mach_timebase_info_data_t timebase;
101 struct curltime cnow;
104 if(0 == timebase.denom)
105 (void) mach_timebase_info(&timebase);
107 usecs = mach_absolute_time();
108 usecs *= timebase.numer;
109 usecs /= timebase.denom;
112 cnow.tv_sec = usecs / 1000000;
113 cnow.tv_usec = usecs % 1000000;
118 #elif defined(HAVE_GETTIMEOFDAY)
120 struct curltime Curl_now(void)
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.
129 (void)gettimeofday(&now, NULL);
130 ret.tv_sec = now.tv_sec;
131 ret.tv_usec = now.tv_usec;
137 struct curltime Curl_now(void)
140 ** time() returns the value of time in seconds since the Epoch.
143 now.tv_sec = time(NULL);
150 #if SIZEOF_TIME_T < 8
151 #define TIME_MAX INT_MAX
152 #define TIME_MIN INT_MIN
154 #define TIME_MAX 9223372036854775807LL
155 #define TIME_MIN -9223372036854775807LL
159 * Returns: time difference in number of milliseconds. For too large diffs it
164 timediff_t Curl_timediff(struct curltime newer, struct curltime older)
166 timediff_t diff = newer.tv_sec-older.tv_sec;
167 if(diff >= (TIME_MAX/1000))
169 else if(diff <= (TIME_MIN/1000))
171 return diff * 1000 + (newer.tv_usec-older.tv_usec)/1000;
175 * Returns: time difference in number of microseconds. For too large diffs it
178 timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
180 timediff_t diff = newer.tv_sec-older.tv_sec;
181 if(diff >= (TIME_MAX/1000000))
183 else if(diff <= (TIME_MIN/1000000))
185 return diff * 1000000 + newer.tv_usec-older.tv_usec;