feat: format elapsed time as human-readable d/h/m/s duration

This commit is contained in:
2026-09-15 12:58:45 +02:00
parent 1286fac03f
commit fd34aa1956
2 changed files with 60 additions and 6 deletions
+30 -3
View File
@@ -39,6 +39,29 @@ static double now_seconds(void)
#endif
}
static void format_duration(double secs, char *buf, size_t bufsize)
{
unsigned long total_ms = (unsigned long)(secs * 1000.0 + 0.5);
unsigned long ms = total_ms % 1000;
unsigned long total_s = total_ms / 1000;
unsigned long s = total_s % 60;
unsigned long total_m = total_s / 60;
unsigned long m = total_m % 60;
unsigned long total_h = total_m / 60;
unsigned long h = total_h % 24;
unsigned long d = total_h / 24;
if (d > 0)
sprintf(buf, "%lud %luh %lum %lus", d, h, m, s);
else if (h > 0)
sprintf(buf, "%luh %lum %lus", h, m, s);
else if (m > 0)
sprintf(buf, "%lum %lu.%03lus", m, s, ms);
else
sprintf(buf, "%lu.%03lus", s, ms);
buf[bufsize - 1] = '\0';
}
#define FPACK_QUIET (1u << 0)
#define FPACK_VERBOSE (1u << 1)
#define FPACK_PROGRESS (1u << 2)
@@ -344,9 +367,13 @@ int main(int argc, char **argv)
if (flags & FPACK_PROGRESS)
printf("\n");
if (!(flags & FPACK_QUIET))
printf("wrote: %s (%lu entries) in %.3fs\n", output,
(unsigned long)entry_count, now_seconds() - start_time);
if (!(flags & FPACK_QUIET)) {
char dur[64];
format_duration(now_seconds() - start_time, dur, sizeof(dur));
printf("wrote: %s (%lu entries) in %s\n", output,
(unsigned long)entry_count, dur);
}
free(output);
return 0;