chiark / gitweb /
util: tweak format_timespan() a bit
authorLennart Poettering <lennart@poettering.net>
Thu, 4 Apr 2013 15:06:00 +0000 (17:06 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 4 Apr 2013 15:06:00 +0000 (17:06 +0200)
Make sure to always print out at least one valid component instead of
falling back early to 0.

src/shared/time-util.c

index b6a2bec..9ee711a 100644 (file)
@@ -251,6 +251,12 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
         if (t == (usec_t) -1)
                 return NULL;
 
+        if (t <= 0) {
+                snprintf(p, l, "0");
+                p[l-1] = 0;
+                return p;
+        }
+
         /* The result of this function can be parsed with parse_sec */
 
         for (i = 0; i < ELEMENTSOF(table); i++) {
@@ -259,15 +265,11 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
                 bool done = false;
                 usec_t a, b;
 
-                if (t == 0 || t < accuracy) {
-                        if (!something) {
-                                snprintf(p, l, "0");
-                                p[l-1] = 0;
-                                return p;
-                        }
+                if (t <= 0)
+                        break;
 
+                if (t < accuracy && something)
                         break;
-                }
 
                 if (t < table[i].usec)
                         continue;
@@ -322,7 +324,6 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
                 l -= n;
                 p += n;
 
-
                 something = true;
         }