2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001, 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.5 2004/03/09 06:11:08 marka Exp $ */
23 #include <isc/string.h> /* Required for HP/UX (and others?) */
26 #include <isc/region.h>
27 #include <isc/stdtime.h>
30 #include <dns/result.h>
33 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
36 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
38 char buf[sizeof "YYYYMMDDHHMMSS"];
45 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
46 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
47 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
50 while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
53 if (tm.tm_year + 1900 > 9999)
57 while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
77 /* yy mm dd HH MM SS */
78 sprintf(buf, "%04d%02d%02d%02d%02d%02d",
79 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
80 tm.tm_hour, tm.tm_min, tm.tm_sec);
82 isc_buffer_availableregion(target, ®ion);
85 if (l > region.length)
86 return (ISC_R_NOSPACE);
88 memcpy(region.base, buf, l);
89 isc_buffer_add(target, l);
90 return (ISC_R_SUCCESS);
94 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
101 * Adjust the time to the closest epoch. This should be changed
102 * to use a 64-bit counterpart to isc_stdtime_get() if one ever
103 * is defined, but even the current code is good until the year
106 isc_stdtime_get(&now);
107 start = (isc_int64_t) now;
110 while ((t = (base + value)) < start) {
114 return (dns_time64_totext(t, target));
118 dns_time64_fromtext(char *source, isc_int64_t *target) {
119 int year, month, day, hour, minute, second;
124 #define RANGE(min, max, value) \
126 if (value < (min) || value > (max)) \
127 return (ISC_R_RANGE); \
130 if (strlen(source) != 14U)
131 return (DNS_R_SYNTAX);
132 if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
133 &year, &month, &day, &hour, &minute, &second) != 6)
134 return (DNS_R_SYNTAX);
136 RANGE(1970, 9999, year);
138 RANGE(1, days[month - 1] +
139 ((month == 2 && is_leap(year)) ? 1 : 0), day);
141 RANGE(0, 59, minute);
142 RANGE(0, 60, second); /* 60 == leap second. */
145 * Calulate seconds since epoch.
147 value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
148 for (i = 0; i < (month - 1) ; i++)
149 value += days[i] * 86400;
150 if (is_leap(year) && month > 2)
152 for (i = 1970; i < year; i++) {
153 secs = (is_leap(i) ? 366 : 365) * 86400;
158 return (ISC_R_SUCCESS);
162 dns_time32_fromtext(char *source, isc_uint32_t *target) {
165 result = dns_time64_fromtext(source, &value64);
166 if (result != ISC_R_SUCCESS)
168 *target = (isc_uint32_t)value64;
170 return (ISC_R_SUCCESS);