1999-06-08 10:35:23 +00:00
|
|
|
/*
|
2009-01-17 23:47:43 +00:00
|
|
|
* Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
|
2004-03-05 05:14:21 +00:00
|
|
|
* Copyright (C) 1998-2003 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2007-06-18 23:47:57 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
1999-06-08 10:35:23 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
1999-06-08 10:35:23 +00:00
|
|
|
*/
|
|
|
|
|
2009-01-17 23:47:43 +00:00
|
|
|
/* $Id: time.c,v 1.33 2009/01/17 23:47:43 tbox Exp $ */
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2000-05-08 19:23:32 +00:00
|
|
|
#include <isc/string.h> /* Required for HP/UX (and others?) */
|
1999-06-08 10:35:23 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2004-08-28 06:20:14 +00:00
|
|
|
#include <isc/print.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/region.h>
|
2001-05-15 22:05:35 +00:00
|
|
|
#include <isc/stdtime.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
#include <dns/result.h>
|
|
|
|
#include <dns/time.h>
|
|
|
|
|
|
|
|
static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-06-08 10:35:23 +00:00
|
|
|
dns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
|
|
|
|
struct tm tm;
|
2001-11-12 19:05:39 +00:00
|
|
|
char buf[sizeof("YYYYMMDDHHMMSS")];
|
1999-06-08 10:35:23 +00:00
|
|
|
int secs;
|
|
|
|
unsigned int l;
|
|
|
|
isc_region_t region;
|
|
|
|
|
|
|
|
REQUIRE(t >= 0);
|
|
|
|
|
|
|
|
#define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
|
|
|
|
#define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
|
2000-05-08 14:38:29 +00:00
|
|
|
#define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
tm.tm_year = 70;
|
|
|
|
while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
|
|
|
|
t -= secs;
|
|
|
|
tm.tm_year++;
|
|
|
|
if (tm.tm_year + 1900 > 9999)
|
2000-05-15 21:14:38 +00:00
|
|
|
return (ISC_R_RANGE);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
tm.tm_mon = 0;
|
|
|
|
while ((secs = month_secs(tm.tm_mon, tm.tm_year + 1900)) <= t) {
|
|
|
|
t -= secs;
|
|
|
|
tm.tm_mon++;
|
|
|
|
}
|
|
|
|
tm.tm_mday = 1;
|
|
|
|
while (86400 <= t) {
|
|
|
|
t -= 86400;
|
|
|
|
tm.tm_mday++;
|
|
|
|
}
|
|
|
|
tm.tm_hour = 0;
|
|
|
|
while (3600 <= t) {
|
|
|
|
t -= 3600;
|
|
|
|
tm.tm_hour++;
|
|
|
|
}
|
|
|
|
tm.tm_min = 0;
|
|
|
|
while (60 <= t) {
|
|
|
|
t -= 60;
|
|
|
|
tm.tm_min++;
|
|
|
|
}
|
1999-10-08 23:58:07 +00:00
|
|
|
tm.tm_sec = (int)t;
|
2003-04-11 07:25:31 +00:00
|
|
|
/* yyyy mm dd HH MM SS */
|
|
|
|
snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02d",
|
|
|
|
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
|
|
|
|
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
1999-06-08 10:35:23 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(target, ®ion);
|
1999-06-08 10:35:23 +00:00
|
|
|
l = strlen(buf);
|
|
|
|
|
|
|
|
if (l > region.length)
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
memcpy(region.base, buf, l);
|
|
|
|
isc_buffer_add(target, l);
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-06-08 10:35:23 +00:00
|
|
|
dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
|
2001-05-15 22:05:35 +00:00
|
|
|
isc_stdtime_t now;
|
1999-06-08 10:35:23 +00:00
|
|
|
isc_int64_t start;
|
|
|
|
isc_int64_t base;
|
|
|
|
isc_int64_t t;
|
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
/*
|
2001-05-15 22:05:35 +00:00
|
|
|
* Adjust the time to the closest epoch. This should be changed
|
|
|
|
* to use a 64-bit counterpart to isc_stdtime_get() if one ever
|
|
|
|
* is defined, but even the current code is good until the year
|
|
|
|
* 2106.
|
2000-05-08 14:38:29 +00:00
|
|
|
*/
|
2001-05-15 22:05:35 +00:00
|
|
|
isc_stdtime_get(&now);
|
|
|
|
start = (isc_int64_t) now;
|
1999-06-08 10:35:23 +00:00
|
|
|
start -= 0x7fffffff;
|
|
|
|
base = 0;
|
|
|
|
while ((t = (base + value)) < start) {
|
|
|
|
base += 0x80000000;
|
|
|
|
base += 0x80000000;
|
|
|
|
}
|
|
|
|
return (dns_time64_totext(t, target));
|
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
2001-09-21 00:11:30 +00:00
|
|
|
dns_time64_fromtext(const char *source, isc_int64_t *target) {
|
1999-06-08 10:35:23 +00:00
|
|
|
int year, month, day, hour, minute, second;
|
|
|
|
isc_int64_t value;
|
|
|
|
int secs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
#define RANGE(min, max, value) \
|
|
|
|
do { \
|
|
|
|
if (value < (min) || value > (max)) \
|
2000-05-15 21:14:38 +00:00
|
|
|
return (ISC_R_RANGE); \
|
1999-06-08 10:35:23 +00:00
|
|
|
} while (0)
|
|
|
|
|
2003-07-25 00:01:16 +00:00
|
|
|
if (strlen(source) != 14U)
|
1999-06-08 10:35:23 +00:00
|
|
|
return (DNS_R_SYNTAX);
|
|
|
|
if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
|
|
|
|
&year, &month, &day, &hour, &minute, &second) != 6)
|
|
|
|
return (DNS_R_SYNTAX);
|
|
|
|
|
|
|
|
RANGE(1970, 9999, year);
|
|
|
|
RANGE(1, 12, month);
|
|
|
|
RANGE(1, days[month - 1] +
|
|
|
|
((month == 2 && is_leap(year)) ? 1 : 0), day);
|
|
|
|
RANGE(0, 23, hour);
|
|
|
|
RANGE(0, 59, minute);
|
2000-05-08 14:38:29 +00:00
|
|
|
RANGE(0, 60, second); /* 60 == leap second. */
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
/*
|
2009-01-17 15:06:49 +00:00
|
|
|
* Calculate seconds since epoch.
|
2000-05-08 14:38:29 +00:00
|
|
|
*/
|
1999-06-08 10:35:23 +00:00
|
|
|
value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
|
2001-11-27 00:56:32 +00:00
|
|
|
for (i = 0; i < (month - 1); i++)
|
1999-06-08 10:35:23 +00:00
|
|
|
value += days[i] * 86400;
|
|
|
|
if (is_leap(year) && month > 2)
|
|
|
|
value += 86400;
|
|
|
|
for (i = 1970; i < year; i++) {
|
|
|
|
secs = (is_leap(i) ? 366 : 365) * 86400;
|
|
|
|
value += secs;
|
|
|
|
}
|
|
|
|
|
|
|
|
*target = value;
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
2001-09-21 00:11:30 +00:00
|
|
|
dns_time32_fromtext(const char *source, isc_uint32_t *target) {
|
1999-06-08 10:35:23 +00:00
|
|
|
isc_int64_t value64;
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
1999-06-08 10:35:23 +00:00
|
|
|
result = dns_time64_fromtext(source, &value64);
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
1999-06-08 10:35:23 +00:00
|
|
|
return (result);
|
2004-03-16 05:52:24 +00:00
|
|
|
*target = (isc_uint32_t)value64;
|
1999-06-08 10:35:23 +00:00
|
|
|
|
2000-05-15 21:14:38 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-08 10:35:23 +00:00
|
|
|
}
|