]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/bind9/lib/dns/time.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / bind9 / lib / dns / time.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009-2011  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2003  Internet Software Consortium.
4  *
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.
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.35.132.2 2011-03-09 23:46:55 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <isc/string.h>         /* Required for HP/UX (and others?) */
26 #include <time.h>
27 #include <ctype.h>
28
29 #include <isc/print.h>
30 #include <isc/region.h>
31 #include <isc/serial.h>
32 #include <isc/stdtime.h>
33 #include <isc/util.h>
34
35 #include <dns/result.h>
36 #include <dns/time.h>
37
38 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
39
40 isc_result_t
41 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
42         struct tm tm;
43         char buf[sizeof("YYYYMMDDHHMMSS")];
44         int secs;
45         unsigned int l;
46         isc_region_t region;
47
48 /*
49  * Warning. Do NOT use arguments with side effects with these macros.
50  */
51 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
52 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
53 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
54
55         tm.tm_year = 70;
56         while (t < 0) {
57                 if (tm.tm_year == 0)
58                         return (ISC_R_RANGE);
59                 tm.tm_year--;
60                 secs = year_secs(tm.tm_year + 1900);
61                 t += secs;
62         }
63         while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
64                 t -= secs;
65                 tm.tm_year++;
66                 if (tm.tm_year + 1900 > 9999)
67                         return (ISC_R_RANGE);
68         }
69         tm.tm_mon = 0;
70         while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
71                 t -= secs;
72                 tm.tm_mon++;
73         }
74         tm.tm_mday = 1;
75         while (86400 <= t) {
76                 t -= 86400;
77                 tm.tm_mday++;
78         }
79         tm.tm_hour = 0;
80         while (3600 <= t) {
81                 t -= 3600;
82                 tm.tm_hour++;
83         }
84         tm.tm_min = 0;
85         while (60 <= t) {
86                 t -= 60;
87                 tm.tm_min++;
88         }
89         tm.tm_sec = (int)t;
90                                  /* yyyy  mm  dd  HH  MM  SS */
91         snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
92                  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
93                  tm.tm_hour, tm.tm_min, tm.tm_sec);
94
95         isc_buffer_availableregion(target, &region);
96         l = strlen(buf);
97
98         if (l > region.length)
99                 return (ISC_R_NOSPACE);
100
101         memcpy(region.base, buf, l);
102         isc_buffer_add(target, l);
103         return (ISC_R_SUCCESS);
104 }
105
106 isc_result_t
107 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
108         isc_stdtime_t now;
109         isc_int64_t start;
110         isc_int64_t t;
111
112         /*
113          * Adjust the time to the closest epoch.  This should be changed
114          * to use a 64-bit counterpart to isc_stdtime_get() if one ever
115          * is defined, but even the current code is good until the year
116          * 2106.
117          */
118         isc_stdtime_get(&now);
119         start = (isc_int64_t) now;
120         if (isc_serial_gt(value, now))
121                 t = start + (value - now);
122         else
123                 t = start - (now - value);
124         return (dns_time64_totext(t, target));
125 }
126
127 isc_result_t
128 dns_time64_fromtext(const char *source, isc_int64_t *target) {
129         int year, month, day, hour, minute, second;
130         isc_int64_t value;
131         int secs;
132         int i;
133
134 #define RANGE(min, max, value) \
135         do { \
136                 if (value < (min) || value > (max)) \
137                         return (ISC_R_RANGE); \
138         } while (0)
139
140         if (strlen(source) != 14U)
141                 return (DNS_R_SYNTAX);
142         /*
143          * Confirm the source only consists digits.  sscanf() allows some
144          * minor exceptions.
145          */
146         for (i = 0; i < 14; i++) {
147                 if (!isdigit((unsigned char)source[i]))
148                         return (DNS_R_SYNTAX);
149         }
150         if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
151                    &year, &month, &day, &hour, &minute, &second) != 6)
152                 return (DNS_R_SYNTAX);
153
154         RANGE(0, 9999, year);
155         RANGE(1, 12, month);
156         RANGE(1, days[month - 1] +
157                  ((month == 2 && is_leap(year)) ? 1 : 0), day);
158         RANGE(0, 23, hour);
159         RANGE(0, 59, minute);
160         RANGE(0, 60, second);           /* 60 == leap second. */
161
162         /*
163          * Calculate seconds from epoch.
164          * Note: this uses a idealized calendar.
165          */
166         value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
167         for (i = 0; i < (month - 1); i++)
168                 value += days[i] * 86400;
169         if (is_leap(year) && month > 2)
170                 value += 86400;
171         if (year < 1970) {
172                 for (i = 1969; i >= year; i--) {
173                         secs = (is_leap(i) ? 366 : 365) * 86400;
174                         value -= secs;
175                 }
176         } else {
177                 for (i = 1970; i < year; i++) {
178                         secs = (is_leap(i) ? 366 : 365) * 86400;
179                         value += secs;
180                 }
181         }
182
183         *target = value;
184         return (ISC_R_SUCCESS);
185 }
186
187 isc_result_t
188 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
189         isc_int64_t value64;
190         isc_result_t result;
191         result = dns_time64_fromtext(source, &value64);
192         if (result != ISC_R_SUCCESS)
193                 return (result);
194         *target = (isc_uint32_t)value64;
195
196         return (ISC_R_SUCCESS);
197 }