1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2014-01-11 07:07:56 +11:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
1998-12-12 20:48:14 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
1998-12-12 20:48:14 +00: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 https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
1998-12-12 20:48:14 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
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
|
|
|
* 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>
|
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>
|
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>
|
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
|
|
|
|
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
|
2018-12-09 00:41:21 +01:00
|
|
|
#else /* if defined(CLOCK_REALTIME_COARSE) */
|
|
|
|
#define CLOCKSOURCE CLOCK_REALTIME
|
2001-02-23 23:12:28 +00:00
|
|
|
#endif /* if defined(CLOCK_REALTIME_COARSE) */
|
|
|
|
|
2021-03-19 22:48:22 -07:00
|
|
|
#if !defined(CLOCKSOURCE_HIRES)
|
|
|
|
#define CLOCKSOURCE_HIRES CLOCKSOURCE
|
|
|
|
#endif /* #ifndef CLOCKSOURCE_HIRES */
|
|
|
|
|
2021-10-20 12:06:09 +02:00
|
|
|
#if !defined(UNIT_TESTING)
|
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;
|
2021-10-20 12:06:09 +02:00
|
|
|
#endif
|
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);
|
2022-11-04 12:02:33 +00:00
|
|
|
REQUIRE(nanoseconds < NS_PER_SEC);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
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);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
1998-10-23 23:01:41 +00: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
|
|
|
}
|
|
|
|
|
2023-03-31 00:12:33 +02:00
|
|
|
static isc_time_t
|
|
|
|
time_now(clockid_t clock) {
|
|
|
|
isc_time_t t;
|
2018-12-09 00:41:21 +01:00
|
|
|
struct timespec ts;
|
1998-10-15 01:20:28 +00:00
|
|
|
|
2023-03-31 00:12:33 +02:00
|
|
|
RUNTIME_CHECK(clock_gettime(clock, &ts) == 0);
|
|
|
|
INSIST(ts.tv_sec >= 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_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
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure the tv_sec value fits in t->seconds.
|
|
|
|
*/
|
2023-03-31 00:12:33 +02:00
|
|
|
INSIST(sizeof(ts.tv_sec) <= sizeof(t.seconds) ||
|
|
|
|
((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
|
|
|
|
2023-03-31 00:12:33 +02:00
|
|
|
t.seconds = ts.tv_sec;
|
|
|
|
t.nanoseconds = ts.tv_nsec;
|
1998-10-15 01:20:28 +00:00
|
|
|
|
2023-03-31 00:12:33 +02:00
|
|
|
return (t);
|
1998-10-15 01:20:28 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 00:12:33 +02:00
|
|
|
isc_time_t
|
|
|
|
isc_time_now_hires(void) {
|
|
|
|
return (time_now(CLOCKSOURCE_HIRES));
|
2021-03-19 22:48:22 -07:00
|
|
|
}
|
|
|
|
|
2023-03-31 00:12:33 +02:00
|
|
|
isc_time_t
|
|
|
|
isc_time_now(void) {
|
|
|
|
return (time_now(CLOCKSOURCE));
|
2021-03-19 22:48:22 -07:00
|
|
|
}
|
|
|
|
|
2023-01-17 16:05:01 +00:00
|
|
|
isc_nanosecs_t
|
|
|
|
isc_time_monotonic(void) {
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
RUNTIME_CHECK(clock_gettime(CLOCK_MONOTONIC, &ts) != -1);
|
|
|
|
|
|
|
|
return (isc_nanosecs_fromtime(((isc_time_t){
|
|
|
|
.seconds = ts.tv_sec,
|
|
|
|
.nanoseconds = ts.tv_nsec,
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-12-09 00:41:21 +01:00
|
|
|
struct timespec ts;
|
1999-06-12 01:15:05 +00:00
|
|
|
|
|
|
|
REQUIRE(t != NULL);
|
|
|
|
REQUIRE(i != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(i->nanoseconds < NS_PER_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
|
|
|
|
2018-12-09 00:41:21 +01:00
|
|
|
if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
|
2022-10-14 17:18:07 +01:00
|
|
|
UNEXPECTED_SYSERROR(errno, "clock_gettime()");
|
1999-06-12 01:15:05 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
2022-11-04 12:02:33 +00:00
|
|
|
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_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
|
|
|
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) &&
|
|
|
|
((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;
|
2022-11-04 12:02:33 +00:00
|
|
|
if (t->nanoseconds >= NS_PER_SEC) {
|
1999-06-12 01:15:05 +00:00
|
|
|
t->seconds++;
|
2022-11-04 12:02:33 +00:00
|
|
|
t->nanoseconds -= NS_PER_SEC;
|
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);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t1->nanoseconds < NS_PER_SEC && t2->nanoseconds < NS_PER_SEC);
|
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);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-22 01:33:20 +00:00
|
|
|
if (t1->seconds > t2->seconds) {
|
1998-10-15 01:20:28 +00:00
|
|
|
return (1);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1998-10-22 01:33:20 +00: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-22 01:33:20 +00: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
|
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);
|
2022-11-04 12:02:33 +00:00
|
|
|
REQUIRE(t->nanoseconds < NS_PER_SEC && i->nanoseconds < NS_PER_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
|
|
|
|
2021-10-20 11:22:52 +02:00
|
|
|
/* Seconds */
|
2022-09-23 15:35:58 +02:00
|
|
|
#if HAVE_BUILTIN_ADD_OVERFLOW
|
|
|
|
if (__builtin_add_overflow(t->seconds, i->seconds, &result->seconds)) {
|
2021-10-20 11:22:52 +02:00
|
|
|
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;
|
2022-11-04 12:02:33 +00:00
|
|
|
if (result->nanoseconds >= NS_PER_SEC) {
|
2021-10-20 11:22:52 +02:00
|
|
|
if (result->seconds == UINT_MAX) {
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
}
|
2022-11-04 12:02:33 +00:00
|
|
|
result->nanoseconds -= NS_PER_SEC;
|
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,
|
|
|
|
isc_time_t *result) {
|
1998-10-23 23:01:41 +00:00
|
|
|
REQUIRE(t != NULL && i != NULL && result != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
REQUIRE(t->nanoseconds < NS_PER_SEC && i->nanoseconds < NS_PER_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
|
|
|
|
2021-10-20 11:22:52 +02:00
|
|
|
/* Seconds */
|
2022-09-23 15:35:58 +02:00
|
|
|
#if HAVE_BUILTIN_SUB_OVERFLOW
|
|
|
|
if (__builtin_sub_overflow(t->seconds, i->seconds, &result->seconds)) {
|
2021-10-20 11:22:52 +02:00
|
|
|
return (ISC_R_RANGE);
|
|
|
|
}
|
|
|
|
#else
|
2021-10-20 11:22:52 +02:00
|
|
|
if (t->seconds < 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
|
|
|
if (t->nanoseconds >= i->nanoseconds) {
|
|
|
|
result->nanoseconds = t->nanoseconds - i->nanoseconds;
|
1998-10-15 01:20:28 +00: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--;
|
2022-11-04 12:02:33 +00:00
|
|
|
result->nanoseconds = NS_PER_SEC + t->nanoseconds -
|
2021-10-20 11:22:52 +02:00
|
|
|
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
|
2001-09-05 17:22:55 +00: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);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t1->nanoseconds < NS_PER_SEC && t2->nanoseconds < NS_PER_SEC);
|
1999-10-09 02:40:32 +00:00
|
|
|
|
2022-11-04 12:02:33 +00:00
|
|
|
i1 = (uint64_t)t1->seconds * NS_PER_SEC + t1->nanoseconds;
|
|
|
|
i2 = (uint64_t)t2->seconds * NS_PER_SEC + t2->nanoseconds;
|
1999-10-09 02:40:32 +00:00
|
|
|
|
|
|
|
if (i1 <= i2) {
|
|
|
|
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
|
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);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
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
|
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);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2012-03-08 15:30:51 +11: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
|
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);
|
|
|
|
|
2022-11-04 12:02:33 +00:00
|
|
|
ENSURE(t->nanoseconds < NS_PER_SEC);
|
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
|
|
|
|
Implement isc_interval_t on top of isc_time_t
Change the isc_interval_t implementation from separate data type and
separate implementation to be shim implementation on top of isc_time_t.
The distinction between isc_interval_t and isc_time_t has been kept
because they are semantically different - isc_interval_t is relative and
isc_time_t is absolute, but this allows isc_time_t and isc_interval_t to
be freely interchangeable, f.e. this:
isc_time_t *t1;
isc_interval_t *interval;
isc_time_t *t2;
isc_interval_set(interval, isc_time_seconds(t2), isc_time_nanoseconds(t2);;
isc_time_subtract(t1, interval, t2);
isc_interval_set(interval, isc_time_seconds(t2), isc_time_nanoseconds(t2));
to just:
isc_time_t *t1;
isc_interval_t *interval;
isc_time_t *t2;
isc_time_subtract(t1, t2, interval);
without introducing a whole set of new functions.
2022-03-13 12:13:11 +01:00
|
|
|
uint32_t
|
|
|
|
isc_time_miliseconds(const isc_time_t *t) {
|
|
|
|
REQUIRE(t != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
Implement isc_interval_t on top of isc_time_t
Change the isc_interval_t implementation from separate data type and
separate implementation to be shim implementation on top of isc_time_t.
The distinction between isc_interval_t and isc_time_t has been kept
because they are semantically different - isc_interval_t is relative and
isc_time_t is absolute, but this allows isc_time_t and isc_interval_t to
be freely interchangeable, f.e. this:
isc_time_t *t1;
isc_interval_t *interval;
isc_time_t *t2;
isc_interval_set(interval, isc_time_seconds(t2), isc_time_nanoseconds(t2);;
isc_time_subtract(t1, interval, t2);
isc_interval_set(interval, isc_time_seconds(t2), isc_time_nanoseconds(t2));
to just:
isc_time_t *t1;
isc_interval_t *interval;
isc_time_t *t2;
isc_time_subtract(t1, t2, interval);
without introducing a whole set of new functions.
2022-03-13 12:13:11 +01:00
|
|
|
|
2022-11-04 12:02:33 +00:00
|
|
|
return ((t->seconds * MS_PER_SEC) + (t->nanoseconds / NS_PER_MS));
|
Implement isc_interval_t on top of isc_time_t
Change the isc_interval_t implementation from separate data type and
separate implementation to be shim implementation on top of isc_time_t.
The distinction between isc_interval_t and isc_time_t has been kept
because they are semantically different - isc_interval_t is relative and
isc_time_t is absolute, but this allows isc_time_t and isc_interval_t to
be freely interchangeable, f.e. this:
isc_time_t *t1;
isc_interval_t *interval;
isc_time_t *t2;
isc_interval_set(interval, isc_time_seconds(t2), isc_time_nanoseconds(t2);;
isc_time_subtract(t1, interval, t2);
isc_interval_set(interval, isc_time_seconds(t2), isc_time_nanoseconds(t2));
to just:
isc_time_t *t1;
isc_interval_t *interval;
isc_time_t *t2;
isc_time_subtract(t1, t2, interval);
without introducing a whole set of new functions.
2022-03-13 12:13:11 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
struct tm tm;
|
2001-08-31 21:51:27 +00:00
|
|
|
|
2018-01-02 10:48:08 +11:00
|
|
|
REQUIRE(t != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2018-01-02 10:48:08 +11:00
|
|
|
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
|
|
|
flen = strftime(buf, len, "%d-%b-%Y %X", localtime_r(&now, &tm));
|
2001-09-05 17:05:47 +00:00
|
|
|
INSIST(flen < len);
|
|
|
|
if (flen != 0) {
|
|
|
|
snprintf(buf + flen, len - flen, ".%03u",
|
2016-01-07 15:34:14 -08:00
|
|
|
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
|
|
|
struct tm tm;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2018-01-02 10:48:08 +11:00
|
|
|
REQUIRE(t != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2018-01-02 10:48:08 +11:00
|
|
|
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
|
|
|
|
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);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
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);
|
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
|
|
|
|
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
|
|
|
struct tm tm;
|
2016-11-22 23:34:47 -08:00
|
|
|
|
2018-01-02 10:57:31 +11:00
|
|
|
REQUIRE(t != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2018-01-02 10:57:31 +11:00
|
|
|
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
|
|
|
|
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
|
|
|
struct tm tm;
|
2016-11-22 23:34:47 -08:00
|
|
|
|
2018-01-02 10:57:31 +11:00
|
|
|
REQUIRE(t != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2018-01-02 10:57:31 +11:00
|
|
|
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);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2020-09-29 14:58:56 +10:00
|
|
|
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
|
|
|
|
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
|
|
|
struct tm tm;
|
2006-12-21 06:03:37 +00:00
|
|
|
|
2018-01-02 10:48:08 +11:00
|
|
|
REQUIRE(t != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2018-01-02 10:48:08 +11:00
|
|
|
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
|
|
|
|
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
|
|
|
struct tm tm;
|
2016-01-07 15:34:14 -08:00
|
|
|
|
2018-01-02 10:48:08 +11:00
|
|
|
REQUIRE(t != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2018-01-02 10:48:08 +11:00
|
|
|
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);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2020-09-29 14:58:56 +10:00
|
|
|
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
|
|
|
|
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
|
|
|
struct tm tm;
|
2017-03-08 23:20:40 -08:00
|
|
|
|
2018-01-04 09:57:40 +11:00
|
|
|
REQUIRE(t != NULL);
|
2022-11-04 12:02:33 +00:00
|
|
|
INSIST(t->nanoseconds < NS_PER_SEC);
|
2018-01-04 09:57:40 +11:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|