Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / isc / unix / stdtime.c
blobc5d0c47df1fccacc9add2d03327073407138be2f
1 /*
2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.19 2007/06/19 23:47:18 tbox Exp $ */
20 /*! \file */
22 #include <config.h>
24 #include <stddef.h> /* NULL */
25 #include <stdlib.h> /* NULL */
26 #include <syslog.h>
28 #include <sys/time.h>
30 #include <isc/stdtime.h>
31 #include <isc/util.h>
33 #ifndef ISC_FIX_TV_USEC
34 #define ISC_FIX_TV_USEC 1
35 #endif
37 #define US_PER_S 1000000
39 #if ISC_FIX_TV_USEC
40 static inline void
41 fix_tv_usec(struct timeval *tv) {
42 isc_boolean_t fixed = ISC_FALSE;
44 if (tv->tv_usec < 0) {
45 fixed = ISC_TRUE;
46 do {
47 tv->tv_sec -= 1;
48 tv->tv_usec += US_PER_S;
49 } while (tv->tv_usec < 0);
50 } else if (tv->tv_usec >= US_PER_S) {
51 fixed = ISC_TRUE;
52 do {
53 tv->tv_sec += 1;
54 tv->tv_usec -= US_PER_S;
55 } while (tv->tv_usec >=US_PER_S);
58 * Call syslog directly as we are called from the logging functions.
60 if (fixed)
61 (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
63 #endif
65 void
66 isc_stdtime_get(isc_stdtime_t *t) {
67 struct timeval tv;
70 * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
71 * 1970.
74 REQUIRE(t != NULL);
76 RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
78 #if ISC_FIX_TV_USEC
79 fix_tv_usec(&tv);
80 INSIST(tv.tv_usec >= 0);
81 #else
82 INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
83 #endif
85 *t = (unsigned int)tv.tv_sec;