1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2018-01-02 23:45:28 +00:00
|
|
|
* Copyright (C) 1998-2001, 2003-2008, 2011, 2012, 2014-2018 Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
1998-12-12 20:48:14 +00:00
|
|
|
*/
|
1998-10-15 01:20:28 +00:00
|
|
|
|
2012-03-08 00:21:15 +11:00
|
|
|
/* $Id$ */
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1998-12-12 19:25:20 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1998-10-15 01:20:28 +00:00
|
|
|
#include <errno.h>
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
#include <limits.h>
|
2014-01-10 18:49:51 -08:00
|
|
|
#include <stdlib.h>
|
2001-02-24 10:22:20 +00:00
|
|
|
#include <syslog.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <time.h>
|
1998-10-15 01:20:28 +00:00
|
|
|
|
2000-05-08 16:30:49 +00:00
|
|
|
#include <sys/time.h> /* Required for struct timeval on some platforms. */
|
|
|
|
|
2001-02-23 23:12:28 +00:00
|
|
|
#include <isc/log.h>
|
2017-08-03 08:42:27 +10:00
|
|
|
#include <isc/platform.h>
|
2001-09-05 17:03:42 +00:00
|
|
|
#include <isc/print.h>
|
2001-08-31 05:57:58 +00:00
|
|
|
#include <isc/strerror.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
1998-10-15 01:20:28 +00:00
|
|
|
#include <isc/time.h>
|
2014-01-14 23:12:17 -08:00
|
|
|
#include <isc/tm.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1998-10-15 01:20:28 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
|
|
|
|
#define NS_PER_US 1000 /*%< Nanoseconds per microsecond. */
|
2016-01-07 15:34:14 -08:00
|
|
|
#define NS_PER_MS 1000000 /*%< Nanoseconds per millisecond. */
|
2005-04-27 04:57:32 +00:00
|
|
|
#define US_PER_S 1000000 /*%< Microseconds per second. */
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* All of the INSIST()s checks of nanoseconds < NS_PER_S are for
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
* consistency checking of the type. In lieu of magic numbers, it
|
|
|
|
* is the best we've got. The check is only performed on functions which
|
|
|
|
* need an initialized type.
|
|
|
|
*/
|
|
|
|
|
2001-02-23 23:12:28 +00:00
|
|
|
#ifndef ISC_FIX_TV_USEC
|
|
|
|
#define ISC_FIX_TV_USEC 1
|
|
|
|
#endif
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
1998-10-23 23:01:41 +00:00
|
|
|
*** Intervals
|
|
|
|
***/
|
|
|
|
|
2012-09-26 14:58:53 +10:00
|
|
|
static const isc_interval_t zero_interval = { 0, 0 };
|
|
|
|
const isc_interval_t * const isc_interval_zero = &zero_interval;
|
1999-06-12 01:15:05 +00:00
|
|
|
|
2001-02-24 10:22:20 +00:00
|
|
|
#if ISC_FIX_TV_USEC
|
|
|
|
static inline void
|
|
|
|
fix_tv_usec(struct timeval *tv) {
|
|
|
|
isc_boolean_t fixed = ISC_FALSE;
|
|
|
|
|
|
|
|
if (tv->tv_usec < 0) {
|
|
|
|
fixed = ISC_TRUE;
|
|
|
|
do {
|
|
|
|
tv->tv_sec -= 1;
|
|
|
|
tv->tv_usec += US_PER_S;
|
|
|
|
} while (tv->tv_usec < 0);
|
|
|
|
} else if (tv->tv_usec >= US_PER_S) {
|
|
|
|
fixed = ISC_TRUE;
|
|
|
|
do {
|
|
|
|
tv->tv_sec += 1;
|
|
|
|
tv->tv_usec -= US_PER_S;
|
|
|
|
} while (tv->tv_usec >=US_PER_S);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Call syslog directly as was are called from the logging functions.
|
|
|
|
*/
|
|
|
|
if (fixed)
|
2001-11-30 01:59:49 +00:00
|
|
|
(void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
|
2001-02-24 10:22:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-10-23 23:01:41 +00:00
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_interval_set(isc_interval_t *i,
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
unsigned int seconds, unsigned int nanoseconds)
|
|
|
|
{
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(i != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
REQUIRE(nanoseconds < NS_PER_S);
|
1998-10-23 23:01:41 +00:00
|
|
|
|
|
|
|
i->seconds = seconds;
|
|
|
|
i->nanoseconds = nanoseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_boolean_t
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_interval_iszero(const isc_interval_t *i) {
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(i != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
INSIST(i->nanoseconds < NS_PER_S);
|
1998-10-23 23:01:41 +00:00
|
|
|
|
|
|
|
if (i->seconds == 0 && i->nanoseconds == 0)
|
|
|
|
return (ISC_TRUE);
|
|
|
|
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
*** Absolute Times
|
|
|
|
***/
|
|
|
|
|
2012-09-26 14:58:53 +10:00
|
|
|
static const isc_time_t epoch = { 0, 0 };
|
|
|
|
const isc_time_t * const isc_time_epoch = &epoch;
|
1999-06-12 01:15:05 +00:00
|
|
|
|
2000-04-24 20:58:03 +00:00
|
|
|
void
|
|
|
|
isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
|
|
|
|
REQUIRE(t != NULL);
|
2000-05-18 18:59:38 +00:00
|
|
|
REQUIRE(nanoseconds < NS_PER_S);
|
2000-04-24 20:58:03 +00:00
|
|
|
|
|
|
|
t->seconds = seconds;
|
|
|
|
t->nanoseconds = nanoseconds;
|
|
|
|
}
|
|
|
|
|
1998-10-23 23:01:41 +00:00
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_settoepoch(isc_time_t *t) {
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
|
|
|
|
t->seconds = 0;
|
|
|
|
t->nanoseconds = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_boolean_t
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_time_isepoch(const isc_time_t *t) {
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(t != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
1998-10-23 23:01:41 +00:00
|
|
|
|
|
|
|
if (t->seconds == 0 && t->nanoseconds == 0)
|
|
|
|
return (ISC_TRUE);
|
|
|
|
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|
|
|
|
|
2001-02-23 23:12:28 +00:00
|
|
|
|
1998-10-26 23:07:57 +00:00
|
|
|
isc_result_t
|
1999-06-12 01:15:05 +00:00
|
|
|
isc_time_now(isc_time_t *t) {
|
1998-10-15 01:20:28 +00:00
|
|
|
struct timeval tv;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-10-15 01:20:28 +00:00
|
|
|
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(t != NULL);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-15 01:20:28 +00:00
|
|
|
if (gettimeofday(&tv, NULL) == -1) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
|
1998-10-15 01:20:28 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
/*
|
|
|
|
* Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
|
|
|
|
* then this test will generate warnings for platforms on which it is
|
|
|
|
* unsigned. In any event, the chances of any of these problems
|
|
|
|
* happening are pretty much zero, but since the libisc library ensures
|
|
|
|
* certain things to be true ...
|
|
|
|
*/
|
2001-02-23 23:12:28 +00:00
|
|
|
#if ISC_FIX_TV_USEC
|
2001-02-24 10:22:20 +00:00
|
|
|
fix_tv_usec(&tv);
|
|
|
|
if (tv.tv_sec < 0)
|
2001-02-23 23:12:28 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
#else
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
|
|
|
|
return (ISC_R_UNEXPECTED);
|
2001-02-23 23:12:28 +00:00
|
|
|
#endif
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Ensure the tv_sec value fits in t->seconds.
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
*/
|
|
|
|
if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
|
2003-07-25 00:01:16 +00:00
|
|
|
((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
return (ISC_R_RANGE);
|
|
|
|
|
1998-10-23 23:01:41 +00:00
|
|
|
t->seconds = tv.tv_sec;
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
t->nanoseconds = tv.tv_usec * NS_PER_US;
|
1998-10-15 01:20:28 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-06-12 01:15:05 +00:00
|
|
|
isc_result_t
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
|
1999-06-12 01:15:05 +00:00
|
|
|
struct timeval tv;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1999-06-12 01:15:05 +00:00
|
|
|
|
|
|
|
REQUIRE(t != NULL);
|
|
|
|
REQUIRE(i != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
INSIST(i->nanoseconds < NS_PER_S);
|
|
|
|
|
1999-06-12 01:15:05 +00:00
|
|
|
if (gettimeofday(&tv, NULL) == -1) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
|
1999-06-12 01:15:05 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
/*
|
|
|
|
* Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
|
|
|
|
* then this test will generate warnings for platforms on which it is
|
|
|
|
* unsigned. In any event, the chances of any of these problems
|
|
|
|
* happening are pretty much zero, but since the libisc library ensures
|
|
|
|
* certain things to be true ...
|
|
|
|
*/
|
2001-02-24 10:22:20 +00:00
|
|
|
#if ISC_FIX_TV_USEC
|
|
|
|
fix_tv_usec(&tv);
|
|
|
|
if (tv.tv_sec < 0)
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
#else
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
|
|
|
|
return (ISC_R_UNEXPECTED);
|
2001-02-24 10:22:20 +00:00
|
|
|
#endif
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure the resulting seconds value fits in the size of an
|
|
|
|
* unsigned int. (It is written this way as a slight optimization;
|
|
|
|
* note that even if both values == INT_MAX, then when added
|
|
|
|
* and getting another 1 added below the result is UINT_MAX.)
|
|
|
|
*/
|
|
|
|
if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
|
|
|
|
((long long)tv.tv_sec + i->seconds > UINT_MAX))
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
|
1999-06-12 01:15:05 +00:00
|
|
|
t->seconds = tv.tv_sec + i->seconds;
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
|
2008-02-15 02:24:04 +00:00
|
|
|
if (t->nanoseconds >= NS_PER_S) {
|
1999-06-12 01:15:05 +00:00
|
|
|
t->seconds++;
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
t->nanoseconds -= NS_PER_S;
|
1999-06-12 01:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-10-15 01:20:28 +00:00
|
|
|
int
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
|
1998-10-22 01:33:20 +00:00
|
|
|
REQUIRE(t1 != NULL && t2 != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
|
1998-10-15 01:20:28 +00:00
|
|
|
|
1998-10-22 01:33:20 +00:00
|
|
|
if (t1->seconds < t2->seconds)
|
1998-10-15 01:20:28 +00:00
|
|
|
return (-1);
|
1998-10-22 01:33:20 +00:00
|
|
|
if (t1->seconds > t2->seconds)
|
1998-10-15 01:20:28 +00:00
|
|
|
return (1);
|
1998-10-22 01:33:20 +00:00
|
|
|
if (t1->nanoseconds < t2->nanoseconds)
|
1998-10-15 01:20:28 +00:00
|
|
|
return (-1);
|
1998-10-22 01:33:20 +00:00
|
|
|
if (t1->nanoseconds > t2->nanoseconds)
|
1998-10-15 01:20:28 +00:00
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
isc_result_t
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
|
|
|
|
{
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(t != NULL && i != NULL && result != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure the resulting seconds value fits in the size of an
|
|
|
|
* unsigned int. (It is written this way as a slight optimization;
|
|
|
|
* note that even if both values == INT_MAX, then when added
|
|
|
|
* and getting another 1 added below the result is UINT_MAX.)
|
|
|
|
*/
|
|
|
|
if ((t->seconds > INT_MAX || i->seconds > INT_MAX) &&
|
|
|
|
((long long)t->seconds + i->seconds > UINT_MAX))
|
|
|
|
return (ISC_R_RANGE);
|
1998-10-15 01:20:28 +00:00
|
|
|
|
1998-10-23 23:01:41 +00:00
|
|
|
result->seconds = t->seconds + i->seconds;
|
|
|
|
result->nanoseconds = t->nanoseconds + i->nanoseconds;
|
2000-09-18 18:43:03 +00:00
|
|
|
if (result->nanoseconds >= NS_PER_S) {
|
1998-10-23 23:01:41 +00:00
|
|
|
result->seconds++;
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
result->nanoseconds -= NS_PER_S;
|
1998-10-15 01:20:28 +00:00
|
|
|
}
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
1998-10-15 01:20:28 +00:00
|
|
|
}
|
|
|
|
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
isc_result_t
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
|
|
|
|
isc_time_t *result)
|
|
|
|
{
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(t != NULL && i != NULL && result != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
|
|
|
|
|
|
|
|
if ((unsigned int)t->seconds < i->seconds ||
|
|
|
|
((unsigned int)t->seconds == i->seconds &&
|
|
|
|
t->nanoseconds < i->nanoseconds))
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
|
1998-10-23 23:01:41 +00:00
|
|
|
result->seconds = t->seconds - i->seconds;
|
|
|
|
if (t->nanoseconds >= i->nanoseconds)
|
|
|
|
result->nanoseconds = t->nanoseconds - i->nanoseconds;
|
1998-10-15 01:20:28 +00:00
|
|
|
else {
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
result->nanoseconds = NS_PER_S - i->nanoseconds +
|
1998-10-23 23:01:41 +00:00
|
|
|
t->nanoseconds;
|
|
|
|
result->seconds--;
|
1998-10-15 01:20:28 +00:00
|
|
|
}
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
1998-10-15 01:20:28 +00:00
|
|
|
}
|
1999-10-09 02:40:32 +00:00
|
|
|
|
|
|
|
isc_uint64_t
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
|
1999-10-09 02:40:32 +00:00
|
|
|
isc_uint64_t i1, i2, i3;
|
|
|
|
|
|
|
|
REQUIRE(t1 != NULL && t2 != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
|
1999-10-09 02:40:32 +00:00
|
|
|
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
i1 = (isc_uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
|
|
|
|
i2 = (isc_uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
|
1999-10-09 02:40:32 +00:00
|
|
|
|
|
|
|
if (i1 <= i2)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
i3 = i1 - i2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert to microseconds.
|
|
|
|
*/
|
2011-03-11 06:11:27 +00:00
|
|
|
i3 /= NS_PER_US;
|
1999-10-09 02:40:32 +00:00
|
|
|
|
|
|
|
return (i3);
|
|
|
|
}
|
2000-03-10 17:49:27 +00:00
|
|
|
|
|
|
|
isc_uint32_t
|
2001-09-20 06:50:34 +00:00
|
|
|
isc_time_seconds(const isc_time_t *t) {
|
2000-04-24 20:58:03 +00:00
|
|
|
REQUIRE(t != NULL);
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
2000-04-24 20:58:03 +00:00
|
|
|
|
2000-03-10 17:49:27 +00:00
|
|
|
return ((isc_uint32_t)t->seconds);
|
|
|
|
}
|
|
|
|
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
isc_result_t
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
time_t seconds;
|
|
|
|
|
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the number of seconds represented by t->seconds
|
|
|
|
* can be represented by a time_t. Since t->seconds is an unsigned
|
|
|
|
* int and since time_t is mostly opaque, this is trickier than
|
|
|
|
* it seems. (This standardized opaqueness of time_t is *very*
|
|
|
|
* frustrating; time_t is not even limited to being an integral
|
|
|
|
* type.)
|
|
|
|
*
|
|
|
|
* The mission, then, is to avoid generating any kind of warning
|
|
|
|
* about "signed versus unsigned" while trying to determine if the
|
|
|
|
* the unsigned int t->seconds is out range for tv_sec, which is
|
|
|
|
* pretty much only true if time_t is a signed integer of the same
|
2000-08-01 01:33:37 +00:00
|
|
|
* size as the return value of isc_time_seconds.
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
*
|
2012-03-08 15:30:51 +11:00
|
|
|
* If the paradox in the if clause below is true, t->seconds is out
|
|
|
|
* of range for time_t.
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
*/
|
|
|
|
seconds = (time_t)t->seconds;
|
|
|
|
|
|
|
|
INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
|
|
|
|
INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
|
|
|
|
|
2012-03-10 23:45:53 +00:00
|
|
|
if (t->seconds > (~0U>>1) && seconds <= (time_t)(~0U>>1))
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
return (ISC_R_RANGE);
|
|
|
|
|
|
|
|
*secondsp = seconds;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-03-10 17:49:27 +00:00
|
|
|
isc_uint32_t
|
2001-09-05 17:22:55 +00:00
|
|
|
isc_time_nanoseconds(const isc_time_t *t) {
|
2000-04-24 20:58:03 +00:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
|
189. [func] isc_time_secondsastimet(), a new function, will ensure
that the number of seconds in an isc_time_t does not
exceed the range of a time_t, or return ISC_R_RANGE.
Similarly, isc_time_now(), isc_time_nowplusinterval(),
isc_time_add() and isc_time_subtract() now check the
range for overflow/underflow. In the case of
isc_time_subtract, this changed a calling requirement
(ie, something that could generate an assertion)
into merely a condition that returns an error result.
isc_time_add() and isc_time_subtract() were void-
valued before but now return isc_result_t.
The seconds member isc_time_t on Unix platforms was changed from time_t
to unsigned int.
unix/time.c now uses macros for nanoseconds per second, nanoseconds per
microsecond and microseconds per second to make sure that the right
number of zeros appears each place the constant is used.
unix/time.c functions which take initialized isc_(interval|time)_t arguments
INSIST() that the nanoseconds value is less than one full second.
unix/time.c's isc_time_microdiff was broken because it did multiplication and
addition with unsigned integers and attempted to set them a 64 bit int to
avoid overflow, but C's ints don't promote to 64 bits on machines that only
have 32 bit longs. Fixed.
Added all the pertinent documentation to time.h.
2000-05-18 17:08:32 +00:00
|
|
|
ENSURE(t->nanoseconds < NS_PER_S);
|
2000-03-10 17:49:27 +00:00
|
|
|
|
|
|
|
return ((isc_uint32_t)t->nanoseconds);
|
|
|
|
}
|
2001-08-31 21:51:27 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
struct tm tm;
|
|
|
|
#endif
|
2001-08-31 21:51:27 +00:00
|
|
|
|
2018-01-02 10:48:08 +11:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
2001-08-31 21:58:49 +00:00
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
2001-08-31 21:51:27 +00:00
|
|
|
now = (time_t) t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
flen = strftime(buf, len, "%d-%b-%Y %X", localtime_r(&now, &tm));
|
|
|
|
#else
|
2001-12-17 02:55:06 +00:00
|
|
|
flen = strftime(buf, len, "%d-%b-%Y %X", localtime(&now));
|
2017-08-03 08:42:27 +10:00
|
|
|
#endif
|
2001-09-05 17:05:47 +00:00
|
|
|
INSIST(flen < len);
|
|
|
|
if (flen != 0)
|
|
|
|
snprintf(buf + flen, len - flen,
|
2016-01-07 15:34:14 -08:00
|
|
|
".%03u", t->nanoseconds / NS_PER_MS);
|
2015-01-12 09:04:16 +05:30
|
|
|
else {
|
2017-09-14 18:11:56 +10:00
|
|
|
strlcpy(buf, "99-Bad-9999 99:99:99.999", len);
|
2015-01-12 09:04:16 +05:30
|
|
|
}
|
2001-08-31 21:51:27 +00:00
|
|
|
}
|
2006-12-21 06:03:37 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
struct tm tm;
|
|
|
|
#endif
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2018-01-02 10:48:08 +11:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
2006-12-21 06:03:37 +00:00
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
2015-02-05 17:18:15 -08:00
|
|
|
/*
|
|
|
|
* 5 spaces, 1 comma, 3 GMT, 2 %d, 4 %Y, 8 %H:%M:%S, 3+ %a, 3+ %b (29+)
|
|
|
|
*/
|
2006-12-21 06:03:37 +00:00
|
|
|
now = (time_t)t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT",
|
|
|
|
gmtime_r(&now, &tm));
|
|
|
|
#else
|
2006-12-21 06:03:37 +00:00
|
|
|
flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now));
|
2017-08-03 08:42:27 +10:00
|
|
|
#endif
|
2006-12-21 06:03:37 +00:00
|
|
|
INSIST(flen < len);
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:14:57 -08:00
|
|
|
isc_result_t
|
|
|
|
isc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
|
|
|
|
struct tm t_tm;
|
|
|
|
time_t when;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
REQUIRE(buf != NULL);
|
|
|
|
REQUIRE(t != NULL);
|
2018-01-02 10:48:08 +11:00
|
|
|
|
2014-01-14 23:12:17 -08:00
|
|
|
p = isc_tm_strptime(buf, "%a, %d %b %Y %H:%M:%S", &t_tm);
|
2014-01-09 15:14:57 -08:00
|
|
|
if (p == NULL)
|
|
|
|
return (ISC_R_UNEXPECTED);
|
2014-01-14 23:12:17 -08:00
|
|
|
when = isc_tm_timegm(&t_tm);
|
2014-01-09 15:14:57 -08:00
|
|
|
if (when == -1)
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
isc_time_set(t, when, 0);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:34:47 -08:00
|
|
|
void
|
|
|
|
isc_time_formatISO8601L(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
struct tm tm;
|
|
|
|
#endif
|
2016-11-22 23:34:47 -08:00
|
|
|
|
2018-01-02 10:57:31 +11:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
2016-11-22 23:34:47 -08:00
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
|
|
|
now = (time_t)t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
|
|
|
|
#else
|
2016-11-22 23:34:47 -08:00
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime(&now));
|
2017-08-03 08:42:27 +10:00
|
|
|
#endif
|
2016-11-22 23:34:47 -08:00
|
|
|
INSIST(flen < len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_time_formatISO8601Lms(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
struct tm tm;
|
|
|
|
#endif
|
2016-11-22 23:34:47 -08:00
|
|
|
|
2018-01-02 10:57:31 +11:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
2016-11-22 23:34:47 -08:00
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
|
|
|
now = (time_t)t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
|
|
|
|
#else
|
2016-11-22 23:34:47 -08:00
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime(&now));
|
2017-08-03 08:42:27 +10:00
|
|
|
#endif
|
2016-11-22 23:34:47 -08:00
|
|
|
INSIST(flen < len);
|
|
|
|
if (flen > 0U && len - flen >= 6) {
|
|
|
|
snprintf(buf + flen, len - flen, ".%03u",
|
|
|
|
t->nanoseconds / NS_PER_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
void
|
|
|
|
isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
struct tm tm;
|
|
|
|
#endif
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2018-01-02 10:48:08 +11:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
2006-12-21 06:03:37 +00:00
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
|
|
|
now = (time_t)t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
|
|
|
|
#else
|
2007-02-13 02:49:08 +00:00
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
|
2017-08-03 08:42:27 +10:00
|
|
|
#endif
|
2006-12-21 06:03:37 +00:00
|
|
|
INSIST(flen < len);
|
|
|
|
}
|
2016-01-07 15:34:14 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
struct tm tm;
|
|
|
|
#endif
|
2016-01-07 15:34:14 -08:00
|
|
|
|
2018-01-02 10:48:08 +11:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
2016-01-07 15:34:14 -08:00
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
|
|
|
now = (time_t)t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
|
|
|
|
#else
|
2016-01-07 15:34:14 -08:00
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
|
2017-08-03 08:42:27 +10:00
|
|
|
#endif
|
2016-01-07 15:34:14 -08:00
|
|
|
INSIST(flen < len);
|
2016-11-22 23:34:47 -08:00
|
|
|
if (flen > 0U && len - flen >= 5) {
|
|
|
|
flen -= 1; /* rewind one character (Z) */
|
2016-01-07 15:34:14 -08:00
|
|
|
snprintf(buf + flen, len - flen, ".%03uZ",
|
|
|
|
t->nanoseconds / NS_PER_MS);
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 23:20:40 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
isc_time_formatshorttimestamp(const isc_time_t *t, char *buf, unsigned int len)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
struct tm tm;
|
|
|
|
#endif
|
2017-03-08 23:20:40 -08:00
|
|
|
|
2018-01-04 09:57:40 +11:00
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
2017-03-08 23:20:40 -08:00
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
|
|
|
now = (time_t)t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
flen = strftime(buf, len, "%Y%m%d%H%M%S", gmtime_r(&now, &tm));
|
|
|
|
#else
|
2017-03-08 23:20:40 -08:00
|
|
|
flen = strftime(buf, len, "%Y%m%d%H%M%S", gmtime(&now));
|
2017-08-03 08:42:27 +10:00
|
|
|
#endif
|
2017-03-08 23:20:40 -08:00
|
|
|
INSIST(flen < len);
|
|
|
|
if (flen > 0U && len - flen >= 5) {
|
|
|
|
snprintf(buf + flen, len - flen, "%03u",
|
|
|
|
t->nanoseconds / NS_PER_MS);
|
|
|
|
}
|
|
|
|
}
|