diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-01-20 13:47:11 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-01-20 17:45:40 +0100 |
commit | 64f3419ec1f56a93b6dd48137ca40c945fc06c59 (patch) | |
tree | fdcc0ff0359bd63c28c617512951f31e3ea3d1de /src/basic/time-util.c | |
parent | 9b9ea806ad0ec469ae40c22b34b9e11220f0710a (diff) | |
download | systemd-64f3419ec1f56a93b6dd48137ca40c945fc06c59.tar.gz |
time-util: add timestamp output style that shows dates only, no times
Diffstat (limited to 'src/basic/time-util.c')
-rw-r--r-- | src/basic/time-util.c | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 791d83a5d8..b700f364ef 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -326,14 +326,15 @@ char *format_timestamp_style( return snprintf_ok(buf, l, "@" USEC_FMT, t / USEC_PER_SEC); /* round down µs → s */ } - utc = IN_SET(style, TIMESTAMP_UTC, TIMESTAMP_US_UTC); + utc = IN_SET(style, TIMESTAMP_UTC, TIMESTAMP_US_UTC, TIMESTAMP_DATE); us = IN_SET(style, TIMESTAMP_US, TIMESTAMP_US_UTC); - if (l < (size_t) (3 + /* week day */ - 1 + 10 + /* space and date */ - 1 + 8 + /* space and time */ - (us ? 1 + 6 : 0) + /* "." and microsecond part */ - 1 + (utc ? 3 : 1) + /* space and shortest possible zone */ + if (l < (size_t) (3 + /* week day */ + 1 + 10 + /* space and date */ + style == TIMESTAMP_DATE ? 0 : + (1 + 8 + /* space and time */ + (us ? 1 + 6 : 0) + /* "." and microsecond part */ + 1 + (utc ? 3 : 1)) + /* space and shortest possible zone */ 1)) return NULL; /* Not enough space even for the shortest form. */ @@ -344,6 +345,7 @@ char *format_timestamp_style( [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", + [TIMESTAMP_DATE] = "--- XXXX-XX-XX", }; assert(l >= strlen(xxx[style]) + 1); @@ -359,6 +361,14 @@ char *format_timestamp_style( assert((size_t) tm.tm_wday < ELEMENTSOF(weekdays)); memcpy(buf, weekdays[tm.tm_wday], 4); + if (style == TIMESTAMP_DATE) { + /* Special format string if only date should be shown. */ + if (strftime(buf + 3, l - 3, " %Y-%m-%d", &tm) <= 0) + return NULL; /* Doesn't fit */ + + return buf; + } + /* Add the main components */ if (strftime(buf + 3, l - 3, " %Y-%m-%d %H:%M:%S", &tm) <= 0) return NULL; /* Doesn't fit */ |