diff options
author | Thomas Voss <mail@thomasvoss.com> | 2025-09-06 15:55:00 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2025-09-06 15:55:00 +0200 |
commit | 2dba229615f7a3037f9eeb49fcf345c904bd44b0 (patch) | |
tree | 3721ba19be8ad50d39d5b2fb92876476d8be30aa /main.c | |
parent | 7ff668f9ea1aec3e4d5ca42c674fe16f3320b47d (diff) |
Get time with clock_gettime()
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 16 |
1 files changed, 7 insertions, 9 deletions
@@ -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, }; |