time.c revision bc298cd0f77dce2d452efd3e3d3153e2a9a17dbf
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews/*
3711866d8b0d21676f3a900a03b566f7476912b8Tinderbox User * Copyright (C) 2004, 2005, 2007, 2009-2011 Internet Systems Consortium, Inc. ("ISC")
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * Copyright (C) 1998-2003 Internet Software Consortium.
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews *
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * Permission to use, copy, modify, and/or distribute this software for any
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * purpose with or without fee is hereby granted, provided that the above
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * copyright notice and this permission notice appear in all copies.
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews *
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews */
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews/* $Id: time.c,v 1.38 2012/01/27 01:43:51 marka Exp $ */
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews/*! \file */
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <config.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <stdio.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <isc/string.h> /* Required for HP/UX (and others?) */
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <time.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <ctype.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <isc/print.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <isc/region.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <isc/serial.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <isc/stdtime.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <isc/util.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <dns/result.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews#include <dns/time.h>
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews
15bee593e70faca91a00331184fbbbc66080d422Mark Andrewsstatic int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews
15bee593e70faca91a00331184fbbbc66080d422Mark Andrewsisc_result_t
15bee593e70faca91a00331184fbbbc66080d422Mark Andrewsdns_time64_totext(isc_int64_t t, isc_buffer_t *target) {
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews struct tm tm;
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews char buf[sizeof("YYYYMMDDHHMMSS")];
15bee593e70faca91a00331184fbbbc66080d422Mark Andrews int secs;
unsigned int l;
isc_region_t region;
/*
* Warning. Do NOT use arguments with side effects with these macros.
*/
#define is_leap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
#define year_secs(y) ((is_leap(y) ? 366 : 365 ) * 86400)
#define month_secs(m,y) ((days[m] + ((m == 1 && is_leap(y)) ? 1 : 0 )) * 86400)
tm.tm_year = 70;
while (t < 0) {
if (tm.tm_year == 0)
return (ISC_R_RANGE);
tm.tm_year--;
secs = year_secs(tm.tm_year + 1900);
t += secs;
}
while ((secs = year_secs(tm.tm_year + 1900)) <= t) {
t -= secs;
tm.tm_year++;
if (tm.tm_year + 1900 > 9999)
return (ISC_R_RANGE);
}
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++;
}
tm.tm_sec = (int)t;
/* 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);
isc_buffer_availableregion(target, &region);
l = strlen(buf);
if (l > region.length)
return (ISC_R_NOSPACE);
memcpy(region.base, buf, l);
isc_buffer_add(target, l);
return (ISC_R_SUCCESS);
}
isc_int64_t
dns_time64_from32(isc_uint32_t value) {
isc_stdtime_t now;
isc_int64_t start;
isc_int64_t t;
/*
* 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.
*/
isc_stdtime_get(&now);
start = (isc_int64_t) now;
if (isc_serial_gt(value, now))
t = start + (value - now);
else
t = start - (now - value);
return (t);
}
isc_result_t
dns_time32_totext(isc_uint32_t value, isc_buffer_t *target) {
return (dns_time64_totext(dns_time64_from32(value), target));
}
isc_result_t
dns_time64_fromtext(const char *source, isc_int64_t *target) {
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)) \
return (ISC_R_RANGE); \
} while (0)
if (strlen(source) != 14U)
return (DNS_R_SYNTAX);
/*
* Confirm the source only consists digits. sscanf() allows some
* minor exceptions.
*/
for (i = 0; i < 14; i++) {
if (!isdigit((unsigned char)source[i]))
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(0, 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);
RANGE(0, 60, second); /* 60 == leap second. */
/*
* Calculate seconds from epoch.
* Note: this uses a idealized calendar.
*/
value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
for (i = 0; i < (month - 1); i++)
value += days[i] * 86400;
if (is_leap(year) && month > 2)
value += 86400;
if (year < 1970) {
for (i = 1969; i >= year; i--) {
secs = (is_leap(i) ? 366 : 365) * 86400;
value -= secs;
}
} else {
for (i = 1970; i < year; i++) {
secs = (is_leap(i) ? 366 : 365) * 86400;
value += secs;
}
}
*target = value;
return (ISC_R_SUCCESS);
}
isc_result_t
dns_time32_fromtext(const char *source, isc_uint32_t *target) {
isc_int64_t value64;
isc_result_t result;
result = dns_time64_fromtext(source, &value64);
if (result != ISC_R_SUCCESS)
return (result);
*target = (isc_uint32_t)value64;
return (ISC_R_SUCCESS);
}