diff options
author | June McEnroe <june@causal.agency> | 2020-10-20 20:55:30 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-10-20 20:56:06 -0400 |
commit | d90a43d948c3b9ed96f2908a58945939cbc30cf0 (patch) | |
tree | 4b5b7bd2e4a6842d50a0a89bef22d099c2498ae7 | |
parent | Humanize restart interval (diff) | |
download | catsit-d90a43d948c3b9ed96f2908a58945939cbc30cf0.tar.gz catsit-d90a43d948c3b9ed96f2908a58945939cbc30cf0.zip |
Humanize milliseconds if interval is less than 1s
The intervals are configurable in milliseconds so humanize should be able to display at that precision.
Diffstat (limited to '')
-rw-r--r-- | service.c | 20 |
1 files 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]; |