From 7cb0868ea8e741330cb1c37d7258e73ca47aa895 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 13 Jan 2023 19:37:12 +0100 Subject: Only use CLOCK_BOOTTIME if available and adjtimex(3) on Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- timeprefix.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'timeprefix.c') diff --git a/timeprefix.c b/timeprefix.c index f76e104..6be5fa6 100644 --- a/timeprefix.c +++ b/timeprefix.c @@ -1,19 +1,34 @@ /* See LICENSE file for copyright and license details. */ -#include +#ifdef __linux__ +# include +# define HAVE_ADJTIMEX +#endif #include #include #include +#ifndef CLOCK_MONOTONIC_RAW +# define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC +#endif +#ifndef CLOCK_BOOTTIME +# define CLOCK_BOOTTIME CLOCK_MONOTONIC_RAW +#endif + static void printline(const char *line) { +#ifdef HAVE_ADJTIMEX static struct timex timex; /* static to zero initialise it */ + int r; +#else + struct timespec walltime; +#endif struct timespec boottime; struct tm *utctime; - int r; clock_gettime(CLOCK_BOOTTIME, &boottime); +#ifdef HAVE_ADJTIMEX r = adjtimex(&timex); if (timex.time.tv_sec % (24 * 60 * 60) == 0) { if (r == TIME_INS) { @@ -32,6 +47,10 @@ printline(const char *line) } else { utctime = gmtime(&timex.time.tv_sec); } +#else + clock_gettime(CLOCK_REALTIME, &walltime); + utctime = gmtime(&walltime.tv_sec); +#endif printf("[%010lu.%04lu %i-%02i-%02i %02i:%02i:%02i UTC] %s", boottime.tv_sec, boottime.tv_nsec / 100000, -- cgit v1.2.3-70-g09d2