2 * Copyright (C) 2004, 2005, 2007, 2009, 2010 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.
18 /* $Id: time.c,v 1.33.186.2 2010/04/21 23:50:05 tbox Exp $ */
25 #include <isc/string.h> /* Required for HP/UX (and others?) */
29 #include <isc/print.h>
30 #include <isc/region.h>
31 #include <isc/stdtime.h>
34 #include <dns/result.h>
37 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
40 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
42 char buf[sizeof("YYYYMMDDHHMMSS")];
49 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
50 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
51 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
54 while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
57 if (tm.tm_year + 1900 > 9999)
61 while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
81 /* yyyy mm dd HH MM SS */
82 snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
83 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
84 tm.tm_hour, tm.tm_min, tm.tm_sec);
86 isc_buffer_availableregion(target, ®ion);
89 if (l > region.length)
90 return (ISC_R_NOSPACE);
92 memcpy(region.base, buf, l);
93 isc_buffer_add(target, l);
94 return (ISC_R_SUCCESS);
98 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
105 * Adjust the time to the closest epoch. This should be changed
106 * to use a 64-bit counterpart to isc_stdtime_get() if one ever
107 * is defined, but even the current code is good until the year
110 isc_stdtime_get(&now);
111 start = (isc_int64_t) now;
114 while ((t = (base + value)) < start) {
118 return (dns_time64_totext(t, target));
122 dns_time64_fromtext(const char *source, isc_int64_t *target) {
123 int year, month, day, hour, minute, second;
128 #define RANGE(min, max, value) \
130 if (value < (min) || value > (max)) \
131 return (ISC_R_RANGE); \
134 if (strlen(source) != 14U)
135 return (DNS_R_SYNTAX);
137 * Confirm the source only consists digits. sscanf() allows some
140 for (i = 0; i < 14; i++) {
141 if (!isdigit((unsigned char)source[i]))
142 return (DNS_R_SYNTAX);
144 if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
145 &year, &month, &day, &hour, &minute, &second) != 6)
146 return (DNS_R_SYNTAX);
148 RANGE(1970, 9999, year);
150 RANGE(1, days[month - 1] +
151 ((month == 2 && is_leap(year)) ? 1 : 0), day);
153 RANGE(0, 59, minute);
154 RANGE(0, 60, second); /* 60 == leap second. */
157 * Calculate seconds since epoch.
159 value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
160 for (i = 0; i < (month - 1); i++)
161 value += days[i] * 86400;
162 if (is_leap(year) && month > 2)
164 for (i = 1970; i < year; i++) {
165 secs = (is_leap(i) ? 366 : 365) * 86400;
170 return (ISC_R_SUCCESS);
174 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
177 result = dns_time64_fromtext(source, &value64);
178 if (result != ISC_R_SUCCESS)
180 *target = (isc_uint32_t)value64;
182 return (ISC_R_SUCCESS);