blob: 24e8a3f41c4052965e31836baa4106b73d3d2249 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
}
|