Merge from vendor branch TCSH:
[dragonfly.git] / contrib / bind-9.3 / lib / dns / time.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: time.c,v 1.18.2.4.2.8 2004/08/28 06:25:20 marka Exp $ */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <isc/string.h>         /* Required for HP/UX (and others?) */
24 #include <time.h>
25
26 #include <isc/print.h>
27 #include <isc/region.h>
28 #include <isc/stdtime.h>
29 #include <isc/util.h>
30
31 #include <dns/result.h>
32 #include <dns/time.h>
33
34 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
35
36 isc_result_t
37 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
38         struct tm tm;
39         char buf[sizeof("YYYYMMDDHHMMSS")];
40         int secs;
41         unsigned int l;
42         isc_region_t region;
43
44         REQUIRE(t >= 0);
45
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)
49
50         tm.tm_year = 70;
51         while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
52                 t -= secs;
53                 tm.tm_year++;
54                 if (tm.tm_year + 1900 > 9999)
55                         return (ISC_R_RANGE);
56         }
57         tm.tm_mon = 0;
58         while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
59                 t -= secs;
60                 tm.tm_mon++;
61         }
62         tm.tm_mday = 1;
63         while (86400 <= t) {
64                 t -= 86400;
65                 tm.tm_mday++;
66         }
67         tm.tm_hour = 0;
68         while (3600 <= t) {
69                 t -= 3600;
70                 tm.tm_hour++;
71         }
72         tm.tm_min = 0;
73         while (60 <= t) {
74                 t -= 60;
75                 tm.tm_min++;
76         }
77         tm.tm_sec = (int)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);
82
83         isc_buffer_availableregion(target, &region);
84         l = strlen(buf);
85
86         if (l > region.length)
87                 return (ISC_R_NOSPACE);
88
89         memcpy(region.base, buf, l);
90         isc_buffer_add(target, l);
91         return (ISC_R_SUCCESS);
92 }
93
94 isc_result_t
95 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
96         isc_stdtime_t now;
97         isc_int64_t start;
98         isc_int64_t base;
99         isc_int64_t t;
100
101         /*
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
105          * 2106.
106          */
107         isc_stdtime_get(&now);
108         start = (isc_int64_t) now;
109         start -= 0x7fffffff;
110         base = 0;
111         while ((t = (base + value)) < start) {
112                 base += 0x80000000;
113                 base += 0x80000000;
114         }
115         return (dns_time64_totext(t, target));
116 }
117
118 isc_result_t
119 dns_time64_fromtext(const char *source, isc_int64_t *target) {
120         int year, month, day, hour, minute, second;
121         isc_int64_t value;
122         int secs;
123         int i;
124
125 #define RANGE(min, max, value) \
126         do { \
127                 if (value < (min) || value > (max)) \
128                         return (ISC_R_RANGE); \
129         } while (0)
130
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);
136
137         RANGE(1970, 9999, year);
138         RANGE(1, 12, month);
139         RANGE(1, days[month - 1] +
140                  ((month == 2 && is_leap(year)) ? 1 : 0), day);
141         RANGE(0, 23, hour);
142         RANGE(0, 59, minute);
143         RANGE(0, 60, second);           /* 60 == leap second. */
144
145         /*
146          * Calulate seconds since epoch.
147          */
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)
152                 value += 86400;
153         for (i = 1970; i < year; i++) {
154                 secs = (is_leap(i) ? 366 : 365) * 86400;
155                 value += secs;
156         }
157
158         *target = value;
159         return (ISC_R_SUCCESS);
160 }
161
162 isc_result_t
163 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
164         isc_int64_t value64;
165         isc_result_t result;
166         result = dns_time64_fromtext(source, &value64);
167         if (result != ISC_R_SUCCESS)
168                 return (result);
169         *target = (isc_uint32_t)value64;
170
171         return (ISC_R_SUCCESS);
172 }