Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / time.c
blob1c315e2ca962b5a976718834a66e1bc461d62e15
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: time.c,v 1.33 2009/01/17 23:47:43 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <stdio.h>
27 #include <isc/string.h> /* Required for HP/UX (and others?) */
28 #include <time.h>
30 #include <isc/print.h>
31 #include <isc/region.h>
32 #include <isc/stdtime.h>
33 #include <isc/util.h>
35 #include <dns/result.h>
36 #include <dns/time.h>
38 static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
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;
48 REQUIRE(t >= 0);
50 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
51 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
52 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
54 tm.tm_year = 70;
55 while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
56 t -= secs;
57 tm.tm_year++;
58 if (tm.tm_year + 1900 > 9999)
59 return (ISC_R_RANGE);
61 tm.tm_mon = 0;
62 while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
63 t -= secs;
64 tm.tm_mon++;
66 tm.tm_mday = 1;
67 while (86400 <= t) {
68 t -= 86400;
69 tm.tm_mday++;
71 tm.tm_hour = 0;
72 while (3600 <= t) {
73 t -= 3600;
74 tm.tm_hour++;
76 tm.tm_min = 0;
77 while (60 <= t) {
78 t -= 60;
79 tm.tm_min++;
81 tm.tm_sec = (int)t;
82 /* yyyy mm dd HH MM SS */
83 snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
84 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
85 tm.tm_hour, tm.tm_min, tm.tm_sec);
87 isc_buffer_availableregion(target, &region);
88 l = strlen(buf);
90 if (l > region.length)
91 return (ISC_R_NOSPACE);
93 memcpy(region.base, buf, l);
94 isc_buffer_add(target, l);
95 return (ISC_R_SUCCESS);
98 isc_result_t
99 dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
100 isc_stdtime_t now;
101 isc_int64_t start;
102 isc_int64_t base;
103 isc_int64_t t;
106 * Adjust the time to the closest epoch. This should be changed
107 * to use a 64-bit counterpart to isc_stdtime_get() if one ever
108 * is defined, but even the current code is good until the year
109 * 2106.
111 isc_stdtime_get(&now);
112 start = (isc_int64_t) now;
113 start -= 0x7fffffff;
114 base = 0;
115 while ((t = (base + value)) < start) {
116 base += 0x80000000;
117 base += 0x80000000;
119 return (dns_time64_totext(t, target));
122 isc_result_t
123 dns_time64_fromtext(const char *source, isc_int64_t *target) {
124 int year, month, day, hour, minute, second;
125 isc_int64_t value;
126 int secs;
127 int i;
129 #define RANGE(min, max, value) \
130 do { \
131 if (value < (min) || value > (max)) \
132 return (ISC_R_RANGE); \
133 } while (0)
135 if (strlen(source) != 14U)
136 return (DNS_R_SYNTAX);
137 if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
138 &year, &month, &day, &hour, &minute, &second) != 6)
139 return (DNS_R_SYNTAX);
141 RANGE(1970, 9999, year);
142 RANGE(1, 12, month);
143 RANGE(1, days[month - 1] +
144 ((month == 2 && is_leap(year)) ? 1 : 0), day);
145 RANGE(0, 23, hour);
146 RANGE(0, 59, minute);
147 RANGE(0, 60, second); /* 60 == leap second. */
150 * Calculate seconds since epoch.
152 value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
153 for (i = 0; i < (month - 1); i++)
154 value += days[i] * 86400;
155 if (is_leap(year) && month > 2)
156 value += 86400;
157 for (i = 1970; i < year; i++) {
158 secs = (is_leap(i) ? 366 : 365) * 86400;
159 value += secs;
162 *target = value;
163 return (ISC_R_SUCCESS);
166 isc_result_t
167 dns_time32_fromtext(const char *source, isc_uint32_t *target) {
168 isc_int64_t value64;
169 isc_result_t result;
170 result = dns_time64_fromtext(source, &value64);
171 if (result != ISC_R_SUCCESS)
172 return (result);
173 *target = (isc_uint32_t)value64;
175 return (ISC_R_SUCCESS);