/* clock.c - Public Domain - It's a digital clock EXEC: cc -O1 -s -pipe -Wall -Wextra -Wpedantic $CPPFLAGS -o clock clock.c && gzexe clock :STOP */ #include #include #include #ifndef CLEAR # define CLEAR "\033[2J" #endif #ifndef CLEAR_DUMB # define CLEAR_DUMB "\n" #endif #ifndef PERIOD # define PERIOD 5 #endif char * hmstime(struct tm * tm) { static char time[9]; sprintf(time, "%.2d:%.2d:%.2d", tm->tm_hour, tm->tm_min, tm->tm_sec); return time; } int main(void) { struct tm * tm; time_t now; int sec; puts("Type C-c to exit.\n"); for (;;) { time(&now); tm = gmtime(&now); /* Junk character may be printed, this is the time to get a new terminal. */ printf(CLEAR " %s\n" CLEAR_DUMB, hmstime(tm)); sec = tm->tm_sec % PERIOD; sleep(!sec ? PERIOD : PERIOD - sec); } return 0; }