feat: format elapsed time as human-readable d/h/m/s duration
This commit is contained in:
+30
-3
@@ -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;
|
||||
|
||||
+30
-3
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user