From fd34aa1956db826523c1787d189d3b1520469190 Mon Sep 17 00:00:00 2001 From: anth64 Date: Tue, 15 Sep 2026 12:58:45 +0200 Subject: [PATCH] feat: format elapsed time as human-readable d/h/m/s duration --- tools/fpack.c | 33 ++++++++++++++++++++++++++++++--- tools/funpack.c | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/tools/fpack.c b/tools/fpack.c index ea711f8..53ac512 100644 --- a/tools/fpack.c +++ b/tools/fpack.c @@ -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; diff --git a/tools/funpack.c b/tools/funpack.c index 74ccd9a..08ea4bf 100644 --- a/tools/funpack.c +++ b/tools/funpack.c @@ -37,6 +37,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 FUNPACK_LIST (1u << 0) #define FUNPACK_PROGRESS (1u << 1) #define FUNPACK_QUIET (1u << 2) @@ -309,9 +332,13 @@ int main(int argc, char **argv) if ((flags & FUNPACK_PROGRESS) && count > 0) printf("\n"); - if (!(flags & FUNPACK_QUIET)) - printf("unpacked: %s -> %s (%lu entries) in %.3fs\n", input, out_dir, - (unsigned long)count, now_seconds() - start_time); + if (!(flags & FUNPACK_QUIET)) { + char dur[64]; + + format_duration(now_seconds() - start_time, dur, sizeof(dur)); + printf("unpacked: %s -> %s (%lu entries) in %s\n", input, out_dir, + (unsigned long)count, dur); + } free(out_dir); return 0;