stdtime.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1999-2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: stdtime.c,v 1.19 2007/06/19 23:47:18 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <stddef.h>     /* NULL */
00025 #include <stdlib.h>     /* NULL */
00026 #include <syslog.h>
00027 
00028 #include <sys/time.h>
00029 
00030 #include <isc/stdtime.h>
00031 #include <isc/util.h>
00032 
00033 #ifndef ISC_FIX_TV_USEC
00034 #define ISC_FIX_TV_USEC 1
00035 #endif
00036 
00037 #define US_PER_S 1000000
00038 
00039 #if ISC_FIX_TV_USEC
00040 static inline void
00041 fix_tv_usec(struct timeval *tv) {
00042         isc_boolean_t fixed = ISC_FALSE;
00043 
00044         if (tv->tv_usec < 0) {
00045                 fixed = ISC_TRUE;
00046                 do {
00047                         tv->tv_sec -= 1;
00048                         tv->tv_usec += US_PER_S;
00049                 } while (tv->tv_usec < 0);
00050         } else if (tv->tv_usec >= US_PER_S) {
00051                 fixed = ISC_TRUE;
00052                 do {
00053                         tv->tv_sec += 1;
00054                         tv->tv_usec -= US_PER_S;
00055                 } while (tv->tv_usec >=US_PER_S);
00056         }
00057         /*
00058          * Call syslog directly as we are called from the logging functions.
00059          */
00060         if (fixed)
00061                 (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
00062 }
00063 #endif
00064 
00065 void
00066 isc_stdtime_get(isc_stdtime_t *t) {
00067         struct timeval tv;
00068 
00069         /*
00070          * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
00071          * 1970.
00072          */
00073 
00074         REQUIRE(t != NULL);
00075 
00076         RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
00077 
00078 #if ISC_FIX_TV_USEC
00079         fix_tv_usec(&tv);
00080         INSIST(tv.tv_usec >= 0);
00081 #else
00082         INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
00083 #endif
00084 
00085         *t = (unsigned int)tv.tv_sec;
00086 }

Generated on Tue Apr 28 17:41:06 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha