Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / unix / stdtime.c
blobb9806b3d4570e7de713caace5f733c0114c7d7c2
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
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.
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.
18 /* $Id: stdtime.c,v 1.11.2.2 2004/03/09 06:12:12 marka Exp $ */
20 #include <config.h>
22 #include <stdlib.h> /* NULL */
23 #include <syslog.h>
25 #include <sys/time.h>
27 #include <isc/stdtime.h>
28 #include <isc/util.h>
30 #ifndef ISC_FIX_TV_USEC
31 #define ISC_FIX_TV_USEC 1
32 #endif
34 #define US_PER_S 1000000
36 #if ISC_FIX_TV_USEC
37 static inline void
38 fix_tv_usec(struct timeval *tv) {
39 isc_boolean_t fixed = ISC_FALSE;
41 if (tv->tv_usec < 0) {
42 fixed = ISC_TRUE;
43 do {
44 tv->tv_sec -= 1;
45 tv->tv_usec += US_PER_S;
46 } while (tv->tv_usec < 0);
47 } else if (tv->tv_usec >= US_PER_S) {
48 fixed = ISC_TRUE;
49 do {
50 tv->tv_sec += 1;
51 tv->tv_usec -= US_PER_S;
52 } while (tv->tv_usec >=US_PER_S);
55 * Call syslog directly as we are called from the logging functions.
57 if (fixed)
58 syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
60 #endif
62 void
63 isc_stdtime_get(isc_stdtime_t *t) {
64 struct timeval tv;
67 * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
68 * 1970.
71 REQUIRE(t != NULL);
73 RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
75 #if ISC_FIX_TV_USEC
76 fix_tv_usec(&tv);
77 INSIST(tv.tv_usec >= 0);
78 #else
79 INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
80 #endif
82 *t = (unsigned int)tv.tv_sec;