Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / time.c
blob901763939178ef6cf0357fb01e4ee91ba62abe57
1 /*
2 * Copyright (C) 2004, 2005, 2007, 2009 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.31.128.2 2009/01/19 23:47:02 tbox Exp $ */
20 /*! \file */
22 #include <config.h>
24 #include <stdio.h>
25 #include <isc/string.h> /* Required for HP/UX (and others?) */
26 #include <time.h>
28 #include <isc/print.h>
29 #include <isc/region.h>
30 #include <isc/stdtime.h>
31 #include <isc/util.h>
33 #include <dns/result.h>
34 #include <dns/time.h>
36 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
38 isc_result_t
39 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
40 struct tm tm;
41 char buf[sizeof("YYYYMMDDHHMMSS")];
42 int secs;
43 unsigned int l;
44 isc_region_t region;
46 REQUIRE(t >= 0);
48 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
49 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
50 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
52 tm.tm_year = 70;
53 while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
54 t -= secs;
55 tm.tm_year++;
56 if (tm.tm_year + 1900 > 9999)
57 return (ISC_R_RANGE);
59 tm.tm_mon = 0;
60 while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
61 t -= secs;
62 tm.tm_mon++;
64 tm.tm_mday = 1;
65 while (86400 <= t) {
66 t -= 86400;
67 tm.tm_mday++;
69 tm.tm_hour = 0;
70 while (3600 <= t) {
71 t -= 3600;
72 tm.tm_hour++;
74 tm.tm_min = 0;
75 while (60 <= t) {
76 t -= 60;
77 tm.tm_min++;
79 tm.tm_sec = (int)t;
80 /* yyyy mm dd HH MM SS */
81 snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
82 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
83 tm.tm_hour, tm.tm_min, tm.tm_sec);
85 isc_buffer_availableregion(target, &region);
86 l = strlen(buf);
88 if (l > region.length)
89 return (ISC_R_NOSPACE);
91 memcpy(region.base, buf, l);
92 isc_buffer_add(target, l);
93 return (ISC_R_SUCCESS);
96 isc_result_t
97 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
98 isc_stdtime_t now;
99 isc_int64_t start;
100 isc_int64_t base;
101 isc_int64_t t;
104 * Adjust the time to the closest epoch. This should be changed
105 * to use a 64-bit counterpart to isc_stdtime_get() if one ever
106 * is defined, but even the current code is good until the year
107 * 2106.
109 isc_stdtime_get(&now);
110 start = (isc_int64_t) now;
111 start -= 0x7fffffff;
112 base = 0;
113 while ((t = (base + value)) < start) {
114 base += 0x80000000;
115 base += 0x80000000;
117 return (dns_time64_totext(t, target));
120 isc_result_t
121 dns_time64_fromtext(const char *source, isc_int64_t *target) {
122 int year, month, day, hour, minute, second;
123 isc_int64_t value;
124 int secs;
125 int i;
127 #define RANGE(min, max, value) \
128 do { \
129 if (value < (min) || value > (max)) \
130 return (ISC_R_RANGE); \
131 } while (0)
133 if (strlen(source) != 14U)
134 return (DNS_R_SYNTAX);
135 if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
136 &year, &month, &day, &hour, &minute, &second) != 6)
137 return (DNS_R_SYNTAX);
139 RANGE(1970, 9999, year);
140 RANGE(1, 12, month);
141 RANGE(1, days[month - 1] +
142 ((month == 2 && is_leap(year)) ? 1 : 0), day);
143 RANGE(0, 23, hour);
144 RANGE(0, 59, minute);
145 RANGE(0, 60, second); /* 60 == leap second. */
148 * Calculate seconds since epoch.
150 value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
151 for (i = 0; i < (month - 1); i++)
152 value += days[i] * 86400;
153 if (is_leap(year) && month > 2)
154 value += 86400;
155 for (i = 1970; i < year; i++) {
156 secs = (is_leap(i) ? 366 : 365) * 86400;
157 value += secs;
160 *target = value;
161 return (ISC_R_SUCCESS);
164 isc_result_t
165 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
166 isc_int64_t value64;
167 isc_result_t result;
168 result = dns_time64_fromtext(source, &value64);
169 if (result != ISC_R_SUCCESS)
170 return (result);
171 *target = (isc_uint32_t)value64;
173 return (ISC_R_SUCCESS);