update to 9.7.1-P2
[tridge/bind9.git] / lib / dns / time.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2010  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.33.186.2 2010/04/21 23:50:05 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/stdtime.h>
32 #include <isc/util.h>
33
34 #include <dns/result.h>
35 #include <dns/time.h>
36
37 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
38
39 isc_result_t
40 dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
41         struct tm tm;
42         char buf[sizeof("YYYYMMDDHHMMSS")];
43         int secs;
44         unsigned int l;
45         isc_region_t region;
46
47         REQUIRE(t >= 0);
48
49 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
50 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
51 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
52
53         tm.tm_year = 70;
54         while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
55                 t -= secs;
56                 tm.tm_year++;
57                 if (tm.tm_year + 1900 > 9999)
58                         return (ISC_R_RANGE);
59         }
60         tm.tm_mon = 0;
61         while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
62                 t -= secs;
63                 tm.tm_mon++;
64         }
65         tm.tm_mday = 1;
66         while (86400 <= t) {
67                 t -= 86400;
68                 tm.tm_mday++;
69         }
70         tm.tm_hour = 0;
71         while (3600 <= t) {
72                 t -= 3600;
73                 tm.tm_hour++;
74         }
75         tm.tm_min = 0;
76         while (60 <= t) {
77                 t -= 60;
78                 tm.tm_min++;
79         }
80         tm.tm_sec = (int)t;
81                                  /* yyyy  mm  dd  HH  MM  SS */
82         snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
83                  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
84                  tm.tm_hour, tm.tm_min, tm.tm_sec);
85
86         isc_buffer_availableregion(target, &region);
87         l = strlen(buf);
88
89         if (l > region.length)
90                 return (ISC_R_NOSPACE);
91
92         memcpy(region.base, buf, l);
93         isc_buffer_add(target, l);
94         return (ISC_R_SUCCESS);
95 }
96
97 isc_result_t
98 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
99         isc_stdtime_t now;
100         isc_int64_t start;
101         isc_int64_t base;
102         isc_int64_t t;
103
104         /*
105          * Adjust the time to the closest epoch.  This should be changed
106          * to use a 64-bit counterpart to isc_stdtime_get() if one ever
107          * is defined, but even the current code is good until the year
108          * 2106.
109          */
110         isc_stdtime_get(&now);
111         start = (isc_int64_t) now;
112         start -= 0x7fffffff;
113         base = 0;
114         while ((t = (base + value)) < start) {
115                 base += 0x80000000;
116                 base += 0x80000000;
117         }
118         return (dns_time64_totext(t, target));
119 }
120
121 isc_result_t
122 dns_time64_fromtext(const char *source, isc_int64_t *target) {
123         int year, month, day, hour, minute, second;
124         isc_int64_t value;
125         int secs;
126         int i;
127
128 #define RANGE(min, max, value) \
129         do { \
130                 if (value < (min) || value > (max)) \
131                         return (ISC_R_RANGE); \
132         } while (0)
133
134         if (strlen(source) != 14U)
135                 return (DNS_R_SYNTAX);
136         /*
137          * Confirm the source only consists digits.  sscanf() allows some
138          * minor exceptions.
139          */
140         for (i = 0; i < 14; i++) {
141                 if (!isdigit((unsigned char)source[i]))
142                         return (DNS_R_SYNTAX);
143         }
144         if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
145                    &year, &month, &day, &hour, &minute, &second) != 6)
146                 return (DNS_R_SYNTAX);
147
148         RANGE(1970, 9999, year);
149         RANGE(1, 12, month);
150         RANGE(1, days[month - 1] +
151                  ((month == 2 && is_leap(year)) ? 1 : 0), day);
152         RANGE(0, 23, hour);
153         RANGE(0, 59, minute);
154         RANGE(0, 60, second);           /* 60 == leap second. */
155
156         /*
157          * Calculate seconds since epoch.
158          */
159         value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
160         for (i = 0; i < (month - 1); i++)
161                 value += days[i] * 86400;
162         if (is_leap(year) && month > 2)
163                 value += 86400;
164         for (i = 1970; i < year; i++) {
165                 secs = (is_leap(i) ? 366 : 365) * 86400;
166                 value += secs;
167         }
168
169         *target = value;
170         return (ISC_R_SUCCESS);
171 }
172
173 isc_result_t
174 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
175         isc_int64_t value64;
176         isc_result_t result;
177         result = dns_time64_fromtext(source, &value64);
178         if (result != ISC_R_SUCCESS)
179                 return (result);
180         *target = (isc_uint32_t)value64;
181
182         return (ISC_R_SUCCESS);
183 }