1*4afad4b7Schristos /* $NetBSD: stdtime.c,v 1.1 2024/02/18 20:57:57 christos Exp $ */
2*4afad4b7Schristos
3*4afad4b7Schristos /*
4*4afad4b7Schristos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5*4afad4b7Schristos *
6*4afad4b7Schristos * SPDX-License-Identifier: MPL-2.0
7*4afad4b7Schristos *
8*4afad4b7Schristos * This Source Code Form is subject to the terms of the Mozilla Public
9*4afad4b7Schristos * License, v. 2.0. If a copy of the MPL was not distributed with this
10*4afad4b7Schristos * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11*4afad4b7Schristos *
12*4afad4b7Schristos * See the COPYRIGHT file distributed with this work for additional
13*4afad4b7Schristos * information regarding copyright ownership.
14*4afad4b7Schristos */
15*4afad4b7Schristos
16*4afad4b7Schristos /*! \file */
17*4afad4b7Schristos
18*4afad4b7Schristos #include <errno.h>
19*4afad4b7Schristos #include <stdbool.h>
20*4afad4b7Schristos #include <stddef.h> /* NULL */
21*4afad4b7Schristos #include <stdlib.h> /* NULL */
22*4afad4b7Schristos #include <syslog.h>
23*4afad4b7Schristos #include <time.h>
24*4afad4b7Schristos
25*4afad4b7Schristos #include <isc/stdtime.h>
26*4afad4b7Schristos #include <isc/strerr.h>
27*4afad4b7Schristos #include <isc/util.h>
28*4afad4b7Schristos
29*4afad4b7Schristos #define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
30*4afad4b7Schristos
31*4afad4b7Schristos #if defined(CLOCK_REALTIME_COARSE)
32*4afad4b7Schristos #define CLOCKSOURCE CLOCK_REALTIME_COARSE
33*4afad4b7Schristos #elif defined(CLOCK_REALTIME_FAST)
34*4afad4b7Schristos #define CLOCKSOURCE CLOCK_REALTIME_FAST
35*4afad4b7Schristos #else /* if defined(CLOCK_REALTIME_COARSE) */
36*4afad4b7Schristos #define CLOCKSOURCE CLOCK_REALTIME
37*4afad4b7Schristos #endif /* if defined(CLOCK_REALTIME_COARSE) */
38*4afad4b7Schristos
39*4afad4b7Schristos void
isc_stdtime_get(isc_stdtime_t * t)40*4afad4b7Schristos isc_stdtime_get(isc_stdtime_t *t) {
41*4afad4b7Schristos REQUIRE(t != NULL);
42*4afad4b7Schristos
43*4afad4b7Schristos struct timespec ts;
44*4afad4b7Schristos
45*4afad4b7Schristos if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
46*4afad4b7Schristos char strbuf[ISC_STRERRORSIZE];
47*4afad4b7Schristos strerror_r(errno, strbuf, sizeof(strbuf));
48*4afad4b7Schristos isc_error_fatal(__FILE__, __LINE__, "clock_gettime failed: %s",
49*4afad4b7Schristos strbuf);
50*4afad4b7Schristos }
51*4afad4b7Schristos
52*4afad4b7Schristos REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_S);
53*4afad4b7Schristos
54*4afad4b7Schristos *t = (isc_stdtime_t)ts.tv_sec;
55*4afad4b7Schristos }
56*4afad4b7Schristos
57*4afad4b7Schristos void
isc_stdtime_tostring(isc_stdtime_t t,char * out,size_t outlen)58*4afad4b7Schristos isc_stdtime_tostring(isc_stdtime_t t, char *out, size_t outlen) {
59*4afad4b7Schristos time_t when;
60*4afad4b7Schristos
61*4afad4b7Schristos REQUIRE(out != NULL);
62*4afad4b7Schristos REQUIRE(outlen >= 26);
63*4afad4b7Schristos
64*4afad4b7Schristos UNUSED(outlen);
65*4afad4b7Schristos
66*4afad4b7Schristos /* time_t and isc_stdtime_t might be different sizes */
67*4afad4b7Schristos when = t;
68*4afad4b7Schristos INSIST((ctime_r(&when, out) != NULL));
69*4afad4b7Schristos *(out + strlen(out) - 1) = '\0';
70*4afad4b7Schristos }
71