Clock written in about 5 minutes. Public Domain.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

50 lines
874B

  1. /* clock.c - Public Domain - It's a digital clock
  2. EXEC:
  3. cc -O1 -s -pipe -Wall -Wextra -Wpedantic $CPPFLAGS -o clock clock.c && gzexe clock
  4. :STOP */
  5. #include <time.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #ifndef CLEAR
  9. # define CLEAR "\033[2J"
  10. #endif
  11. #ifndef CLEAR_DUMB
  12. # define CLEAR_DUMB "\n"
  13. #endif
  14. #ifndef PERIOD
  15. # define PERIOD 5
  16. #endif
  17. char * hmstime(struct tm * tm)
  18. {
  19. static char time[9];
  20. sprintf(time, "%.2d:%.2d:%.2d",
  21. tm->tm_hour, tm->tm_min, tm->tm_sec);
  22. return time;
  23. }
  24. int
  25. main(void)
  26. {
  27. struct tm * tm;
  28. time_t now;
  29. int sec;
  30. puts("Type C-c to exit.\n");
  31. for (;;)
  32. {
  33. time(&now);
  34. tm = gmtime(&now);
  35. /* Junk character may be printed, this is the time to get a new terminal. */
  36. printf(CLEAR " %s\n" CLEAR_DUMB, hmstime(tm));
  37. sec = tm->tm_sec % PERIOD;
  38. sleep(!sec ? PERIOD : PERIOD - sec);
  39. }
  40. return 0;
  41. }