From d90a43d948c3b9ed96f2908a58945939cbc30cf0 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Tue, 20 Oct 2020 20:55:30 -0400 Subject: Humanize milliseconds if interval is less than 1s The intervals are configurable in milliseconds so humanize should be able to display at that precision. --- service.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/service.c b/service.c index 5f817bf..4aaad54 100644 --- a/service.c +++ b/service.c @@ -127,18 +127,22 @@ void serviceDrop(size_t index) { services.ptr[index] = services.ptr[--services.len]; } -static const char *humanize(struct timespec uptime) { +static const char *humanize(struct timespec interval) { static char buf[256]; - int days = uptime.tv_sec / (24 * 60 * 60); - uptime.tv_sec %= (24 * 60 * 60); - int hours = uptime.tv_sec / (60 * 60); - uptime.tv_sec %= (60 * 60); - int mins = uptime.tv_sec / 60; - uptime.tv_sec %= 60; + if (!interval.tv_sec) { + snprintf(buf, sizeof(buf), "%dms", (int)(interval.tv_nsec / 1000000)); + return buf; + } + int days = interval.tv_sec / (24 * 60 * 60); + interval.tv_sec %= (24 * 60 * 60); + int hours = interval.tv_sec / (60 * 60); + interval.tv_sec %= (60 * 60); + int mins = interval.tv_sec / 60; + interval.tv_sec %= 60; int d, h, m, s; snprintf( buf, sizeof(buf), "%n%dd %n%dh %n%dm %n%ds", - &d, days, &h, hours, &m, mins, &s, (int)uptime.tv_sec + &d, days, &h, hours, &m, mins, &s, (int)interval.tv_sec ); if (days) return &buf[d]; if (hours) return &buf[h]; -- cgit 1.4.1