Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares__timeval.c
1 /* $Id$ */
2
3 /* Copyright (C) 2008 by Daniel Stenberg et al
4  *
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.
14  */
15
16 #include "setup.h"
17 #include "ares.h"
18 #include "ares_private.h"
19
20 #if defined(WIN32) && !defined(MSDOS)
21
22 struct timeval ares__tvnow(void)
23 {
24   /*
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.
28   */
29   struct timeval now;
30   DWORD milliseconds = GetTickCount();
31   now.tv_sec = milliseconds / 1000;
32   now.tv_usec = (milliseconds % 1000) * 1000;
33   return now;
34 }
35
36 #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
37
38 struct timeval ares__tvnow(void)
39 {
40   /*
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.
46   */
47   struct timeval now;
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;
52   }
53   /*
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.
57   */
58 #ifdef HAVE_GETTIMEOFDAY
59   else
60     (void)gettimeofday(&now, NULL);
61 #else
62   else {
63     now.tv_sec = (long)time(NULL);
64     now.tv_usec = 0;
65   }
66 #endif
67   return now;
68 }
69
70 #elif defined(HAVE_GETTIMEOFDAY)
71
72 struct timeval ares__tvnow(void)
73 {
74   /*
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.
78   */
79   struct timeval now;
80   (void)gettimeofday(&now, NULL);
81   return now;
82 }
83
84 #else
85
86 struct timeval ares__tvnow(void)
87 {
88   /*
89   ** time() returns the value of time in seconds since the Epoch.
90   */
91   struct timeval now;
92   now.tv_sec = (long)time(NULL);
93   now.tv_usec = 0;
94   return now;
95 }
96
97 #endif
98
99 #if 0 /* Not used */
100 /*
101  * Make sure that the first argument is the more recent time, as otherwise
102  * we'll get a weird negative time-diff back...
103  *
104  * Returns: the time difference in number of milliseconds.
105  */
106 long ares__tvdiff(struct timeval newer, struct timeval older)
107 {
108   return (newer.tv_sec-older.tv_sec)*1000+
109     (newer.tv_usec-older.tv_usec)/1000;
110 }
111 #endif
112