From 8c85de545f2fc998accd7d6f88bebf234c728ce5 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 4 Sep 2020 21:51:38 +0200 Subject: Add leap second support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- config.mk | 2 +- mongoclock.c | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/config.mk b/config.mk index 9e148ca..0911b00 100644 --- a/config.mk +++ b/config.mk @@ -2,5 +2,5 @@ PREFIX = /usr/local MANPREFIX = $(PREFIX)/share/man CFLAGS = -std=c99 -Wall -Wextra -O2 -CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -DUSE_ADJTIMEX LDFLAGS = -s diff --git a/mongoclock.c b/mongoclock.c index cf34e6d..442a380 100644 --- a/mongoclock.c +++ b/mongoclock.c @@ -9,6 +9,9 @@ #include #include #include +#ifdef USE_ADJTIMEX +# include +#endif #include "arg.h" @@ -80,7 +83,6 @@ print_time(const char ***str, size_t y, size_t x) int main(int argc, char *argv[]) { - time_t now_; struct tm *now; const char **digits[9]; size_t x = 0, y = 0; @@ -90,6 +92,12 @@ main(int argc, char *argv[]) struct itimerspec itimerspec; uint64_t _overrun; struct sigaction sigact; +#ifdef USE_ADJTIMEX + struct timex timex; + int r; +#else + time_t now_; +#endif ARGBEGIN { default: @@ -123,6 +131,10 @@ main(int argc, char *argv[]) sigact.sa_handler = sigwinch; sigaction(SIGWINCH, &sigact, NULL); +#ifdef USE_ADJTIMEX + memset(&timex, 0, sizeof(timex)); +#endif + while (!caught_sigterm) { if (caught_sigwinch) { if (ioctl(STDOUT_FILENO, (unsigned long)TIOCGWINSZ, &winsize) < 0) { @@ -152,12 +164,23 @@ main(int argc, char *argv[]) continue; } +#ifdef USE_ADJTIMEX + r = adjtimex(&timex); + if (r == -1) + goto fail; + now = localtime(&timex.time.tv_sec); + if (now == NULL) + goto fail; + if (r == TIME_OOP) + now->tm_sec += 1; +#else now_ = time(NULL); if (now_ == -1) goto fail; now = localtime(&now_); if (now == NULL) goto fail; +#endif digits[0] = mongo_ds[now->tm_hour / 10]; digits[1] = mongo_ds[now->tm_hour % 10]; -- cgit v1.2.3-70-g09d2