diff options
author | Paul Oliver <contact@pauloliver.dev> | 2025-03-27 21:01:40 +0100 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2025-04-02 21:02:38 +0200 |
commit | f160176e2f5f5fcddf264203e3a74693e3775ac7 (patch) | |
tree | 20c1c4a86236ee86aa277d52b27ec82116a750bc /miniclock.c |
Diffstat (limited to 'miniclock.c')
-rw-r--r-- | miniclock.c | 25 |
1 files changed, 25 insertions, 0 deletions
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 <stdio.h> +#include <time.h> + +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; +} |