diff options
-rw-r--r-- | usr.bin/dig/lib/dns/dns_time.c | 93 | ||||
-rw-r--r-- | usr.bin/dig/lib/dns/include/dns/time.h | 18 |
2 files changed, 24 insertions, 87 deletions
diff --git a/usr.bin/dig/lib/dns/dns_time.c b/usr.bin/dig/lib/dns/dns_time.c index b0a30cb5e57..9d564a411a2 100644 --- a/usr.bin/dig/lib/dns/dns_time.c +++ b/usr.bin/dig/lib/dns/dns_time.c @@ -14,12 +14,12 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: dns_time.c,v 1.6 2020/02/24 13:49:38 jsg Exp $ */ +/* $Id: dns_time.c,v 1.7 2020/04/02 16:57:45 florian Exp $ */ /*! \file */ #include <stdio.h> -#include <string.h> /* Required for HP/UX (and others?) */ +#include <string.h> #include <time.h> #include <isc/region.h> @@ -28,65 +28,18 @@ #include <dns/time.h> -static const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; - -isc_result_t -dns_time64_totext(int64_t t, isc_buffer_t *target) { - struct tm tm; - char buf[sizeof("!!!!!!YYYY!!!!!!!!MM!!!!!!!!DD!!!!!!!!HH!!!!!!!!MM!!!!!!!!SS")]; - int secs; - unsigned int l; +static isc_result_t +dns_time64_totext(time_t t, isc_buffer_t *target) { + struct tm *tm; + char buf[sizeof("YYYYMMDDHHMMSS")]; + size_t l; isc_region_t region; -/* - * Warning. Do NOT use arguments with side effects with these macros. - */ -#define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) -#define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400) -#define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400) - - tm.tm_year = 70; - while (t < 0) { - if (tm.tm_year == 0) - return (ISC_R_RANGE); - tm.tm_year--; - secs = year_secs(tm.tm_year + 1900); - t += secs; - } - while ((secs = year_secs(tm.tm_year + 1900)) <= t) { - t -= secs; - tm.tm_year++; - if (tm.tm_year + 1900 > 9999) - return (ISC_R_RANGE); - } - tm.tm_mon = 0; - while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) { - t -= secs; - tm.tm_mon++; - } - tm.tm_mday = 1; - while (86400 <= t) { - t -= 86400; - tm.tm_mday++; - } - tm.tm_hour = 0; - while (3600 <= t) { - t -= 3600; - tm.tm_hour++; - } - tm.tm_min = 0; - while (60 <= t) { - t -= 60; - tm.tm_min++; - } - tm.tm_sec = (int)t; - /* yyyy mm dd HH MM SS */ - snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec); + tm = gmtime(&t); + if ((l = strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", tm)) == 0) + return (ISC_R_NOSPACE); isc_buffer_availableregion(target, ®ion); - l = strlen(buf); if (l > region.length) return (ISC_R_NOSPACE); @@ -96,24 +49,20 @@ dns_time64_totext(int64_t t, isc_buffer_t *target) { return (ISC_R_SUCCESS); } -int64_t +static time_t dns_time64_from32(uint32_t value) { - time_t now; - int64_t start; - int64_t t; + uint32_t now32; + time_t start; + time_t t; + + time(&start); + now32 = (uint32_t) start; - /* - * Adjust the time to the closest epoch. This should be changed - * to use a 64-bit counterpart to time() if one ever - * is defined, but even the current code is good until the year - * 2106. - */ - time(&now); - start = (int64_t) now; - if (isc_serial_gt(value, now)) - t = start + (value - now); + /* Adjust the time to the closest epoch. */ + if (isc_serial_gt(value, now32)) + t = start + (value - now32); else - t = start - (now - value); + t = start - (now32 - value); return (t); } diff --git a/usr.bin/dig/lib/dns/include/dns/time.h b/usr.bin/dig/lib/dns/include/dns/time.h index c44ae79db54..da845705f2e 100644 --- a/usr.bin/dig/lib/dns/include/dns/time.h +++ b/usr.bin/dig/lib/dns/include/dns/time.h @@ -14,7 +14,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: time.h,v 1.3 2020/02/23 19:54:26 jung Exp $ */ +/* $Id: time.h,v 1.4 2020/04/02 16:57:45 florian Exp $ */ #ifndef DNS_TIME_H #define DNS_TIME_H 1 @@ -32,25 +32,13 @@ ***/ isc_result_t -dns_time64_totext(int64_t value, isc_buffer_t *target); -/*%< - * Convert a 64-bit count of seconds since Jan 1 1970 0:00 GMT into - * a YYYYMMDDHHMMSS text representation and append it to 'target'. - */ - -isc_result_t dns_time32_totext(uint32_t value, isc_buffer_t *target); /*%< - * Like dns_time64_totext, but for a 32-bit cyclic time value. + * Convert 32-bit cyclic time value into a YYYYMMDDHHMMSS text representation + * and append it to 'target'. * Of those dates whose counts of seconds since Jan 1 1970 0:00 GMT * are congruent with 'value' modulo 2^32, the one closest to the * current date is chosen. */ -int64_t -dns_time64_from32(uint32_t value); -/*%< - * Covert a 32-bit cyclic time value into a 64 bit time stamp. - */ - #endif /* DNS_TIME_H */ |