From f160176e2f5f5fcddf264203e3a74693e3775ac7 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Thu, 27 Mar 2025 21:01:40 +0100 Subject: Initial --- .gitignore | 1 + README.md | 4 ++++ build.sh | 3 +++ miniclock.c | 25 +++++++++++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 build.sh create mode 100644 miniclock.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e46d295 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +miniclock diff --git a/README.md b/README.md new file mode 100644 index 0000000..dfb0a6f --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Miniclock + +Tiny clock app, outputs time accurately each second. Meant to be used with +status bar apps like i3-blocks. diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..9371f5a --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +gcc -O3 miniclock.c -o miniclock diff --git a/miniclock.c b/miniclock.c new file mode 100644 index 0000000..24e8a3f --- /dev/null +++ b/miniclock.c @@ -0,0 +1,25 @@ +#include +#include + +int main() { + struct timespec ts = {0}; + struct tm tm = {0}; + + for (;;) { + // print time + clock_gettime(CLOCK_REALTIME, &ts); + localtime_r(&ts.tv_sec, &tm); + printf("%s", asctime(&tm)); + fflush(stdout); + + // delay until next second + struct timespec td = { + .tv_sec = 0, + .tv_nsec = 1000000000 - ts.tv_nsec, + }; + + nanosleep(&td, NULL); + } + + return 0; +} -- cgit v1.2.1