summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-01-20 13:44:26 +0100
committerLennart Poettering <lennart@poettering.net>2023-01-20 17:42:55 +0100
commit4361678c4b88af10333100a3ae3cf3b7b5825bdd (patch)
tree840cecde38a39748d99fa70f0d5bc07fad5385ad /src
parent060c9c02d8231b8b5780a147c61b4f1769b1be72 (diff)
downloadsystemd-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.c12
-rw-r--r--src/test/test-time-util.c4
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);