diff options
-rw-r--r-- | service.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/service.c b/service.c index 6718b06..4ea9126 100644 --- a/service.c +++ b/service.c @@ -133,21 +133,23 @@ static const char *humanize(struct timespec interval) { snprintf(buf, sizeof(buf), "%dms", (int)(interval.tv_nsec / 1000000)); return buf; } - int days = interval.tv_sec / (24 * 60 * 60); + int d = interval.tv_sec / (24 * 60 * 60); interval.tv_sec %= (24 * 60 * 60); - int hours = interval.tv_sec / (60 * 60); + int h = interval.tv_sec / (60 * 60); interval.tv_sec %= (60 * 60); - int mins = interval.tv_sec / 60; + int m = 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)interval.tv_sec - ); - if (days) return &buf[d]; - if (hours) return &buf[h]; - if (mins) return &buf[m]; - return &buf[s]; + int s = interval.tv_sec; + if (d) { + snprintf(buf, sizeof(buf), "%dd %dh %dm %ds", d, h, m, s); + } else if (h) { + snprintf(buf, sizeof(buf), "%dh %dm %ds", h, m, s); + } else if (m) { + snprintf(buf, sizeof(buf), "%dm %ds", m, s); + } else { + snprintf(buf, sizeof(buf), "%ds", s); + } + return buf; } void serviceStatus(struct Service *service) { |