time.c revision 16201b15a6e228bf289f2448cb3b4d6ef47ff621
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews/*
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * Copyright (C) 2004-2008, 2011, 2012, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews *
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * purpose with or without fee is hereby granted, provided that the above
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * copyright notice and this permission notice appear in all copies.
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews *
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews * PERFORMANCE OF THIS SOFTWARE.
7e4d75a5daeaaf8a7f559f9bd7fbf540184e235cMark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence/* $Id$ */
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff/*! \file */
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <config.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <errno.h>
8c8360e5c3e4b27641633c22d0fcfd3f1f3af2b5Michael Graff#include <limits.h>
8c8360e5c3e4b27641633c22d0fcfd3f1f3af2b5Michael Graff#include <stdlib.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <syslog.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <time.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <sys/time.h> /* Required for struct timeval on some platforms. */
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley#include <isc/log.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <isc/print.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <isc/strerror.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <isc/string.h>
37d266d288410d1ead241c02a8a1dbcb0160be46Michael Graff#include <isc/time.h>
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews#include <isc/tm.h>
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <isc/util.h>
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#define NS_PER_US 1000 /*%< Nanoseconds per microsecond. */
d0783e645b149fcea7e7f22cd43f87b5d188b055Mark Andrews#define NS_PER_MS 1000000 /*%< Nanoseconds per millisecond. */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#define US_PER_S 1000000 /*%< Microseconds per second. */
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley/*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * All of the INSIST()s checks of nanoseconds < NS_PER_S are for
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * consistency checking of the type. In lieu of magic numbers, it
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * is the best we've got. The check is only performed on functions which
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * need an initialized type.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#ifndef ISC_FIX_TV_USEC
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#define ISC_FIX_TV_USEC 1
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#endif
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/*%
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *** Intervals
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ***/
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic const isc_interval_t zero_interval = { 0, 0 };
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsconst isc_interval_t * const isc_interval_zero = &zero_interval;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#if ISC_FIX_TV_USEC
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic inline void
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsfix_tv_usec(struct timeval *tv) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_boolean_t fixed = ISC_FALSE;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (tv->tv_usec < 0) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews fixed = ISC_TRUE;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews do {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews tv->tv_sec -= 1;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews tv->tv_usec += US_PER_S;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } while (tv->tv_usec < 0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } else if (tv->tv_usec >= US_PER_S) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews fixed = ISC_TRUE;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews do {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews tv->tv_sec += 1;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews tv->tv_usec -= US_PER_S;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews } while (tv->tv_usec >=US_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Call syslog directly as was are called from the logging functions.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (fixed)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#endif
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_interval_set(isc_interval_t *i,
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews unsigned int seconds, unsigned int nanoseconds)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews{
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(i != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews i->seconds = seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews i->nanoseconds = nanoseconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_boolean_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_interval_iszero(const isc_interval_t *i) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(i != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(i->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (i->seconds == 0 && i->nanoseconds == 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_TRUE);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_FALSE);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews/***
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *** Absolute Times
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ***/
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic const isc_time_t epoch = { 0, 0 };
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsconst isc_time_t * const isc_time_epoch = &epoch;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->seconds = seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds = nanoseconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_settoepoch(isc_time_t *t) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->seconds = 0;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds = 0;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_boolean_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_isepoch(const isc_time_t *t) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (t->seconds == 0 && t->nanoseconds == 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_TRUE);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
6add3af8397580475ab9fa10ac8c1269465cff87Mark Andrews return (ISC_FALSE);
6add3af8397580475ab9fa10ac8c1269465cff87Mark Andrews}
6add3af8397580475ab9fa10ac8c1269465cff87Mark Andrews
6add3af8397580475ab9fa10ac8c1269465cff87Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_now(isc_time_t *t) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews struct timeval tv;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews char strbuf[ISC_STRERRORSIZE];
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if (gettimeofday(&tv, NULL) == -1) {
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews return (ISC_R_UNEXPECTED);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * then this test will generate warnings for platforms on which it is
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * unsigned. In any event, the chances of any of these problems
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * happening are pretty much zero, but since the libisc library ensures
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * certain things to be true ...
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#if ISC_FIX_TV_USEC
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews fix_tv_usec(&tv);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (tv.tv_sec < 0)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews return (ISC_R_UNEXPECTED);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews#else
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews return (ISC_R_UNEXPECTED);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews#endif
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * Ensure the tv_sec value fits in t->seconds.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_RANGE);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->seconds = tv.tv_sec;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds = tv.tv_usec * NS_PER_US;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews return (ISC_R_SUCCESS);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_result_t
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewsisc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews struct timeval tv;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews char strbuf[ISC_STRERRORSIZE];
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL);
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews REQUIRE(i != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(i->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if (gettimeofday(&tv, NULL) == -1) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc__strerror(errno, strbuf, sizeof(strbuf));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_UNEXPECTED);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * then this test will generate warnings for platforms on which it is
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * unsigned. In any event, the chances of any of these problems
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * happening are pretty much zero, but since the libisc library ensures
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * certain things to be true ...
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#if ISC_FIX_TV_USEC
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews fix_tv_usec(&tv);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (tv.tv_sec < 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_UNEXPECTED);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#else
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_UNEXPECTED);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#endif
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Ensure the resulting seconds value fits in the size of an
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * unsigned int. (It is written this way as a slight optimization;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * note that even if both values == INT_MAX, then when added
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * and getting another 1 added below the result is UINT_MAX.)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ((long long)tv.tv_sec + i->seconds > UINT_MAX))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_RANGE);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->seconds = tv.tv_sec + i->seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
ea023072092112a831ca7c2943387ccbb422a3f2Mark Andrews if (t->nanoseconds >= NS_PER_S) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->seconds++;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds -= NS_PER_S;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_SUCCESS);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews}
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsint
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewsisc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t1 != NULL && t2 != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (t1->seconds < t2->seconds)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews return (-1);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if (t1->seconds > t2->seconds)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (1);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (t1->nanoseconds < t2->nanoseconds)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (-1);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (t1->nanoseconds > t2->nanoseconds)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (1);
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews return (0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews{
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews REQUIRE(t != NULL && i != NULL && result != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Ensure the resulting seconds value fits in the size of an
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * unsigned int. (It is written this way as a slight optimization;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * note that even if both values == INT_MAX, then when added
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley * and getting another 1 added below the result is UINT_MAX.)
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ((long long)t->seconds + i->seconds > UINT_MAX))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_RANGE);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews result->seconds = t->seconds + i->seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result->nanoseconds = t->nanoseconds + i->nanoseconds;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if (result->nanoseconds >= NS_PER_S) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result->seconds++;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result->nanoseconds -= NS_PER_S;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_SUCCESS);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewsisc_result_t
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewsisc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews isc_time_t *result)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews{
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL && i != NULL && result != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews if ((unsigned int)t->seconds < i->seconds ||
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ((unsigned int)t->seconds == i->seconds &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds < i->nanoseconds))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_RANGE);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews result->seconds = t->seconds - i->seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (t->nanoseconds >= i->nanoseconds)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result->nanoseconds = t->nanoseconds - i->nanoseconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews else {
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews result->nanoseconds = NS_PER_S - i->nanoseconds +
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result->seconds--;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_SUCCESS);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewsisc_uint64_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_uint64_t i1, i2, i3;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t1 != NULL && t2 != NULL);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews i1 = (isc_uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews i2 = (isc_uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (i1 <= i2)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews i3 = i1 - i2;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Convert to microseconds.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews i3 /= NS_PER_US;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (i3);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_uint32_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_seconds(const isc_time_t *t) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return ((isc_uint32_t)t->seconds);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews time_t seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Ensure that the number of seconds represented by t->seconds
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * can be represented by a time_t. Since t->seconds is an unsigned
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * int and since time_t is mostly opaque, this is trickier than
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * it seems. (This standardized opaqueness of time_t is *very*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * frustrating; time_t is not even limited to being an integral
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * type.)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * The mission, then, is to avoid generating any kind of warning
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * about "signed versus unsigned" while trying to determine if the
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * the unsigned int t->seconds is out range for tv_sec, which is
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * pretty much only true if time_t is a signed integer of the same
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * size as the return value of isc_time_seconds.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * If the paradox in the if clause below is true, t->seconds is out
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * of range for time_t.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews seconds = (time_t)t->seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (t->seconds > (~0U>>1) && seconds <= (time_t)(~0U>>1))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_RANGE);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *secondsp = seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_SUCCESS);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_uint32_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_nanoseconds(const isc_time_t *t) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ENSURE(t->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return ((isc_uint32_t)t->nanoseconds);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews time_t now;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int flen;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews REQUIRE(len > 0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews now = (time_t) t->seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews flen = strftime(buf, len, "%d-%b-%Y %X", localtime(&now));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(flen < len);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (flen != 0)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews snprintf(buf + flen, len - flen,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ".%03u", t->nanoseconds / NS_PER_MS);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews else {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews strncpy(buf, "99-Bad-9999 99:99:99.999", len);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews buf[len - 1] = 0;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews time_t now;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int flen;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence REQUIRE(len > 0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews /*
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * 5 spaces, 1 comma, 3 GMT, 2 %d, 4 %Y, 8 %H:%M:%S, 3+ %a, 3+ %b (29+)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews now = (time_t)t->seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(flen < len);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_result_t
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews struct tm t_tm;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews time_t when;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews char *p;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(buf != NULL);
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence REQUIRE(t != NULL);
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence p = isc_tm_strptime(buf, "%a, %d %b %Y %H:%M:%S", &t_tm);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (p == NULL)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_UNEXPECTED);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews when = isc_tm_timegm(&t_tm);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (when == -1)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_UNEXPECTED);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews isc_time_set(t, when, 0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (ISC_R_SUCCESS);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews time_t now;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int flen;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence REQUIRE(len > 0);
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence
35c842e05dc6382ce1d9161a658d3ff4b2c3d4c9Bob Halley now = (time_t)t->seconds;
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(flen < len);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsvoid
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews time_t now;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int flen;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(len > 0);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews now = (time_t)t->seconds;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews INSIST(flen < len);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (flen == len - 5) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews flen -= 1; /* rewind one character */
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews snprintf(buf + flen, len - flen, ".%03uZ",
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds / NS_PER_MS);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews }
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews}
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews