From: Michael Tremer Date: Mon, 10 Feb 2025 17:30:42 +0000 (+0000) Subject: strings: Refactor formatting time X-Git-Tag: 0.9.30~47 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=de40166daacc960a93d4835293803d605f14da1a;p=pakfire.git strings: Refactor formatting time The previous function was generating wrong outputs, and strtime() should be used for timestamps instead of deltas. Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/string.c b/src/pakfire/string.c index 09aa6037..0563f455 100644 --- a/src/pakfire/string.c +++ b/src/pakfire/string.c @@ -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 = {};