diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-02-21 16:13:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-21 16:13:47 +0100 |
commit | 67e993b9e0edf50a03d32ee79ecee57cfb1349fe (patch) | |
tree | de7979e24ac2850b45ffca91fa8b955a893577d3 /src | |
parent | 521da4646ef05cbb344d6ea3b87268d4c5ffc441 (diff) | |
parent | b58b4a9f379748fec667fb60606de945eaafadbe (diff) | |
download | systemd-67e993b9e0edf50a03d32ee79ecee57cfb1349fe.tar.gz |
Merge pull request #22573 from mrc0mmand/epoch-timestamp
time-util: introduce TIMESTAMP_UNIX
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/time-util.c | 11 | ||||
-rw-r--r-- | src/basic/time-util.h | 1 | ||||
-rw-r--r-- | src/systemctl/systemctl.c | 7 | ||||
-rw-r--r-- | src/test/test-time-util.c | 5 |
4 files changed, 19 insertions, 5 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c index b659d6905d..c0841af8f3 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -320,11 +320,13 @@ char *format_timestamp_style( time_t sec; size_t n; bool utc = false, us = false; + int r; assert(buf); switch (style) { case TIMESTAMP_PRETTY: + case TIMESTAMP_UNIX: break; case TIMESTAMP_US: us = true; @@ -350,6 +352,14 @@ char *format_timestamp_style( if (t <= 0 || t == USEC_INFINITY) return NULL; /* Timestamp is unset */ + if (style == TIMESTAMP_UNIX) { + r = snprintf(buf, l, "@" USEC_FMT, t / USEC_PER_SEC); /* round down µs → s */ + if (r < 0 || (size_t) r >= l) + return NULL; /* Doesn't fit */ + + return buf; + } + /* Let's not format times with years > 9999 */ if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) { assert(l >= STRLEN("--- XXXX-XX-XX XX:XX:XX") + 1); @@ -1632,6 +1642,7 @@ static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = { [TIMESTAMP_US] = "us", [TIMESTAMP_UTC] = "utc", [TIMESTAMP_US_UTC] = "us+utc", + [TIMESTAMP_UNIX] = "unix", }; /* Use the macro for enum → string to allow for aliases */ diff --git a/src/basic/time-util.h b/src/basic/time-util.h index 895af88299..01a72026e3 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -34,6 +34,7 @@ typedef enum TimestampStyle { TIMESTAMP_US, TIMESTAMP_UTC, TIMESTAMP_US_UTC, + TIMESTAMP_UNIX, _TIMESTAMP_STYLE_MAX, _TIMESTAMP_STYLE_INVALID = -EINVAL, } TimestampStyle; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 9031e685ea..0489796a75 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -296,11 +296,8 @@ static int systemctl_help(void) { " --boot-loader-entry=NAME\n" " Boot into a specific boot loader entry on next boot\n" " --plain Print unit dependencies as a list instead of a tree\n" - " --timestamp=FORMAT Change format of printed timestamps.\n" - " 'pretty' (default): 'Day YYYY-MM-DD HH:MM:SS TZ\n" - " 'us': 'Day YYYY-MM-DD HH:MM:SS.UUUUUU TZ\n" - " 'utc': 'Day YYYY-MM-DD HH:MM:SS UTC\n" - " 'us+utc': 'Day YYYY-MM-DD HH:MM:SS.UUUUUU UTC\n" + " --timestamp=FORMAT Change format of printed timestamps (pretty, unix,\n" + " us, utc, us+utc)\n" " --read-only Create read-only bind mount\n" " --mkdir Create directory before mounting, if missing\n" " --marked Restart/reload previously marked units\n" diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index 554693834b..799d271a44 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -325,6 +325,11 @@ TEST(format_timestamp) { assert_se(parse_timestamp(buf, &y) >= 0); assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); + assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UNIX)); + log_debug("%s", buf); + assert_se(parse_timestamp(buf, &y) >= 0); + assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); + assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UTC)); log_debug("%s", buf); assert_se(parse_timestamp(buf, &y) >= 0); |