time.c revision 16201b15a6e228bf289f2448cb3b4d6ef47ff621
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 * 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 * 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.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews#include <sys/time.h> /* Required for struct timeval on some platforms. */
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. */
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 *** Intervals
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic const isc_interval_t zero_interval = { 0, 0 };
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsconst isc_interval_t * const isc_interval_zero = &zero_interval;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsstatic inline void
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * Call syslog directly as was are called from the logging functions.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
af9cfbc64363b61aa5903dd916e9fbc152084d4cMark Andrews unsigned int seconds, unsigned int nanoseconds)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews *** Absolute Times
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsconst isc_time_t * const isc_time_epoch = &epoch;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
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 ...
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews * Ensure the tv_sec value fits in t->seconds.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewsisc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
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 if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
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 if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ((long long)tv.tv_sec + i->seconds > UINT_MAX))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewsisc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews return (-1);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews return (-1);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews REQUIRE(t != NULL && i != NULL && result != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
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.)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews ((long long)t->seconds + i->seconds > UINT_MAX))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result->nanoseconds = t->nanoseconds + i->nanoseconds;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrewsisc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews REQUIRE(t != NULL && i != NULL && result != NULL);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews result->nanoseconds = t->nanoseconds - i->nanoseconds;
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews result->nanoseconds = NS_PER_S - i->nanoseconds +
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
1a03b5e68553c37e9cc0097368909dfc37fb8cefMark Andrews INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
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 * Convert to microseconds.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
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 * 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 * If the paradox in the if clause below is true, t->seconds is out
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * of range for time_t.
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews if (t->seconds > (~0U>>1) && seconds <= (time_t)(~0U>>1))
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int flen;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews flen = strftime(buf, len, "%d-%b-%Y %X", localtime(&now));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int flen;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews * 5 spaces, 1 comma, 3 GMT, 2 %d, 4 %Y, 8 %H:%M:%S, 3+ %a, 3+ %b (29+)
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence p = isc_tm_strptime(buf, "%a, %d %b %Y %H:%M:%S", &t_tm);
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int flen;
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrewsisc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews unsigned int flen;
7d2b275f7e9238e2c709737601f6260b5a9a4ee1Mark Andrews flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));