00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #include <config.h>
00023 
00024 #include <stddef.h>     
00025 #include <stdlib.h>     
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 
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 
00071 
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 }