Merge branch 'vendor/LIBPCAP' (early part)
[dragonfly.git] / contrib / bind-9.3 / lib / isc / unix / time.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001, 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: time.c,v 1.34.2.6.2.4 2004/03/06 08:15:03 marka Exp $ */
19
20 #include <config.h>
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <syslog.h>
25 #include <time.h>
26
27 #include <sys/time.h>   /* Required for struct timeval on some platforms. */
28
29 #include <isc/log.h>
30 #include <isc/print.h>
31 #include <isc/strerror.h>
32 #include <isc/string.h>
33 #include <isc/time.h>
34 #include <isc/util.h>
35
36 #define NS_PER_S        1000000000      /* Nanoseconds per second. */
37 #define NS_PER_US       1000            /* Nanoseconds per microsecond. */
38 #define US_PER_S        1000000         /* Microseconds per second. */
39
40 /*
41  * All of the INSIST()s checks of nanoseconds < NS_PER_S are for
42  * consistency checking of the type. In lieu of magic numbers, it
43  * is the best we've got.  The check is only performed on functions which
44  * need an initialized type.
45  */
46
47 #ifndef ISC_FIX_TV_USEC
48 #define ISC_FIX_TV_USEC 1
49 #endif
50
51 /***
52  *** Intervals
53  ***/
54
55 static isc_interval_t zero_interval = { 0, 0 };
56 isc_interval_t *isc_interval_zero = &zero_interval;
57
58 #if ISC_FIX_TV_USEC
59 static inline void
60 fix_tv_usec(struct timeval *tv) {
61         isc_boolean_t fixed = ISC_FALSE;
62
63         if (tv->tv_usec < 0) {
64                 fixed = ISC_TRUE;
65                 do {
66                         tv->tv_sec -= 1;
67                         tv->tv_usec += US_PER_S;
68                 } while (tv->tv_usec < 0);
69         } else if (tv->tv_usec >= US_PER_S) {
70                 fixed = ISC_TRUE;
71                 do {
72                         tv->tv_sec += 1;
73                         tv->tv_usec -= US_PER_S;
74                 } while (tv->tv_usec >=US_PER_S);
75         }
76         /*
77          * Call syslog directly as was are called from the logging functions.
78          */
79         if (fixed)
80                 (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
81 }
82 #endif
83
84 void
85 isc_interval_set(isc_interval_t *i,
86                  unsigned int seconds, unsigned int nanoseconds)
87 {
88         REQUIRE(i != NULL);
89         REQUIRE(nanoseconds < NS_PER_S);
90
91         i->seconds = seconds;
92         i->nanoseconds = nanoseconds;
93 }
94
95 isc_boolean_t
96 isc_interval_iszero(const isc_interval_t *i) {
97         REQUIRE(i != NULL);
98         INSIST(i->nanoseconds < NS_PER_S);
99
100         if (i->seconds == 0 && i->nanoseconds == 0)
101                 return (ISC_TRUE);
102
103         return (ISC_FALSE);
104 }
105
106
107 /***
108  *** Absolute Times
109  ***/
110
111 static isc_time_t epoch = { 0, 0 };
112 isc_time_t *isc_time_epoch = &epoch;
113
114 void
115 isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
116         REQUIRE(t != NULL);
117         REQUIRE(nanoseconds < NS_PER_S);
118
119         t->seconds = seconds;
120         t->nanoseconds = nanoseconds;
121 }
122
123 void
124 isc_time_settoepoch(isc_time_t *t) {
125         REQUIRE(t != NULL);
126
127         t->seconds = 0;
128         t->nanoseconds = 0;
129 }
130
131 isc_boolean_t
132 isc_time_isepoch(const isc_time_t *t) {
133         REQUIRE(t != NULL);
134         INSIST(t->nanoseconds < NS_PER_S);
135
136         if (t->seconds == 0 && t->nanoseconds == 0)
137                 return (ISC_TRUE);
138
139         return (ISC_FALSE);
140 }
141
142
143 isc_result_t
144 isc_time_now(isc_time_t *t) {
145         struct timeval tv;
146         char strbuf[ISC_STRERRORSIZE];
147
148         REQUIRE(t != NULL);
149
150         if (gettimeofday(&tv, NULL) == -1) {
151                 isc__strerror(errno, strbuf, sizeof(strbuf));
152                 UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
153                 return (ISC_R_UNEXPECTED);
154         }
155
156         /*
157          * Does POSIX guarantee the signedness of tv_sec and tv_usec?  If not,
158          * then this test will generate warnings for platforms on which it is
159          * unsigned.  In any event, the chances of any of these problems
160          * happening are pretty much zero, but since the libisc library ensures
161          * certain things to be true ...
162          */
163 #if ISC_FIX_TV_USEC
164         fix_tv_usec(&tv);
165         if (tv.tv_sec < 0)
166                 return (ISC_R_UNEXPECTED);
167 #else
168         if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
169                 return (ISC_R_UNEXPECTED);
170 #endif
171
172         /*
173          * Ensure the tv_sec value fits in t->seconds.
174          */
175         if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
176             ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
177                 return (ISC_R_RANGE);
178
179         t->seconds = tv.tv_sec;
180         t->nanoseconds = tv.tv_usec * NS_PER_US;
181
182         return (ISC_R_SUCCESS);
183 }
184
185 isc_result_t
186 isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
187         struct timeval tv;
188         char strbuf[ISC_STRERRORSIZE];
189
190         REQUIRE(t != NULL);
191         REQUIRE(i != NULL);
192         INSIST(i->nanoseconds < NS_PER_S);
193
194         if (gettimeofday(&tv, NULL) == -1) {
195                 isc__strerror(errno, strbuf, sizeof(strbuf));
196                 UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
197                 return (ISC_R_UNEXPECTED);
198         }
199
200         /*
201          * Does POSIX guarantee the signedness of tv_sec and tv_usec?  If not,
202          * then this test will generate warnings for platforms on which it is
203          * unsigned.  In any event, the chances of any of these problems
204          * happening are pretty much zero, but since the libisc library ensures
205          * certain things to be true ...
206          */
207 #if ISC_FIX_TV_USEC
208         fix_tv_usec(&tv);
209         if (tv.tv_sec < 0)
210                 return (ISC_R_UNEXPECTED);
211 #else
212         if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
213                 return (ISC_R_UNEXPECTED);
214 #endif
215
216         /*
217          * Ensure the resulting seconds value fits in the size of an
218          * unsigned int.  (It is written this way as a slight optimization;
219          * note that even if both values == INT_MAX, then when added
220          * and getting another 1 added below the result is UINT_MAX.)
221          */
222         if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
223             ((long long)tv.tv_sec + i->seconds > UINT_MAX))
224                 return (ISC_R_RANGE);
225
226         t->seconds = tv.tv_sec + i->seconds;
227         t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
228         if (t->nanoseconds > NS_PER_S) {
229                 t->seconds++;
230                 t->nanoseconds -= NS_PER_S;
231         }
232
233         return (ISC_R_SUCCESS);
234 }
235
236 int
237 isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
238         REQUIRE(t1 != NULL && t2 != NULL);
239         INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
240
241         if (t1->seconds < t2->seconds)
242                 return (-1);
243         if (t1->seconds > t2->seconds)
244                 return (1);
245         if (t1->nanoseconds < t2->nanoseconds)
246                 return (-1);
247         if (t1->nanoseconds > t2->nanoseconds)
248                 return (1);
249         return (0);
250 }
251
252 isc_result_t
253 isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
254 {
255         REQUIRE(t != NULL && i != NULL && result != NULL);
256         INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
257
258         /*
259          * Ensure the resulting seconds value fits in the size of an
260          * unsigned int.  (It is written this way as a slight optimization;
261          * note that even if both values == INT_MAX, then when added
262          * and getting another 1 added below the result is UINT_MAX.)
263          */
264         if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
265             ((long long)t->seconds + i->seconds > UINT_MAX))
266                 return (ISC_R_RANGE);
267
268         result->seconds = t->seconds + i->seconds;
269         result->nanoseconds = t->nanoseconds + i->nanoseconds;
270         if (result->nanoseconds >= NS_PER_S) {
271                 result->seconds++;
272                 result->nanoseconds -= NS_PER_S;
273         }
274
275         return (ISC_R_SUCCESS);
276 }
277
278 isc_result_t
279 isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
280                   isc_time_t *result)
281 {
282         REQUIRE(t != NULL && i != NULL && result != NULL);
283         INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
284
285         if ((unsigned int)t->seconds < i->seconds ||
286             ((unsigned int)t->seconds == i->seconds &&
287              t->nanoseconds < i->nanoseconds))
288             return (ISC_R_RANGE);
289
290         result->seconds = t->seconds - i->seconds;
291         if (t->nanoseconds >= i->nanoseconds)
292                 result->nanoseconds = t->nanoseconds - i->nanoseconds;
293         else {
294                 result->nanoseconds = NS_PER_S - i->nanoseconds +
295                         t->nanoseconds;
296                 result->seconds--;
297         }
298
299         return (ISC_R_SUCCESS);
300 }
301
302 isc_uint64_t
303 isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
304         isc_uint64_t i1, i2, i3;
305
306         REQUIRE(t1 != NULL && t2 != NULL);
307         INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
308
309         i1 = (isc_uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
310         i2 = (isc_uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
311
312         if (i1 <= i2)
313                 return (0);
314
315         i3 = i1 - i2;
316
317         /*
318          * Convert to microseconds.
319          */
320         i3 = (i1 - i2) / NS_PER_US;
321
322         return (i3);
323 }
324
325 isc_uint32_t
326 isc_time_seconds(const isc_time_t *t) {
327         REQUIRE(t != NULL);
328         INSIST(t->nanoseconds < NS_PER_S);
329
330         return ((isc_uint32_t)t->seconds);
331 }
332
333 isc_result_t
334 isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
335         isc_uint64_t i;
336         time_t seconds;
337
338         REQUIRE(t != NULL);
339         INSIST(t->nanoseconds < NS_PER_S);
340
341         /*
342          * Ensure that the number of seconds represented by t->seconds
343          * can be represented by a time_t.  Since t->seconds is an unsigned
344          * int and since time_t is mostly opaque, this is trickier than
345          * it seems.  (This standardized opaqueness of time_t is *very*
346          * frustrating; time_t is not even limited to being an integral
347          * type.)
348          *
349          * The mission, then, is to avoid generating any kind of warning
350          * about "signed versus unsigned" while trying to determine if the
351          * the unsigned int t->seconds is out range for tv_sec, which is
352          * pretty much only true if time_t is a signed integer of the same
353          * size as the return value of isc_time_seconds.
354          *
355          * The use of the 64 bit integer ``i'' takes advantage of C's
356          * conversion rules to either zero fill or sign extend the widened
357          * type.
358          *
359          * Solaris 5.6 gives this warning about the left shift:
360          *      warning: integer overflow detected: op "<<"
361          * if the U(nsigned) qualifier is not on the 1.
362          */
363         seconds = (time_t)t->seconds;
364
365         INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
366         INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
367
368         if (sizeof(time_t) == sizeof(isc_uint32_t) &&          /* Same size. */
369             (time_t)0.5 != 0.5 &&              /* Not a floating point type. */
370             (i = (time_t)-1) != 4294967295u &&                 /* Is signed. */
371             (seconds &
372              (1U << (sizeof(time_t) * CHAR_BIT - 1))) != 0U) {   /* Negative. */
373                 /*
374                  * This UNUSED() is here to shut up the IRIX compiler:
375                  *      variable "i" was set but never used
376                  * when the value of i *was* used in the third test.
377                  * (Let's hope the compiler got the actual test right.)
378                  */
379                 UNUSED(i);
380                 return (ISC_R_RANGE);
381         }
382
383         *secondsp = seconds;
384
385         return (ISC_R_SUCCESS);
386 }
387
388 isc_uint32_t
389 isc_time_nanoseconds(const isc_time_t *t) {
390         REQUIRE(t != NULL);
391
392         ENSURE(t->nanoseconds < NS_PER_S);
393
394         return ((isc_uint32_t)t->nanoseconds);
395 }
396
397 void
398 isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
399         time_t now;
400         unsigned int flen;
401
402         REQUIRE(len > 0);
403
404         now = (time_t) t->seconds;
405         flen = strftime(buf, len, "%d-%b-%Y %X", localtime(&now));
406         INSIST(flen < len);
407         if (flen != 0)
408                 snprintf(buf + flen, len - flen,
409                          ".%03u", t->nanoseconds / 1000000);
410         else
411                 snprintf(buf, len, "99-Bad-9999 99:99:99.999");
412 }