Blame lib/isc/unix/time.c

Packit Service ae04f2
/*
Packit Service ae04f2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit Service ae04f2
 *
Packit Service ae04f2
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit Service ae04f2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service ae04f2
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service ae04f2
 *
Packit Service ae04f2
 * See the COPYRIGHT file distributed with this work for additional
Packit Service ae04f2
 * information regarding copyright ownership.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
Packit Service ae04f2
/*! \file */
Packit Service ae04f2
Packit Service ae04f2
#include <config.h>
Packit Service ae04f2
Packit Service ae04f2
#include <errno.h>
Packit Service ae04f2
#include <limits.h>
Packit Service ae04f2
#include <inttypes.h>
Packit Service ae04f2
#include <stdbool.h>
Packit Service ae04f2
#include <stdlib.h>
Packit Service ae04f2
#include <syslog.h>
Packit Service ae04f2
#include <time.h>
Packit Service ae04f2
Packit Service ae04f2
#include <sys/time.h>	/* Required for struct timeval on some platforms. */
Packit Service ae04f2
Packit Service ae04f2
#include <isc/log.h>
Packit Service ae04f2
#include <isc/platform.h>
Packit Service ae04f2
#include <isc/print.h>
Packit Service ae04f2
#include <isc/strerror.h>
Packit Service ae04f2
#include <isc/string.h>
Packit Service ae04f2
#include <isc/time.h>
Packit Service ae04f2
#include <isc/tm.h>
Packit Service ae04f2
#include <isc/util.h>
Packit Service ae04f2
Packit Service ae04f2
#define NS_PER_S	1000000000	/*%< Nanoseconds per second. */
Packit Service ae04f2
#define NS_PER_US	1000		/*%< Nanoseconds per microsecond. */
Packit Service ae04f2
#define NS_PER_MS	1000000		/*%< Nanoseconds per millisecond. */
Packit Service ae04f2
#define US_PER_S	1000000		/*%< Microseconds per second. */
Packit Service ae04f2
Packit Service ae04f2
/*
Packit Service ae04f2
 * All of the INSIST()s checks of nanoseconds < NS_PER_S are for
Packit Service ae04f2
 * consistency checking of the type. In lieu of magic numbers, it
Packit Service ae04f2
 * is the best we've got.  The check is only performed on functions which
Packit Service ae04f2
 * need an initialized type.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
#ifndef ISC_FIX_TV_USEC
Packit Service ae04f2
#define ISC_FIX_TV_USEC 1
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
/*%
Packit Service ae04f2
 *** Intervals
Packit Service ae04f2
 ***/
Packit Service ae04f2
Packit Service ae04f2
static const isc_interval_t zero_interval = { 0, 0 };
Packit Service ae04f2
const isc_interval_t * const isc_interval_zero = &zero_interval;
Packit Service ae04f2
Packit Service ae04f2
#if ISC_FIX_TV_USEC
Packit Service ae04f2
static inline void
Packit Service ae04f2
fix_tv_usec(struct timeval *tv) {
Packit Service ae04f2
	bool fixed = false;
Packit Service ae04f2
Packit Service ae04f2
	if (tv->tv_usec < 0) {
Packit Service ae04f2
		fixed = true;
Packit Service ae04f2
		do {
Packit Service ae04f2
			tv->tv_sec -= 1;
Packit Service ae04f2
			tv->tv_usec += US_PER_S;
Packit Service ae04f2
		} while (tv->tv_usec < 0);
Packit Service ae04f2
	} else if (tv->tv_usec >= US_PER_S) {
Packit Service ae04f2
		fixed = true;
Packit Service ae04f2
		do {
Packit Service ae04f2
			tv->tv_sec += 1;
Packit Service ae04f2
			tv->tv_usec -= US_PER_S;
Packit Service ae04f2
		} while (tv->tv_usec >=US_PER_S);
Packit Service ae04f2
	}
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Call syslog directly as was are called from the logging functions.
Packit Service ae04f2
	 */
Packit Service ae04f2
	if (fixed)
Packit Service ae04f2
		(void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
Packit Service ae04f2
}
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_interval_set(isc_interval_t *i,
Packit Service ae04f2
		 unsigned int seconds, unsigned int nanoseconds)
Packit Service ae04f2
{
Packit Service ae04f2
	REQUIRE(i != NULL);
Packit Service ae04f2
	REQUIRE(nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	i->seconds = seconds;
Packit Service ae04f2
	i->nanoseconds = nanoseconds;
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
bool
Packit Service ae04f2
isc_interval_iszero(const isc_interval_t *i) {
Packit Service ae04f2
	REQUIRE(i != NULL);
Packit Service ae04f2
	INSIST(i->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	if (i->seconds == 0 && i->nanoseconds == 0)
Packit Service ae04f2
		return (true);
Packit Service ae04f2
Packit Service ae04f2
	return (false);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
Packit Service ae04f2
/***
Packit Service ae04f2
 *** Absolute Times
Packit Service ae04f2
 ***/
Packit Service ae04f2
Packit Service ae04f2
static const isc_time_t epoch = { 0, 0 };
Packit Service ae04f2
const isc_time_t * const isc_time_epoch = &epoch;
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	REQUIRE(nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	t->seconds = seconds;
Packit Service ae04f2
	t->nanoseconds = nanoseconds;
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_time_settoepoch(isc_time_t *t) {
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
Packit Service ae04f2
	t->seconds = 0;
Packit Service ae04f2
	t->nanoseconds = 0;
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
bool
Packit Service ae04f2
isc_time_isepoch(const isc_time_t *t) {
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	if (t->seconds == 0 && t->nanoseconds == 0)
Packit Service ae04f2
		return (true);
Packit Service ae04f2
Packit Service ae04f2
	return (false);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_time_now(isc_time_t *t) {
Packit Service ae04f2
	struct timeval tv;
Packit Service ae04f2
	char strbuf[ISC_STRERRORSIZE];
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
Packit Service ae04f2
	if (gettimeofday(&tv, NULL) == -1) {
Packit Service ae04f2
		isc__strerror(errno, strbuf, sizeof(strbuf));
Packit Service ae04f2
		UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Does POSIX guarantee the signedness of tv_sec and tv_usec?  If not,
Packit Service ae04f2
	 * then this test will generate warnings for platforms on which it is
Packit Service ae04f2
	 * unsigned.  In any event, the chances of any of these problems
Packit Service ae04f2
	 * happening are pretty much zero, but since the libisc library ensures
Packit Service ae04f2
	 * certain things to be true ...
Packit Service ae04f2
	 */
Packit Service ae04f2
#if ISC_FIX_TV_USEC
Packit Service ae04f2
	fix_tv_usec(&tv;;
Packit Service ae04f2
	if (tv.tv_sec < 0)
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
#else
Packit Service ae04f2
	if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Ensure the tv_sec value fits in t->seconds.
Packit Service ae04f2
	 */
Packit Service ae04f2
	if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
Packit Service ae04f2
	    ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
Packit Service ae04f2
		return (ISC_R_RANGE);
Packit Service ae04f2
Packit Service ae04f2
	t->seconds = tv.tv_sec;
Packit Service ae04f2
	t->nanoseconds = tv.tv_usec * NS_PER_US;
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
Packit Service ae04f2
	struct timeval tv;
Packit Service ae04f2
	char strbuf[ISC_STRERRORSIZE];
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	REQUIRE(i != NULL);
Packit Service ae04f2
	INSIST(i->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	if (gettimeofday(&tv, NULL) == -1) {
Packit Service ae04f2
		isc__strerror(errno, strbuf, sizeof(strbuf));
Packit Service ae04f2
		UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Does POSIX guarantee the signedness of tv_sec and tv_usec?  If not,
Packit Service ae04f2
	 * then this test will generate warnings for platforms on which it is
Packit Service ae04f2
	 * unsigned.  In any event, the chances of any of these problems
Packit Service ae04f2
	 * happening are pretty much zero, but since the libisc library ensures
Packit Service ae04f2
	 * certain things to be true ...
Packit Service ae04f2
	 */
Packit Service ae04f2
#if ISC_FIX_TV_USEC
Packit Service ae04f2
	fix_tv_usec(&tv;;
Packit Service ae04f2
	if (tv.tv_sec < 0)
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
#else
Packit Service ae04f2
	if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Ensure the resulting seconds value fits in the size of an
Packit Service ae04f2
	 * unsigned int.  (It is written this way as a slight optimization;
Packit Service ae04f2
	 * note that even if both values == INT_MAX, then when added
Packit Service ae04f2
	 * and getting another 1 added below the result is UINT_MAX.)
Packit Service ae04f2
	 */
Packit Service ae04f2
	if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
Packit Service ae04f2
	    ((long long)tv.tv_sec + i->seconds > UINT_MAX))
Packit Service ae04f2
		return (ISC_R_RANGE);
Packit Service ae04f2
Packit Service ae04f2
	t->seconds = tv.tv_sec + i->seconds;
Packit Service ae04f2
	t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
Packit Service ae04f2
	if (t->nanoseconds >= NS_PER_S) {
Packit Service ae04f2
		t->seconds++;
Packit Service ae04f2
		t->nanoseconds -= NS_PER_S;
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
int
Packit Service ae04f2
isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
Packit Service ae04f2
	REQUIRE(t1 != NULL && t2 != NULL);
Packit Service ae04f2
	INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	if (t1->seconds < t2->seconds)
Packit Service ae04f2
		return (-1);
Packit Service ae04f2
	if (t1->seconds > t2->seconds)
Packit Service ae04f2
		return (1);
Packit Service ae04f2
	if (t1->nanoseconds < t2->nanoseconds)
Packit Service ae04f2
		return (-1);
Packit Service ae04f2
	if (t1->nanoseconds > t2->nanoseconds)
Packit Service ae04f2
		return (1);
Packit Service ae04f2
	return (0);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
Packit Service ae04f2
{
Packit Service ae04f2
	REQUIRE(t != NULL && i != NULL && result != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Ensure the resulting seconds value fits in the size of an
Packit Service ae04f2
	 * unsigned int.  (It is written this way as a slight optimization;
Packit Service ae04f2
	 * note that even if both values == INT_MAX, then when added
Packit Service ae04f2
	 * and getting another 1 added below the result is UINT_MAX.)
Packit Service ae04f2
	 */
Packit Service ae04f2
	if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
Packit Service ae04f2
	    ((long long)t->seconds + i->seconds > UINT_MAX))
Packit Service ae04f2
		return (ISC_R_RANGE);
Packit Service ae04f2
Packit Service ae04f2
	result->seconds = t->seconds + i->seconds;
Packit Service ae04f2
	result->nanoseconds = t->nanoseconds + i->nanoseconds;
Packit Service ae04f2
	if (result->nanoseconds >= NS_PER_S) {
Packit Service ae04f2
		result->seconds++;
Packit Service ae04f2
		result->nanoseconds -= NS_PER_S;
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
Packit Service ae04f2
		  isc_time_t *result)
Packit Service ae04f2
{
Packit Service ae04f2
	REQUIRE(t != NULL && i != NULL && result != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	if ((unsigned int)t->seconds < i->seconds ||
Packit Service ae04f2
	    ((unsigned int)t->seconds == i->seconds &&
Packit Service ae04f2
	     t->nanoseconds < i->nanoseconds))
Packit Service ae04f2
	    return (ISC_R_RANGE);
Packit Service ae04f2
Packit Service ae04f2
	result->seconds = t->seconds - i->seconds;
Packit Service ae04f2
	if (t->nanoseconds >= i->nanoseconds)
Packit Service ae04f2
		result->nanoseconds = t->nanoseconds - i->nanoseconds;
Packit Service ae04f2
	else {
Packit Service ae04f2
		result->nanoseconds = NS_PER_S - i->nanoseconds +
Packit Service ae04f2
			t->nanoseconds;
Packit Service ae04f2
		result->seconds--;
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
uint64_t
Packit Service ae04f2
isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
Packit Service ae04f2
	uint64_t i1, i2, i3;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(t1 != NULL && t2 != NULL);
Packit Service ae04f2
	INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	i1 = (uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
Packit Service ae04f2
	i2 = (uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
Packit Service ae04f2
Packit Service ae04f2
	if (i1 <= i2)
Packit Service ae04f2
		return (0);
Packit Service ae04f2
Packit Service ae04f2
	i3 = i1 - i2;
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Convert to microseconds.
Packit Service ae04f2
	 */
Packit Service ae04f2
	i3 /= NS_PER_US;
Packit Service ae04f2
Packit Service ae04f2
	return (i3);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
uint32_t
Packit Service ae04f2
isc_time_seconds(const isc_time_t *t) {
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	return ((uint32_t)t->seconds);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
Packit Service ae04f2
	time_t seconds;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * Ensure that the number of seconds represented by t->seconds
Packit Service ae04f2
	 * can be represented by a time_t.  Since t->seconds is an unsigned
Packit Service ae04f2
	 * int and since time_t is mostly opaque, this is trickier than
Packit Service ae04f2
	 * it seems.  (This standardized opaqueness of time_t is *very*
Packit Service ae04f2
	 * frustrating; time_t is not even limited to being an integral
Packit Service ae04f2
	 * type.)
Packit Service ae04f2
	 *
Packit Service ae04f2
	 * The mission, then, is to avoid generating any kind of warning
Packit Service ae04f2
	 * about "signed versus unsigned" while trying to determine if the
Packit Service ae04f2
	 * the unsigned int t->seconds is out range for tv_sec, which is
Packit Service ae04f2
	 * pretty much only true if time_t is a signed integer of the same
Packit Service ae04f2
	 * size as the return value of isc_time_seconds.
Packit Service ae04f2
	 *
Packit Service ae04f2
	 * If the paradox in the if clause below is true, t->seconds is out
Packit Service ae04f2
	 * of range for time_t.
Packit Service ae04f2
	 */
Packit Service ae04f2
	seconds = (time_t)t->seconds;
Packit Service ae04f2
Packit Service ae04f2
	INSIST(sizeof(unsigned int) == sizeof(uint32_t));
Packit Service ae04f2
	INSIST(sizeof(time_t) >= sizeof(uint32_t));
Packit Service ae04f2
Packit Service ae04f2
	if (t->seconds > (~0U>>1) && seconds <= (time_t)(~0U>>1))
Packit Service ae04f2
		return (ISC_R_RANGE);
Packit Service ae04f2
Packit Service ae04f2
	*secondsp = seconds;
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
uint32_t
Packit Service ae04f2
isc_time_nanoseconds(const isc_time_t *t) {
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
Packit Service ae04f2
	ENSURE(t->nanoseconds < NS_PER_S);
Packit Service ae04f2
Packit Service ae04f2
	return ((uint32_t)t->nanoseconds);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
Packit Service ae04f2
	time_t now;
Packit Service ae04f2
	unsigned int flen;
Packit Service ae04f2
#ifdef ISC_PLATFORM_USETHREADS
Packit Service ae04f2
	struct tm tm;
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S);
Packit Service ae04f2
	REQUIRE(buf != NULL);
Packit Service ae04f2
	REQUIRE(len > 0);
Packit Service ae04f2
Packit Service ae04f2
	now = (time_t) t->seconds;
Packit Service ae04f2
#ifdef ISC_PLATFORM_USETHREADS
Packit Service ae04f2
	flen = strftime(buf, len, "%d-%b-%Y %X", localtime_r(&now, &tm));
Packit Service ae04f2
#else
Packit Service ae04f2
	flen = strftime(buf, len, "%d-%b-%Y %X", localtime(&now));
Packit Service ae04f2
#endif
Packit Service ae04f2
	INSIST(flen < len);
Packit Service ae04f2
	if (flen != 0)
Packit Service ae04f2
		snprintf(buf + flen, len - flen,
Packit Service ae04f2
			 ".%03u", t->nanoseconds / NS_PER_MS);
Packit Service ae04f2
	else {
Packit Service ae04f2
		strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
Packit Service ae04f2
	}
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
Packit Service ae04f2
	time_t now;
Packit Service ae04f2
	unsigned int flen;
Packit Service ae04f2
#ifdef ISC_PLATFORM_USETHREADS
Packit Service ae04f2
	struct tm tm;
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S);
Packit Service ae04f2
	REQUIRE(buf != NULL);
Packit Service ae04f2
	REQUIRE(len > 0);
Packit Service ae04f2
Packit Service ae04f2
	/*
Packit Service ae04f2
	 * 5 spaces, 1 comma, 3 GMT, 2 %d, 4 %Y, 8 %H:%M:%S, 3+ %a, 3+ %b (29+)
Packit Service ae04f2
	 */
Packit Service ae04f2
	now = (time_t)t->seconds;
Packit Service ae04f2
#ifdef ISC_PLATFORM_USETHREADS
Packit Service ae04f2
	flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT",
Packit Service ae04f2
			gmtime_r(&now, &tm));
Packit Service ae04f2
#else
Packit Service ae04f2
	flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now));
Packit Service ae04f2
#endif
Packit Service ae04f2
	INSIST(flen < len);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
isc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
Packit Service ae04f2
	struct tm t_tm;
Packit Service ae04f2
	time_t when;
Packit Service ae04f2
	char *p;
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(buf != NULL);
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
Packit Service ae04f2
	p = isc_tm_strptime(buf, "%a, %d %b %Y %H:%M:%S", &t_tm);
Packit Service ae04f2
	if (p == NULL)
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
	when = isc_tm_timegm(&t_tm);
Packit Service ae04f2
	if (when == -1)
Packit Service ae04f2
		return (ISC_R_UNEXPECTED);
Packit Service ae04f2
	isc_time_set(t, when, 0);
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
Packit Service ae04f2
	time_t now;
Packit Service ae04f2
	unsigned int flen;
Packit Service ae04f2
#ifdef ISC_PLATFORM_USETHREADS
Packit Service ae04f2
	struct tm tm;
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S);
Packit Service ae04f2
	REQUIRE(buf != NULL);
Packit Service ae04f2
	REQUIRE(len > 0);
Packit Service ae04f2
Packit Service ae04f2
	now = (time_t)t->seconds;
Packit Service ae04f2
#ifdef ISC_PLATFORM_USETHREADS
Packit Service ae04f2
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
Packit Service ae04f2
#else
Packit Service ae04f2
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
Packit Service ae04f2
#endif
Packit Service ae04f2
	INSIST(flen < len);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
Packit Service ae04f2
	time_t now;
Packit Service ae04f2
	unsigned int flen;
Packit Service ae04f2
#ifdef ISC_PLATFORM_USETHREADS
Packit Service ae04f2
	struct tm tm;
Packit Service ae04f2
#endif
Packit Service ae04f2
Packit Service ae04f2
	REQUIRE(t != NULL);
Packit Service ae04f2
	INSIST(t->nanoseconds < NS_PER_S);
Packit Service ae04f2
	REQUIRE(buf != NULL);
Packit Service ae04f2
	REQUIRE(len > 0);
Packit Service ae04f2
Packit Service ae04f2
	now = (time_t)t->seconds;
Packit Service ae04f2
#ifdef ISC_PLATFORM_USETHREADS
Packit Service ae04f2
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
Packit Service ae04f2
#else
Packit Service ae04f2
	flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
Packit Service ae04f2
#endif
Packit Service ae04f2
	INSIST(flen < len);
Packit Service ae04f2
	if (flen > 0U && len - flen >= 5) {
Packit Service ae04f2
		flen -= 1; /* rewind one character (Z) */
Packit Service ae04f2
		snprintf(buf + flen, len - flen, ".%03uZ",
Packit Service ae04f2
			 t->nanoseconds / NS_PER_MS);
Packit Service ae04f2
	}
Packit Service ae04f2
}