2 * Copyright (C) 2004, 2005, 2007, 2009-2012, 2014 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 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.
25 #include <isc/string.h> /* Required for HP/UX (and others?) */
29 #include <isc/print.h>
30 #include <isc/region.h>
31 #include <isc/serial.h>
32 #include <isc/stdtime.h>
35 #include <dns/result.h>
38 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
41 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
43 char buf[sizeof("YYYYMMDDHHMMSS")];
49 * Warning. Do NOT use arguments with side effects with these macros.
51 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
52 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
53 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
60 secs = year_secs(tm.tm_year + 1900);
63 while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
66 if (tm.tm_year + 1900 > 9999)
70 while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
90 /* yyyy mm dd HH MM SS */
91 snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
92 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
93 tm.tm_hour, tm.tm_min, tm.tm_sec);
95 isc_buffer_availableregion(target, ®ion);
98 if (l > region.length)
99 return (ISC_R_NOSPACE);
101 memmove(region.base, buf, l);
102 isc_buffer_add(target, l);
103 return (ISC_R_SUCCESS);
107 dns_time64_from32(isc_uint32_t value) {
113 * Adjust the time to the closest epoch. This should be changed
114 * to use a 64-bit counterpart to isc_stdtime_get() if one ever
115 * is defined, but even the current code is good until the year
118 isc_stdtime_get(&now);
119 start = (isc_int64_t) now;
120 if (isc_serial_gt(value, now))
121 t = start + (value - now);
123 t = start - (now - value);
129 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
130 return (dns_time64_totext(dns_time64_from32(value), target));
134 dns_time64_fromtext(const char *source, isc_int64_t *target) {
135 int year, month, day, hour, minute, second;
140 #define RANGE(min, max, value) \
142 if (value < (min) || value > (max)) \
143 return (ISC_R_RANGE); \
146 if (strlen(source) != 14U)
147 return (DNS_R_SYNTAX);
149 * Confirm the source only consists digits. sscanf() allows some
152 for (i = 0; i < 14; i++) {
153 if (!isdigit((unsigned char)source[i]))
154 return (DNS_R_SYNTAX);
156 if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
157 &year, &month, &day, &hour, &minute, &second) != 6)
158 return (DNS_R_SYNTAX);
160 RANGE(0, 9999, year);
162 RANGE(1, days[month - 1] +
163 ((month == 2 && is_leap(year)) ? 1 : 0), day);
165 RANGE(0, 59, minute);
166 RANGE(0, 60, second); /* 60 == leap second. */
169 * Calculate seconds from epoch.
170 * Note: this uses a idealized calendar.
172 value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
173 for (i = 0; i < (month - 1); i++)
174 value += days[i] * 86400;
175 if (is_leap(year) && month > 2)
178 for (i = 1969; i >= year; i--) {
179 secs = (is_leap(i) ? 366 : 365) * 86400;
183 for (i = 1970; i < year; i++) {
184 secs = (is_leap(i) ? 366 : 365) * 86400;
190 return (ISC_R_SUCCESS);
194 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
197 result = dns_time64_fromtext(source, &value64);
198 if (result != ISC_R_SUCCESS)
200 *target = (isc_uint32_t)value64;
202 return (ISC_R_SUCCESS);