From 2dba229615f7a3037f9eeb49fcf345c904bd44b0 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 6 Sep 2025 15:55:00 +0200 Subject: Get time with clock_gettime() --- main.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 1b984d4..7ddc3b9 100644 --- a/main.c +++ b/main.c @@ -85,12 +85,10 @@ main(int argc, char **argv) : /* default */ syncs; for (;;) { - struct timespec before, after; - if (clock_gettime(CLOCK_REALTIME, &before) == -1) + struct timespec now, then; + if (clock_gettime(CLOCK_REALTIME, &now) == -1) warn(_("failed to get the time")); - - time_t now = time(NULL); - struct tm *tm = localtime(&now); + struct tm *tm = localtime(&now.tv_sec); char buf[256]; size_t n = strftime(buf, sizeof(buf), dfmt, tm); @@ -98,13 +96,13 @@ main(int argc, char **argv) warnx(_("buffer too small")); puts(buf); - if (clock_gettime(CLOCK_REALTIME, &after) == -1) + if (clock_gettime(CLOCK_REALTIME, &then) == -1) warn(_("failed to get the time")); /* Duration of the clock formatting and printing */ struct timespec Δ = { - after.tv_sec - before.tv_sec, - after.tv_nsec - before.tv_nsec, + then.tv_sec - now.tv_sec, + then.tv_nsec - now.tv_nsec, }; if (Δ.tv_nsec < 0) { Δ.tv_nsec += NSEC_PER_SEC; @@ -112,7 +110,7 @@ main(int argc, char **argv) } struct timespec rqtp = { - .tv_sec = sync(after.tv_sec), + .tv_sec = sync(then.tv_sec), .tv_nsec = 0, }; -- cgit v1.2.3