]> git.ipfire.org Git - pakfire.git/commitdiff
strings: Refactor formatting time
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 17:30:42 +0000 (17:30 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Feb 2025 17:30:42 +0000 (17:30 +0000)
The previous function was generating wrong outputs, and strtime() should
be used for timestamps instead of deltas.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/string.c

index 09aa6037cc1982cf054432111f6706bfae0c2a42..0563f4555a92efe4ce36b4eaf86f722c3612814a 100644 (file)
@@ -651,6 +651,7 @@ int __pakfire_strftime_now(char* buffer, size_t length, const char* format) {
 
        return __pakfire_strftime(buffer, length, format, t);
 }
+#pragma GCC diagnostic pop
 
 int __pakfire_format_time(char* buffer, const size_t length, const time_t t) {
        const char* format = NULL;
@@ -659,18 +660,20 @@ int __pakfire_format_time(char* buffer, const size_t length, const time_t t) {
        if (t < 0)
                return -EINVAL;
 
-       if (t >= 86400)
-               format = "%dd%Hh%Mm";
-       else if (t >= 3600)
-               format = "%Hh%Mm%Ss";
-       else if (t >= 60)
-               format = "%Mm%Ss";
+       int d = t / 86400;
+       int h = t % 86400 / 3600;
+       int m = t % 3600 / 60;
+       int s = t % 60;
+
+       if (d)
+               return __pakfire_string_format(buffer, length, "%dd%dh%dm", d, h, m);
+       else if (h)
+               return __pakfire_string_format(buffer, length, "%dh%dm%ds", h, m, s);
+       else if (m)
+               return __pakfire_string_format(buffer, length, "%dm%ds", m, s);
        else
-               format = "%Ss";
-
-       return __pakfire_strftime(buffer, length, format, t);
+               return __pakfire_string_format(buffer, length, "%ds", s);
 }
-#pragma GCC diagnostic pop
 
 int __pakfire_timeval_to_iso8601(char* buffer, const size_t length, const struct timeval* t) {
        struct tm tm = {};