time.c revision 6fa1cb5754695d550a58c6e8978fda65f5146af7
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Copyright (C) 1998, 1999, 2000 Internet Software Consortium.
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.
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
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence#include <sys/time.h> /* Required for struct timeval on some platforms. */
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 * 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.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *** Intervals
87983da955bf63128de85d180359bdc418516c3cDavid Lawrencestatic isc_interval_t zero_interval = { 0, 0 };
87983da955bf63128de85d180359bdc418516c3cDavid Lawrenceisc_interval_t *isc_interval_zero = &zero_interval;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence unsigned int seconds, unsigned int nanoseconds)
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().
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence * Returns ISC_TRUE iff. 'i' is the zero interval.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence *** Absolute Times
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set 't' to a particular number of seconds + nanoseconds since the
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set 't' to the time of the epoch.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Returns ISC_TRUE iff. 't' is the epoch ("time zero").
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set *t to the current absolute time.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__, strerror(errno));
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 if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Ensure the tv_sec value fits in t->seconds.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_nowplusinterval(isc_time_t *t, isc_interval_t *i) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Set *t to the current absolute time + i.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__, strerror(errno));
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 if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
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 if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
6e033f89d4f1fcf266e07d91fcabf474af4939a1David Lawrence ((long long)tv.tv_sec + i->seconds > UINT_MAX))
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrenceisc_time_compare(isc_time_t *t1, isc_time_t *t2) {
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Compare the times referenced by 't1' and 't2'
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrewsisc_time_add(isc_time_t *t, isc_interval_t *i, isc_time_t *result) {
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews * Add 't' to 'i', storing the result in 'result'.
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews REQUIRE(t != NULL && i != NULL && result != NULL);
b2e6eb8cea7c491667c97fb46ad04149a86195a4Mark Andrews INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
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.)
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence ((long long)t->seconds + i->seconds > UINT_MAX))
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence result->nanoseconds = t->nanoseconds + i->nanoseconds;
d3be9a9c6ef76a5d7671b0962785ca025b153d2bAndreas Gustafssonisc_time_subtract(isc_time_t *t, isc_interval_t *i, isc_time_t *result) {
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * Subtract 'i' from 't', storing the result in 'result'.
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence REQUIRE(t != NULL && i != NULL && result != NULL);
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence result->nanoseconds = t->nanoseconds - i->nanoseconds;
30a4d5b0c23eb7a73d9635a98250560437a42d59David Lawrence result->nanoseconds = NS_PER_S - i->nanoseconds +
4716e94840921878b26e493576f84afe4fe08752Mark Andrewsisc_time_microdiff(isc_time_t *t1, isc_time_t *t2) {
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
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;
df3974bf576d3238f2e1feb152d9a63ab526d4d0David Lawrence * Convert to microseconds.
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrenceisc_time_secondsastimet(isc_time_t *t, time_t *secondsp) {
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 * 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 * 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.
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
3734f3f1bad4160cdd7563bc4801bca7e82f8abdDavid Lawrence INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
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. */