From: Ben Harris Date: Sat, 3 Nov 2018 14:25:04 +0000 (+0000) Subject: Start keeping track of what time we think the clock is displaying. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=8f0ee44833ee4b77ec1f1d32beaa1235a9cf933f;p=clunk.git Start keeping track of what time we think the clock is displaying. --- diff --git a/clunk.c b/clunk.c index c9b77d5..f7e7271 100644 --- a/clunk.c +++ b/clunk.c @@ -3,7 +3,9 @@ #include #include -long const pulsewidth = 250000000; /* nanoseconds */ +static long const pulsewidth = 250000000; /* nanoseconds */ + +static struct tm displayed; static void pulse() @@ -13,8 +15,35 @@ pulse() printf("ker..."); fflush(stdout); clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL); - printf("chunk!\n"); + printf("chunk!"); + displayed.tm_sec += 30; + while (displayed.tm_sec >= 60) { + displayed.tm_min++; + displayed.tm_sec -= 60; + } + while (displayed.tm_min >= 60) { + displayed.tm_hour++; + displayed.tm_min -= 60; + } + displayed.tm_hour %= 12; + printf(" (%02d:%02d:%02d)\n", + displayed.tm_hour, displayed.tm_min, displayed.tm_sec); + fflush(stdout); +} + +static void +init() +{ + struct timespec ts; + + tzset(); + if (clock_gettime(CLOCK_REALTIME, &ts) != 0) + err (1, "clock_gettime"); + if (localtime_r(&ts.tv_sec, &displayed) == NULL) + err(1, "localtime_r"); + displayed.tm_sec = (displayed.tm_sec >= 30) ? 30 : 0; } + static void run() @@ -44,7 +73,7 @@ int main(int argc, char **argv) { - tzset(); + init(); run(); return 0; }