Challenge
Your task is to write a program which, once a second (including immediately when your program is started), prints the elapsed time from the time your program was started.
Rules
- Time must be printed in
hh:mm:ss
format. (leading zeros for single-digit values) - The time stamps must be separated by CR, LF, or CRLF. (no leading whitespace)
- A new time must appear every second. (stdout cannot be buffered for a second)
- The behavior of the program if it is run past 23:59:59 is undefined.
- You may use
sleep(1)
even if a specific second may be skipped whenever the overhead to print, calculate, loop, etc. accumulates to a second.
Example output:
00:00:00
00:00:01
00:00:02
00:00:04
00:00:05
⋮
Note that 00:00:03
is missing here due to processing overhead. The actual skipped values (if any) are of course dependent on implementation and/or system.
Reference implementation in C: (POSIX-compatible systems only)
#include <unistd.h> // sleep()
#include <tgmath.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#ifndef __STDC_IEC_559__
#error "unsupported double"
#endif
static_assert(sizeof(double) == 8, "double must have double precision");
#define MAX_PRECISE_DOUBLE ((double)(1ULL << 52))
int main(void) {
time_t start = time(NULL);
if (start == (time_t)-1) return EXIT_FAILURE;
while (1) {
time_t now = time(NULL);
if (now == (time_t)-1) return EXIT_FAILURE;
double diff = difftime(now, start);
if (isnan(diff) || diff < 0) return EXIT_FAILURE;
if (diff > MAX_PRECISE_DOUBLE) return EXIT_FAILURE;
unsigned long long seconds = diff;
unsigned long long h = seconds / 3600;
seconds %= 3600;
unsigned long long m = seconds / 60;
seconds %= 60;
unsigned long long s = seconds;
(void)printf("\r%02llu:%02llu:%02llu", h, m, s);
(void)fflush(stdout);
(void)sleep(1);
}
}
Winning criteria
This is code-golf, shortest code in bytes win!