diff --git a/src/util.c b/src/util.c index fada69c..1639544 100644 --- a/src/util.c +++ b/src/util.c @@ -1960,13 +1960,11 @@ char *format_timespan(char *buf, size_t l, usec_t t) { { "d", USEC_PER_DAY }, { "h", USEC_PER_HOUR }, { "min", USEC_PER_MINUTE }, - { "s", USEC_PER_SEC }, - { "ms", USEC_PER_MSEC }, - { "us", 1 }, }; unsigned i; char *p = buf; + int k; assert(buf); assert(l > 0); @@ -1983,7 +1981,6 @@ char *format_timespan(char *buf, size_t l, usec_t t) { /* The result of this function can be parsed with parse_usec */ for (i = 0; i < ELEMENTSOF(table); i++) { - int k; size_t n; if (t < table[i].usec) @@ -2001,6 +1998,10 @@ char *format_timespan(char *buf, size_t l, usec_t t) { t %= table[i].usec; } + /* We have left a value between 0 and 59,999,999 (that is, 0s and 59.999999s) */ + k = snprintf(p, l, "%s%llu.%06llus", p > buf ? " " : "", (unsigned long long) (t / USEC_PER_SEC), (unsigned long long) (t % USEC_PER_SEC)); + p += MIN((size_t) k, l); + *p = 0; return buf;