diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-01-20 13:44:26 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-01-20 17:42:55 +0100 |
commit | 4361678c4b88af10333100a3ae3cf3b7b5825bdd (patch) | |
tree | 840cecde38a39748d99fa70f0d5bc07fad5385ad /src | |
parent | 060c9c02d8231b8b5780a147c61b4f1769b1be72 (diff) | |
download | systemd-4361678c4b88af10333100a3ae3cf3b7b5825bdd.tar.gz |
time-util: if a date is unrepresentable, honour style to generate XXX string
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/time-util.c | 12 | ||||
-rw-r--r-- | src/test/test-time-util.c | 4 |
2 files changed, 11 insertions, 5 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 197c485552..f427205f0d 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 6b546fb9f5..5c415b07b8 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); |