1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) 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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1998-12-12 20:48:14 +00:00
|
|
|
*/
|
1998-10-15 01:20:28 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1998-10-15 01:20:28 +00:00
|
|
|
#include <errno.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <limits.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2014-01-10 18:49:51 -08:00
|
|
|
#include <stdlib.h>
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <sys/time.h> /* Required for struct timeval on some platforms. */
|
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
|
|
|
|
2001-02-23 23:12:28 +00:00
|
|
|
#include <isc/log.h>
|
2001-09-05 17:03:42 +00:00
|
|
|
#include <isc/print.h>
|
2018-08-28 15:43:44 -07:00
|
|
|
#include <isc/strerr.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
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
|
|
|
|
#define NS_PER_US 1000 /*%< Nanoseconds per microsecond. */
|
|
|
|
#define NS_PER_MS 1000000 /*%< Nanoseconds per millisecond. */
|
2021-04-22 18:58:01 -07:00
|
|
|
#define MS_PER_S 1000 /*%< Milliseonds 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
|
|
|
|
2021-03-19 22:48:22 -07:00
|
|
|
#if defined(CLOCK_REALTIME)
|
|
|
|
#define CLOCKSOURCE_HIRES CLOCK_REALTIME
|
|
|
|
#endif /* #if defined(CLOCK_REALTIME) */
|
|
|
|
|
2019-11-28 12:00:27 +01:00
|
|
|
#if defined(CLOCK_REALTIME_COARSE)
|
2018-12-09 00:41:21 +01:00
|
|
|
#define CLOCKSOURCE CLOCK_REALTIME_COARSE
|
2019-11-28 12:00:27 +01:00
|
|
|
#elif defined(CLOCK_REALTIME_FAST)
|
|
|
|
#define CLOCKSOURCE CLOCK_REALTIME_FAST
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(CLOCK_REALTIME_COARSE) */
|
2018-12-09 00:41:21 +01:00
|
|
|
#define CLOCKSOURCE CLOCK_REALTIME
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(CLOCK_REALTIME_COARSE) */
|
2001-02-23 23:12:28 +00:00
|
|
|
|
2021-03-19 22:48:22 -07:00
|
|
|
#if !defined(CLOCKSOURCE_HIRES)
|
|
|
|
#define CLOCKSOURCE_HIRES CLOCKSOURCE
|
|
|
|
#endif /* #ifndef CLOCKSOURCE_HIRES */
|
|
|
|
|
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 };
|
2020-02-12 13:59:18 +01:00
|
|
|
const isc_interval_t *const isc_interval_zero = &zero_interval;
|
1999-06-12 01:15:05 +00:00
|
|
|
|
1998-10-23 23:01:41 +00:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_interval_set(isc_interval_t *i, unsigned int seconds,
|
2020-02-13 14:44:37 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-13 14:44:37 -08: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
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (i->seconds == 0 && i->nanoseconds == 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-23 23:01:41 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
1998-10-23 23:01:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 18:58:01 -07:00
|
|
|
unsigned int
|
|
|
|
isc_interval_ms(const isc_interval_t *i) {
|
|
|
|
REQUIRE(i != NULL);
|
|
|
|
INSIST(i->nanoseconds < NS_PER_S);
|
|
|
|
|
|
|
|
return ((i->seconds * MS_PER_S) + (i->nanoseconds / NS_PER_MS));
|
|
|
|
}
|
|
|
|
|
1998-10-23 23:01:41 +00:00
|
|
|
/***
|
|
|
|
*** Absolute Times
|
|
|
|
***/
|
|
|
|
|
2012-09-26 14:58:53 +10:00
|
|
|
static const isc_time_t epoch = { 0, 0 };
|
2020-02-12 13:59:18 +01:00
|
|
|
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
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
|
2000-04-24 20:58:03 +00:00
|
|
|
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
|
2020-02-13 14:44:37 -08: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;
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-13 14:44:37 -08: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
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (t->seconds == 0 && t->nanoseconds == 0) {
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-23 23:01:41 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
1998-10-23 23:01:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 22:48:22 -07:00
|
|
|
static inline isc_result_t
|
|
|
|
time_now(isc_time_t *t, clockid_t clock) {
|
2018-12-09 00:41:21 +01:00
|
|
|
struct timespec ts;
|
2020-02-13 14:44:37 -08: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
|
|
|
|
2021-03-19 22:48:22 -07:00
|
|
|
if (clock_gettime(clock, &ts) == -1) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2001-08-31 05:57:58 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
|
1998-10-15 01:20:28 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
2018-12-09 00:41:21 +01:00
|
|
|
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_S) {
|
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_UNEXPECTED);
|
2018-12-09 00:41:21 +01: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
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
2018-12-09 00:41:21 +01:00
|
|
|
if (sizeof(ts.tv_sec) > sizeof(t->seconds) &&
|
2020-02-13 14:44:37 -08:00
|
|
|
((ts.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);
|
2020-02-13 21:48:23 +01: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
|
|
|
|
2018-12-09 00:41:21 +01:00
|
|
|
t->seconds = ts.tv_sec;
|
|
|
|
t->nanoseconds = ts.tv_nsec;
|
1998-10-15 01:20:28 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2021-03-19 22:48:22 -07:00
|
|
|
isc_result_t
|
|
|
|
isc_time_now_hires(isc_time_t *t) {
|
|
|
|
return time_now(t, CLOCKSOURCE_HIRES);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_time_now(isc_time_t *t) {
|
|
|
|
return time_now(t, CLOCKSOURCE);
|
|
|
|
}
|
|
|
|
|
1999-06-12 01:15:05 +00:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
|
2018-12-09 00:41:21 +01:00
|
|
|
struct timespec ts;
|
2020-02-13 14:44:37 -08: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);
|
|
|
|
|
2018-12-09 00:41:21 +01:00
|
|
|
if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
2001-08-31 05:57:58 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
|
1999-06-12 01:15:05 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
2018-12-09 00:41:21 +01:00
|
|
|
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_S) {
|
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_UNEXPECTED);
|
2018-12-09 00:41:21 +01: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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.)
|
|
|
|
*/
|
2018-12-09 00:41:21 +01:00
|
|
|
if ((ts.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
|
2020-02-13 14:44:37 -08:00
|
|
|
((long long)ts.tv_sec + i->seconds > UINT_MAX))
|
|
|
|
{
|
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);
|
2020-02-13 21:48:23 +01: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
|
|
|
|
2018-12-09 00:41:21 +01:00
|
|
|
t->seconds = ts.tv_sec + i->seconds;
|
|
|
|
t->nanoseconds = ts.tv_nsec + 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
|
2020-02-13 14:44:37 -08: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
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (t1->seconds < t2->seconds) {
|
1998-10-15 01:20:28 +00:00
|
|
|
return (-1);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (t1->seconds > t2->seconds) {
|
1998-10-15 01:20:28 +00:00
|
|
|
return (1);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (t1->nanoseconds < t2->nanoseconds) {
|
1998-10-15 01:20:28 +00:00
|
|
|
return (-1);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (t1->nanoseconds > t2->nanoseconds) {
|
1998-10-15 01:20:28 +00:00
|
|
|
return (1);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-15 01:20:28 +00:00
|
|
|
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
|
2020-02-13 14:44:37 -08: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);
|
2021-10-20 11:22:52 +02:00
|
|
|
REQUIRE(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
|
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
|
|
|
|
2021-10-20 11:22:52 +02:00
|
|
|
/* Seconds */
|
2021-10-20 11:22:52 +02:00
|
|
|
#if HAVE_BUILTIN_OVERFLOW
|
|
|
|
if (__builtin_uadd_overflow(t->seconds, i->seconds, &result->seconds)) {
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
}
|
|
|
|
#else
|
2021-10-20 11:22:52 +02:00
|
|
|
if (t->seconds > UINT_MAX - 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
|
|
|
return (ISC_R_RANGE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-23 23:01:41 +00:00
|
|
|
result->seconds = t->seconds + i->seconds;
|
2021-10-20 11:22:52 +02:00
|
|
|
#endif
|
2021-10-20 11:22:52 +02:00
|
|
|
|
|
|
|
/* Nanoseconds */
|
1998-10-23 23:01:41 +00:00
|
|
|
result->nanoseconds = t->nanoseconds + i->nanoseconds;
|
2000-09-18 18:43:03 +00:00
|
|
|
if (result->nanoseconds >= NS_PER_S) {
|
2021-10-20 11:22:52 +02:00
|
|
|
if (result->seconds == UINT_MAX) {
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
}
|
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;
|
2021-10-20 11:22:52 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_t *result) {
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(t != NULL && i != NULL && result != NULL);
|
2021-10-20 11:22:52 +02:00
|
|
|
REQUIRE(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
|
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
|
|
|
|
2021-10-20 11:22:52 +02:00
|
|
|
/* Seconds */
|
2021-10-20 11:22:52 +02:00
|
|
|
#if HAVE_BUILTIN_OVERFLOW
|
|
|
|
if (__builtin_usub_overflow(t->seconds, i->seconds, &result->seconds)) {
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
}
|
|
|
|
#else
|
2021-10-20 11:22:52 +02:00
|
|
|
if (t->seconds < i->seconds) {
|
2020-02-12 13:59:18 +01:00
|
|
|
return (ISC_R_RANGE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-23 23:01:41 +00:00
|
|
|
result->seconds = t->seconds - i->seconds;
|
2021-10-20 11:22:52 +02:00
|
|
|
#endif
|
2021-10-20 11:22:52 +02:00
|
|
|
|
|
|
|
/* Nanoseconds */
|
2020-02-13 21:48:23 +01:00
|
|
|
if (t->nanoseconds >= i->nanoseconds) {
|
1998-10-23 23:01:41 +00:00
|
|
|
result->nanoseconds = t->nanoseconds - i->nanoseconds;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2021-10-20 11:22:52 +02:00
|
|
|
if (result->seconds == 0) {
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
}
|
1998-10-23 23:01:41 +00:00
|
|
|
result->seconds--;
|
2021-10-20 11:22:52 +02:00
|
|
|
result->nanoseconds = NS_PER_S + t->nanoseconds -
|
|
|
|
i->nanoseconds;
|
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
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
uint64_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
|
2018-03-28 14:19:37 +02:00
|
|
|
uint64_t i1, i2, i3;
|
1999-10-09 02:40:32 +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);
|
1999-10-09 02:40:32 +00:00
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
i1 = (uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
|
|
|
|
i2 = (uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
|
1999-10-09 02:40:32 +00:00
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (i1 <= i2) {
|
1999-10-09 02:40:32 +00:00
|
|
|
return (0);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-10-09 02:40:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t
|
2020-02-13 14:44:37 -08: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
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
return ((uint32_t)t->seconds);
|
2000-03-10 17:49:27 +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
|
2020-02-13 14:44:37 -08: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
|
2021-10-20 11:22:52 +02:00
|
|
|
* 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.)
|
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
|
|
|
*
|
|
|
|
* The mission, then, is to avoid generating any kind of warning
|
2021-10-20 11:22:52 +02:00
|
|
|
* about "signed versus unsigned" while trying to determine if
|
|
|
|
* 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 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
|
|
|
*
|
2021-10-20 11:22:52 +02: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;
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
INSIST(sizeof(unsigned int) == sizeof(uint32_t));
|
|
|
|
INSIST(sizeof(time_t) >= sizeof(uint32_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
|
|
|
|
2020-02-13 21:48:23 +01: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);
|
2020-02-13 21:48:23 +01: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
|
|
|
|
|
|
|
*secondsp = seconds;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t
|
2020-02-13 14:44:37 -08: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
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
return ((uint32_t)t->nanoseconds);
|
2000-03-10 17:49:27 +00:00
|
|
|
}
|
2001-08-31 21:51:27 +00:00
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
2001-08-31 21:51:27 +00:00
|
|
|
unsigned int flen;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct tm tm;
|
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);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
now = (time_t)t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
flen = strftime(buf, len, "%d-%b-%Y %X", localtime_r(&now, &tm));
|
2001-09-05 17:05:47 +00:00
|
|
|
INSIST(flen < len);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (flen != 0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
snprintf(buf + flen, len - flen, ".%03u",
|
|
|
|
t->nanoseconds / NS_PER_MS);
|
2020-02-13 21:48:23 +01:00
|
|
|
} 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
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
2006-12-21 06:03:37 +00:00
|
|
|
unsigned int flen;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct tm tm;
|
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
|
|
|
/*
|
2021-10-20 11:22:52 +02:00
|
|
|
* 5 spaces, 1 comma, 3 GMT, 2 %d, 4 %Y, 8 %H:%M:%S, 3+ %a, 3+
|
|
|
|
* %b (29+)
|
2015-02-05 17:18:15 -08:00
|
|
|
*/
|
2006-12-21 06:03:37 +00:00
|
|
|
now = (time_t)t->seconds;
|
2017-08-03 08:42:27 +10:00
|
|
|
flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT",
|
|
|
|
gmtime_r(&now, &tm));
|
2006-12-21 06:03:37 +00:00
|
|
|
INSIST(flen < len);
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:14:57 -08:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
|
2014-01-09 15:14:57 -08:00
|
|
|
struct tm t_tm;
|
2020-02-13 14:44:37 -08:00
|
|
|
time_t when;
|
|
|
|
char *p;
|
2014-01-09 15:14:57 -08:00
|
|
|
|
|
|
|
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);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (p == NULL) {
|
2014-01-09 15:14:57 -08:00
|
|
|
return (ISC_R_UNEXPECTED);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-01-14 23:12:17 -08:00
|
|
|
when = isc_tm_timegm(&t_tm);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (when == -1) {
|
2014-01-09 15:14:57 -08:00
|
|
|
return (ISC_R_UNEXPECTED);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2014-01-09 15:14:57 -08:00
|
|
|
isc_time_set(t, when, 0);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2016-11-22 23:34:47 -08:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_formatISO8601L(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
2016-11-22 23:34:47 -08:00
|
|
|
unsigned int flen;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct tm tm;
|
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
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
|
2016-11-22 23:34:47 -08:00
|
|
|
INSIST(flen < len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_formatISO8601Lms(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
2016-11-22 23:34:47 -08:00
|
|
|
unsigned int flen;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct tm tm;
|
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
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:58:56 +10:00
|
|
|
void
|
|
|
|
isc_time_formatISO8601Lus(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
|
|
|
now = (time_t)t->seconds;
|
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
|
|
|
|
INSIST(flen < len);
|
|
|
|
if (flen > 0U && len - flen >= 6) {
|
|
|
|
snprintf(buf + flen, len - flen, ".%06u",
|
|
|
|
t->nanoseconds / NS_PER_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-21 06:03:37 +00:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
2006-12-21 06:03:37 +00:00
|
|
|
unsigned int flen;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct tm tm;
|
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
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
|
2006-12-21 06:03:37 +00:00
|
|
|
INSIST(flen < len);
|
|
|
|
}
|
2016-01-07 15:34:14 -08:00
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
2016-01-07 15:34:14 -08:00
|
|
|
unsigned int flen;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct tm tm;
|
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
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
|
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
|
|
|
|
2020-09-29 14:58:56 +10:00
|
|
|
void
|
|
|
|
isc_time_formatISO8601us(const isc_time_t *t, char *buf, unsigned int len) {
|
|
|
|
time_t now;
|
|
|
|
unsigned int flen;
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
REQUIRE(t != NULL);
|
|
|
|
INSIST(t->nanoseconds < NS_PER_S);
|
|
|
|
REQUIRE(buf != NULL);
|
|
|
|
REQUIRE(len > 0);
|
|
|
|
|
|
|
|
now = (time_t)t->seconds;
|
|
|
|
flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm));
|
|
|
|
INSIST(flen < len);
|
|
|
|
if (flen > 0U && len - flen >= 5) {
|
|
|
|
flen -= 1; /* rewind one character (Z) */
|
|
|
|
snprintf(buf + flen, len - flen, ".%06uZ",
|
|
|
|
t->nanoseconds / NS_PER_US);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 23:20:40 -08:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_formatshorttimestamp(const isc_time_t *t, char *buf,
|
|
|
|
unsigned int len) {
|
|
|
|
time_t now;
|
2017-03-08 23:20:40 -08:00
|
|
|
unsigned int flen;
|
2020-02-13 14:44:37 -08:00
|
|
|
struct tm tm;
|
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
|
|
|
flen = strftime(buf, len, "%Y%m%d%H%M%S", gmtime_r(&now, &tm));
|
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);
|
|
|
|
}
|
|
|
|
}
|