Provide isc_stdtime_now(void) that returns value

As isc_stdtime_get() cannot fail, the API seems to be too complicated,
add new isc_stdtime_now() that returns the unixtime as a return value.
This commit is contained in:
Ondřej Surý 2023-03-30 21:04:43 +02:00
parent 3e106b3a8f
commit c11af0448a
No known key found for this signature in database
GPG key ID: 2820F37E873DEA41
2 changed files with 15 additions and 21 deletions

View file

@ -19,6 +19,7 @@
#include <stdlib.h>
#include <isc/lang.h>
#include <isc/time.h>
/*%
* It's public information that 'isc_stdtime_t' is an unsigned integral type.
@ -29,16 +30,19 @@ typedef uint32_t isc_stdtime_t;
ISC_LANG_BEGINDECLS
/* */
void
isc_stdtime_get(isc_stdtime_t *t);
isc_stdtime_t
isc_stdtime_now(void);
/*%<
* Set 't' to the number of seconds since 00:00:00 UTC, January 1, 1970.
*
* Requires:
*
*\li 't' is a valid pointer.
* Return the number of seconds since 00:00:00 UTC, January 1, 1970.
*/
/* Compatibility macro */
#define isc_stdtime_get(tp) \
{ \
REQUIRE(tp != NULL); \
*tp = isc_stdtime_now(); \
}
void
isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen);
/*
@ -53,9 +57,4 @@ isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen);
* 'outlen' is at least 26.
*/
#define isc_stdtime_convert32(t, t32p) (*(t32p) = t)
/*
* Convert the standard time to its 32-bit version.
*/
ISC_LANG_ENDDECLS

View file

@ -33,19 +33,16 @@
#define CLOCKSOURCE CLOCK_REALTIME
#endif /* if defined(CLOCK_REALTIME_COARSE) */
void
isc_stdtime_get(isc_stdtime_t *t) {
REQUIRE(t != NULL);
isc_stdtime_t
isc_stdtime_now(void) {
struct timespec ts;
if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
FATAL_SYSERROR(errno, "clock_gettime()");
}
INSIST(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_SEC);
REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_SEC);
*t = (isc_stdtime_t)ts.tv_sec;
return ((isc_stdtime_t)ts.tv_sec);
}
void
@ -55,8 +52,6 @@ isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen) {
REQUIRE(out != NULL);
REQUIRE(outlen >= 26);
UNUSED(outlen);
/* time_t and isc_stdtime_t might be different sizes */
when = t;
INSIST((ctime_r(&when, out) != NULL));