2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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: time.c,v 1.18.2.4.2.8 2004/08/28 06:25:20 marka Exp $ */
23 #include <isc/string.h> /* Required for HP/UX (and others?) */
26 #include <isc/print.h>
27 #include <isc/region.h>
28 #include <isc/stdtime.h>
31 #include <dns/result.h>
34 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
37 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
39 char buf[sizeof("YYYYMMDDHHMMSS")];
46 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
47 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
48 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
51 while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
54 if (tm.tm_year + 1900 > 9999)
58 while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
78 /* yyyy mm dd HH MM SS */
79 snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
80 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
81 tm.tm_hour, tm.tm_min, tm.tm_sec);
83 isc_buffer_availableregion(target, ®ion);
86 if (l > region.length)
87 return (ISC_R_NOSPACE);
89 memcpy(region.base, buf, l);
90 isc_buffer_add(target, l);
91 return (ISC_R_SUCCESS);
95 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
102 * Adjust the time to the closest epoch. This should be changed
103 * to use a 64-bit counterpart to isc_stdtime_get() if one ever
104 * is defined, but even the current code is good until the year
107 isc_stdtime_get(&now);
108 start = (isc_int64_t) now;
111 while ((t = (base + value)) < start) {
115 return (dns_time64_totext(t, target));
119 dns_time64_fromtext(const char *source, isc_int64_t *target) {
120 int year, month, day, hour, minute, second;
125 #define RANGE(min, max, value) \
127 if (value < (min) || value > (max)) \
128 return (ISC_R_RANGE); \
131 if (strlen(source) != 14U)
132 return (DNS_R_SYNTAX);
133 if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
134 &year, &month, &day, &hour, &minute, &second) != 6)
135 return (DNS_R_SYNTAX);
137 RANGE(1970, 9999, year);
139 RANGE(1, days[month - 1] +
140 ((month == 2 && is_leap(year)) ? 1 : 0), day);
142 RANGE(0, 59, minute);
143 RANGE(0, 60, second); /* 60 == leap second. */
146 * Calulate seconds since epoch.
148 value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
149 for (i = 0; i < (month - 1); i++)
150 value += days[i] * 86400;
151 if (is_leap(year) && month > 2)
153 for (i = 1970; i < year; i++) {
154 secs = (is_leap(i) ? 366 : 365) * 86400;
159 return (ISC_R_SUCCESS);
163 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
166 result = dns_time64_fromtext(source, &value64);
167 if (result != ISC_R_SUCCESS)
169 *target = (isc_uint32_t)value64;
171 return (ISC_R_SUCCESS);