time.c revision 6fa1cb5754695d550a58c6e8978fda65f5146af7
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence/*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Copyright (C) 1998, 1999, 2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Permission to use, copy, modify, and distribute this software for any
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * purpose with or without fee is hereby granted, provided that the above
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15a44745412679c30a6d022733925af70a38b715David Lawrence * SOFTWARE.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson#include <config.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#include <errno.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <limits.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <time.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <sys/time.h> /* Required for struct timeval on some platforms. */
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/string.h>
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#include <isc/time.h>
ce2be9b7211ab5bacaa10fe74ef35def3a3f6089David Lawrence#include <isc/util.h>
ce2be9b7211ab5bacaa10fe74ef35def3a3f6089David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#define NS_PER_S 1000000000 /* Nanoseconds per second. */
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence#define NS_PER_US 1000 /* Nanoseconds per microsecond. */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence#define US_PER_S 1000000 /* Microseconds per second. */
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence/*
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence * All of the INSIST()s checks of nanoseconds < NS_PER_S are for
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * consistency checking of the type. In lieu of magic numbers, it
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * is the best we've got. The check is only performed on functions which
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence * need an initialized type.
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence */
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence/***
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *** Intervals
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ***/
dbb33bdc762a897844661e013823672c03af67fcDavid Lawrence
87983da955bf63128de85d180359bdc418516c3cDavid Lawrencestatic isc_interval_t zero_interval = { 0, 0 };
87983da955bf63128de85d180359bdc418516c3cDavid Lawrenceisc_interval_t *isc_interval_zero = &zero_interval;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrencevoid
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrenceisc_interval_set(isc_interval_t *i,
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence unsigned int seconds, unsigned int nanoseconds)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence{
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set 'i' to a value representing an interval of 'seconds' seconds
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews * and 'nanoseconds' nanoseconds, suitable for use in isc_time_add()
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews * and isc_time_subtract().
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(i != NULL);
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence REQUIRE(nanoseconds < NS_PER_S);
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence i->seconds = seconds;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence i->nanoseconds = nanoseconds;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence}
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrenceisc_boolean_t
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrenceisc_interval_iszero(isc_interval_t *i) {
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence /*
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence * Returns ISC_TRUE iff. 'i' is the zero interval.
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson REQUIRE(i != NULL);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson INSIST(i->nanoseconds < NS_PER_S);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (i->seconds == 0 && i->nanoseconds == 0)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_TRUE);
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_FALSE);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence/***
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *** Absolute Times
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ***/
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrencestatic isc_time_t epoch = { 0, 0 };
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_t *isc_time_epoch = &epoch;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrencevoid
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set 't' to a particular number of seconds + nanoseconds since the
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * epoch.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t != NULL);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t->nanoseconds < NS_PER_S);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->seconds = seconds;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->nanoseconds = nanoseconds;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrencevoid
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_settoepoch(isc_time_t *t) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set 't' to the time of the epoch.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t != NULL);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->seconds = 0;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->nanoseconds = 0;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_boolean_t
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_isepoch(isc_time_t *t) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Returns ISC_TRUE iff. 't' is the epoch ("time zero").
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t != NULL);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence INSIST(t->nanoseconds < NS_PER_S);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (t->seconds == 0 && t->nanoseconds == 0)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_TRUE);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_FALSE);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_result_t
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_now(isc_time_t *t) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence struct timeval tv;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set *t to the current absolute time.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t != NULL);
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews if (gettimeofday(&tv, NULL) == -1) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__, strerror(errno));
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_UNEXPECTED);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence }
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * then this test will generate warnings for platforms on which it is
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * unsigned. In any event, the chances of any of these problems
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * happening are pretty much zero, but since the libisc library ensures
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * certain things to be true ...
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_UNEXPECTED);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Ensure the tv_sec value fits in t->seconds.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_RANGE);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->seconds = tv.tv_sec;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->nanoseconds = tv.tv_usec * NS_PER_US;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_SUCCESS);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_result_t
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_nowplusinterval(isc_time_t *t, isc_interval_t *i) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence struct timeval tv;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set *t to the current absolute time + i.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t != NULL);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(i != NULL);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence INSIST(i->nanoseconds < NS_PER_S);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (gettimeofday(&tv, NULL) == -1) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__, strerror(errno));
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_UNEXPECTED);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence }
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * then this test will generate warnings for platforms on which it is
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * unsigned. In any event, the chances of any of these problems
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * happening are pretty much zero, but since the libisc library ensures
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * certain things to be true ...
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_UNEXPECTED);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Ensure the resulting seconds value fits in the size of an
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * unsigned int. (It is written this way as a slight optimization;
6e033f89d4f1fcf266e07d91fcabf474af4939a1David Lawrence * note that even if both values == INT_MAX, then when added
6e033f89d4f1fcf266e07d91fcabf474af4939a1David Lawrence * and getting another 1 added below the result is UINT_MAX.)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
6e033f89d4f1fcf266e07d91fcabf474af4939a1David Lawrence ((long long)tv.tv_sec + i->seconds > UINT_MAX))
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_RANGE);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->seconds = tv.tv_sec + i->seconds;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (t->nanoseconds > NS_PER_S) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->seconds++;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->nanoseconds -= NS_PER_S;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence }
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_SUCCESS);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceint
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_compare(isc_time_t *t1, isc_time_t *t2) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Compare the times referenced by 't1' and 't2'
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t1 != NULL && t2 != NULL);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (t1->seconds < t2->seconds)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (-1);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (t1->seconds > t2->seconds)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (1);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (t1->nanoseconds < t2->nanoseconds)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (-1);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (t1->nanoseconds > t2->nanoseconds)
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews return (1);
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews return (0);
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews}
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrewsisc_result_t
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrewsisc_time_add(isc_time_t *t, isc_interval_t *i, isc_time_t *result) {
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews /*
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews * Add 't' to 'i', storing the result in 'result'.
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews */
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews REQUIRE(t != NULL && i != NULL && result != NULL);
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews /*
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews * Ensure the resulting seconds value fits in the size of an
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews * unsigned int. (It is written this way as a slight optimization;
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews * note that even if both values == INT_MAX, then when added
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews * and getting another 1 added below the result is UINT_MAX.)
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ((long long)t->seconds + i->seconds > UINT_MAX))
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_RANGE);
f53f42b5cbe87ba20ba64ae537ae7559eeaa98bcMichael Sawyer
f53f42b5cbe87ba20ba64ae537ae7559eeaa98bcMichael Sawyer result->seconds = t->seconds + i->seconds;
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence result->nanoseconds = t->nanoseconds + i->nanoseconds;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence if (result->nanoseconds > NS_PER_S) {
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence result->seconds++;
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence result->nanoseconds -= NS_PER_S;
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence }
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence
f53f42b5cbe87ba20ba64ae537ae7559eeaa98bcMichael Sawyer return (ISC_R_SUCCESS);
f53f42b5cbe87ba20ba64ae537ae7559eeaa98bcMichael Sawyer}
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrenceisc_result_t
d3be9a9c6ef76a5d7671b0962785ca025b153d2bAndreas Gustafssonisc_time_subtract(isc_time_t *t, isc_interval_t *i, isc_time_t *result) {
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence /*
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * Subtract 'i' from 't', storing the result in 'result'.
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence */
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence REQUIRE(t != NULL && i != NULL && result != NULL);
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if ((unsigned int)t->seconds < i->seconds ||
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ((unsigned int)t->seconds == i->seconds &&
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->nanoseconds < i->nanoseconds))
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_RANGE);
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence result->seconds = t->seconds - i->seconds;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence if (t->nanoseconds >= i->nanoseconds)
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence result->nanoseconds = t->nanoseconds - i->nanoseconds;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence else {
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence result->nanoseconds = NS_PER_S - i->nanoseconds +
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence t->nanoseconds;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence result->seconds--;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence }
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_SUCCESS);
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence}
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrenceisc_uint64_t
4716e94840921878b26e493576f84afe4fe08752Mark Andrewsisc_time_microdiff(isc_time_t *t1, isc_time_t *t2) {
4716e94840921878b26e493576f84afe4fe08752Mark Andrews isc_uint64_t i1, i2, i3;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence REQUIRE(t1 != NULL && t2 != NULL);
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence i1 = (isc_uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence i2 = (isc_uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
ce2be9b7211ab5bacaa10fe74ef35def3a3f6089David Lawrence if (i1 <= i2)
ce2be9b7211ab5bacaa10fe74ef35def3a3f6089David Lawrence return (0);
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson i3 = i1 - i2;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence /*
df3974bf576d3238f2e1feb152d9a63ab526d4d0David Lawrence * Convert to microseconds.
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence i3 = (i1 - i2) / NS_PER_US;
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (i3);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_uint32_t
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_seconds(isc_time_t *t) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t != NULL);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence INSIST(t->nanoseconds < NS_PER_S);
6dc32b43da2c7af5bf460a45a17d03226c162c91Michael Sawyer
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer return ((isc_uint32_t)t->seconds);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrenceisc_result_t
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrenceisc_time_secondsastimet(isc_time_t *t, time_t *secondsp) {
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence isc_uint64_t i;
5096958739769958dd7a6b69356bf41260033873David Lawrence time_t seconds;
299b673e8ed4f3d6239c61375c2376bfb63c7c01Brian Wellington
5096958739769958dd7a6b69356bf41260033873David Lawrence REQUIRE(t != NULL);
5096958739769958dd7a6b69356bf41260033873David Lawrence INSIST(t->nanoseconds < NS_PER_S);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence /*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Ensure that the number of seconds represented by t->seconds
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * can be represented by a time_t. Since t->seconds is an unsigned
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * int and since time_t is mostly opaque, this is trickier than
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * it seems. (This standardized opaqueness of time_t is *very*
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * frustrating; time_t is not even limited to being an integral
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * type.)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * The mission, then, is to avoid generating any kind of warning
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * about "signed versus unsigned" while trying to determine if the
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * the unsigned int t->seconds is out range for tv_sec, which is
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * pretty much only true if time_t is a signed integer of the same
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence * size as the return value of isc_time_seconds.
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence *
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence * The use of a 64 bit integer takes advantage of C's conversion rules
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer * to either zero fill or sign extend the widened type.
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence */
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence seconds = (time_t)t->seconds;
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (sizeof(time_t) == sizeof(isc_uint32_t) && /* Same size. */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence (time_t)0.5 != 0.5 && /* Not a floating point type. */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence (i = (time_t)-1) != 4294967295u && /* Is signed. */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence (seconds & (1 << (sizeof(time_t) * 8 - 1))) != 0) /* Negative. */
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_RANGE);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *secondsp = seconds;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return (ISC_R_SUCCESS);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_uint32_t
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_nanoseconds(isc_time_t *t) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence REQUIRE(t != NULL);
5096958739769958dd7a6b69356bf41260033873David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ENSURE(t->nanoseconds < NS_PER_S);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence return ((isc_uint32_t)t->nanoseconds);
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence}
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence