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