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