2 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 Internet Software Consortium.
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.
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.
18 /* $Id: time.c,v 1.31.128.2 2009/01/19 23:47:02 tbox Exp $ */
25 #include <isc/string.h> /* Required for HP/UX (and others?) */
28 #include <isc/print.h>
29 #include <isc/region.h>
30 #include <isc/stdtime.h>
33 #include <dns/result.h>
36 static int days
[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
39 dns_time64_totext(isc_int64_t t
, isc_buffer_t
*target
) {
41 char buf
[sizeof("YYYYMMDDHHMMSS")];
48 #define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
49 #define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
50 #define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
53 while ((secs
= year_secs(tm
.tm_year
+ 1900)) <= t
) {
56 if (tm
.tm_year
+ 1900 > 9999)
60 while ((secs
= month_secs(tm
.tm_mon
, tm
.tm_year
+ 1900)) <= t
) {
80 /* yyyy mm dd HH MM SS */
81 snprintf(buf
, sizeof(buf
), "%04d%02d%02d%02d%02d%02d",
82 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
83 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
85 isc_buffer_availableregion(target
, ®ion
);
88 if (l
> region
.length
)
89 return (ISC_R_NOSPACE
);
91 memcpy(region
.base
, buf
, l
);
92 isc_buffer_add(target
, l
);
93 return (ISC_R_SUCCESS
);
97 dns_time32_totext(isc_uint32_t value
, isc_buffer_t
*target
) {
104 * Adjust the time to the closest epoch. This should be changed
105 * to use a 64-bit counterpart to isc_stdtime_get() if one ever
106 * is defined, but even the current code is good until the year
109 isc_stdtime_get(&now
);
110 start
= (isc_int64_t
) now
;
113 while ((t
= (base
+ value
)) < start
) {
117 return (dns_time64_totext(t
, target
));
121 dns_time64_fromtext(const char *source
, isc_int64_t
*target
) {
122 int year
, month
, day
, hour
, minute
, second
;
127 #define RANGE(min, max, value) \
129 if (value < (min) || value > (max)) \
130 return (ISC_R_RANGE); \
133 if (strlen(source
) != 14U)
134 return (DNS_R_SYNTAX
);
135 if (sscanf(source
, "%4d%2d%2d%2d%2d%2d",
136 &year
, &month
, &day
, &hour
, &minute
, &second
) != 6)
137 return (DNS_R_SYNTAX
);
139 RANGE(1970, 9999, year
);
141 RANGE(1, days
[month
- 1] +
142 ((month
== 2 && is_leap(year
)) ? 1 : 0), day
);
144 RANGE(0, 59, minute
);
145 RANGE(0, 60, second
); /* 60 == leap second. */
148 * Calculate seconds since epoch.
150 value
= second
+ (60 * minute
) + (3600 * hour
) + ((day
- 1) * 86400);
151 for (i
= 0; i
< (month
- 1); i
++)
152 value
+= days
[i
] * 86400;
153 if (is_leap(year
) && month
> 2)
155 for (i
= 1970; i
< year
; i
++) {
156 secs
= (is_leap(i
) ? 366 : 365) * 86400;
161 return (ISC_R_SUCCESS
);
165 dns_time32_fromtext(const char *source
, isc_uint32_t
*target
) {
168 result
= dns_time64_fromtext(source
, &value64
);
169 if (result
!= ISC_R_SUCCESS
)
171 *target
= (isc_uint32_t
)value64
;
173 return (ISC_R_SUCCESS
);