chiark / gitweb /
Start keeping track of what time we think the clock is displaying.
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 3 Nov 2018 14:25:04 +0000 (14:25 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 3 Nov 2018 14:25:04 +0000 (14:25 +0000)
clunk.c

diff --git a/clunk.c b/clunk.c
index c9b77d56046bddc806837adaa0db6125a33ef233..f7e7271975a79b1c1c83ed8131a373d8d9cb02ff 100644 (file)
--- a/clunk.c
+++ b/clunk.c
@@ -3,7 +3,9 @@
 #include <stdio.h>
 #include <time.h>
 
-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;
 }