From: Lennart Poettering Date: Fri, 20 Jan 2023 12:44:26 +0000 (+0100) Subject: time-util: if a date is unrepresentable, honour style to generate XXX string X-Git-Tag: v253-rc1~52^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4361678c4b88af10333100a3ae3cf3b7b5825bdd;p=thirdparty%2Fsystemd.git time-util: if a date is unrepresentable, honour style to generate XXX string --- diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 197c4855524..f427205f0d9 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -340,9 +340,15 @@ char *format_timestamp_style( /* Let's not format times with years > 9999 */ if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) { - assert(l >= STRLEN("--- XXXX-XX-XX XX:XX:XX") + 1); - strcpy(buf, "--- XXXX-XX-XX XX:XX:XX"); - return buf; + static const char* const xxx[_TIMESTAMP_STYLE_MAX] = { + [TIMESTAMP_PRETTY] = "--- XXXX-XX-XX XX:XX:XX", + [TIMESTAMP_US] = "--- XXXX-XX-XX XX:XX:XX.XXXXXX", + [TIMESTAMP_UTC] = "--- XXXX-XX-XX XX:XX:XX UTC", + [TIMESTAMP_US_UTC] = "--- XXXX-XX-XX XX:XX:XX.XXXXXX UTC", + }; + + assert(l >= strlen(xxx[style]) + 1); + return strcpy(buf, xxx[style]); } sec = (time_t) (t / USEC_PER_SEC); /* Round down */ diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index 6b546fb9f57..5c415b07b80 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -481,10 +481,10 @@ TEST(format_timestamp_utc) { #if SIZEOF_TIME_T == 8 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Thu 9999-12-30 23:59:59 UTC"); - test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX"); + test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX UTC"); #elif SIZEOF_TIME_T == 4 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Tue 2038-01-19 03:14:07 UTC"); - test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX"); + test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX UTC"); #endif test_format_timestamp_utc_one(USEC_INFINITY, NULL);