3 /* Copyright (C) 2008 by Daniel Stenberg et al
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation for any purpose and without fee is hereby granted, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. M.I.T. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
18 #include "ares_private.h"
20 #if defined(WIN32) && !defined(MSDOS)
22 struct timeval ares__tvnow(void)
25 ** GetTickCount() is available on _all_ Windows versions from W95 up
26 ** to nowadays. Returns milliseconds elapsed since last system boot,
27 ** increases monotonically and wraps once 49.7 days have elapsed.
30 DWORD milliseconds = GetTickCount();
31 now.tv_sec = milliseconds / 1000;
32 now.tv_usec = (milliseconds % 1000) * 1000;
36 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
38 struct timeval ares__tvnow(void)
41 ** clock_gettime() is granted to be increased monotonically when the
42 ** monotonic clock is queried. Time starting point is unspecified, it
43 ** could be the system start-up time, the Epoch, or something else,
44 ** in any case the time starting point does not change once that the
45 ** system has started up.
48 struct timespec tsnow;
49 if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
50 now.tv_sec = tsnow.tv_sec;
51 now.tv_usec = tsnow.tv_nsec / 1000;
54 ** Even when the configure process has truly detected monotonic clock
55 ** availability, it might happen that it is not actually available at
56 ** run-time. When this occurs simply fallback to other time source.
58 #ifdef HAVE_GETTIMEOFDAY
60 (void)gettimeofday(&now, NULL);
63 now.tv_sec = (long)time(NULL);
70 #elif defined(HAVE_GETTIMEOFDAY)
72 struct timeval ares__tvnow(void)
75 ** gettimeofday() is not granted to be increased monotonically, due to
76 ** clock drifting and external source time synchronization it can jump
77 ** forward or backward in time.
80 (void)gettimeofday(&now, NULL);
86 struct timeval ares__tvnow(void)
89 ** time() returns the value of time in seconds since the Epoch.
92 now.tv_sec = (long)time(NULL);
101 * Make sure that the first argument is the more recent time, as otherwise
102 * we'll get a weird negative time-diff back...
104 * Returns: the time difference in number of milliseconds.
106 long ares__tvdiff(struct timeval newer, struct timeval older)
108 return (newer.tv_sec-older.tv_sec)*1000+
109 (newer.tv_usec-older.tv_usec)/1000;