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