]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
time-util: simplify formatting of UNIX timestamps
authorLennart Poettering <lennart@poettering.net>
Fri, 20 Jan 2023 12:46:40 +0000 (13:46 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 20 Jan 2023 16:44:17 +0000 (17:44 +0100)
src/basic/time-util.c

index 081fa0100408872ab3bafe30859099fc5ba6ac30..dcafad48828ed3f4407283134b50b20367b24ffb 100644 (file)
@@ -311,7 +311,6 @@ char *format_timestamp_style(
         bool utc, us;
         time_t sec;
         size_t n;
-        int r;
 
         assert(buf);
         assert(style >= 0);
@@ -321,11 +320,10 @@ char *format_timestamp_style(
                 return NULL; /* Timestamp is unset */
 
         if (style == TIMESTAMP_UNIX) {
-                r = snprintf(buf, l, "@" USEC_FMT, t / USEC_PER_SEC);  /* round down µs → s */
-                if (r < 0 || (size_t) r >= l)
-                        return NULL; /* Doesn't fit */
+                if (l < (size_t) (1 + 1 + 1))
+                        return NULL; /* not enough space for even the shortest of forms */
 
-                return buf;
+                return snprintf_ok(buf, l, "@" USEC_FMT, t / USEC_PER_SEC);  /* round down µs → s */
         }
 
         utc = IN_SET(style, TIMESTAMP_UTC, TIMESTAMP_US_UTC);