1 /* MIT License
2 *
3 * Copyright (c) 2008 Daniel Stenberg
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26
27 #include "ares_private.h"
28
29 #if defined(_WIN32) && !defined(MSDOS)
30
ares_tvnow(ares_timeval_t * now)31 void ares_tvnow(ares_timeval_t *now)
32 {
33 /* QueryPerformanceCounters() has been around since Windows 2000, though
34 * significant fixes were made in later versions. Documentation states
35 * 1 microsecond or better resolution with a rollover not less than 100 years.
36 * This differs from GetTickCount{64}() which has a resolution between 10 and
37 * 16 ms. */
38 LARGE_INTEGER freq;
39 LARGE_INTEGER current;
40
41 /* Not sure how long it takes to get the frequency, I see it recommended to
42 * cache it */
43 QueryPerformanceFrequency(&freq);
44 QueryPerformanceCounter(¤t);
45
46 now->sec = current.QuadPart / freq.QuadPart;
47 /* We want to prevent overflows so we get the remainder, then multiply to
48 * microseconds before dividing */
49 now->usec = (unsigned int)(((current.QuadPart % freq.QuadPart) * 1000000) /
50 freq.QuadPart);
51 }
52
53 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
54
ares_tvnow(ares_timeval_t * now)55 void ares_tvnow(ares_timeval_t *now)
56 {
57 /* clock_gettime() is guaranteed to be increased monotonically when the
58 * monotonic clock is queried. Time starting point is unspecified, it
59 * could be the system start-up time, the Epoch, or something else,
60 * in any case the time starting point does not change once that the
61 * system has started up. */
62 struct timespec tsnow;
63
64 if (clock_gettime(CLOCK_MONOTONIC, &tsnow) == 0) {
65 now->sec = (ares_int64_t)tsnow.tv_sec;
66 now->usec = (unsigned int)(tsnow.tv_nsec / 1000);
67 } else {
68 /* LCOV_EXCL_START: FallbackCode */
69 struct timeval tv;
70 (void)gettimeofday(&tv, NULL);
71 now->sec = (ares_int64_t)tv.tv_sec;
72 now->usec = (unsigned int)tv.tv_usec;
73 /* LCOV_EXCL_STOP */
74 }
75 }
76
77 #elif defined(HAVE_GETTIMEOFDAY)
78
ares_tvnow(ares_timeval_t * now)79 void ares_tvnow(ares_timeval_t *now)
80 {
81 /* gettimeofday() is not granted to be increased monotonically, due to
82 * clock drifting and external source time synchronization it can jump
83 * forward or backward in time. */
84 struct timeval tv;
85
86 (void)gettimeofday(&tv, NULL);
87 now->sec = (ares_int64_t)tv.tv_sec;
88 now->usec = (unsigned int)tv.tv_usec;
89 }
90
91 #else
92
93 # error missing sub-second time retrieval function
94
95 #endif
96