|
|
594167 |
From 5f59cc1593eaa251161061fe9a4ac4afb1592e6e Mon Sep 17 00:00:00 2001
|
|
|
594167 |
From: Frantisek Sumsal <frantisek@sumsal.cz>
|
|
|
594167 |
Date: Mon, 21 Feb 2022 13:08:20 +0100
|
|
|
594167 |
Subject: [PATCH] time-util: introduce TIMESTAMP_UNIX
|
|
|
594167 |
|
|
|
594167 |
Allow formatting timestamps as number of seconds since the Epoch for easier
|
|
|
594167 |
machine parsing.
|
|
|
594167 |
|
|
|
594167 |
Fixes: #22567
|
|
|
594167 |
|
|
|
594167 |
```
|
|
|
594167 |
$ systemctl show systemd-journald | grep Timestamp
|
|
|
594167 |
WatchdogTimestampMonotonic=0
|
|
|
594167 |
ExecMainStartTimestamp=Sat 2021-12-11 15:25:57 CET
|
|
|
594167 |
ExecMainStartTimestampMonotonic=13030408
|
|
|
594167 |
ExecMainExitTimestampMonotonic=0
|
|
|
594167 |
StateChangeTimestamp=Sat 2021-12-11 15:25:57 CET
|
|
|
594167 |
StateChangeTimestampMonotonic=13049273
|
|
|
594167 |
InactiveExitTimestamp=Sat 2021-12-11 15:25:57 CET
|
|
|
594167 |
InactiveExitTimestampMonotonic=13030430
|
|
|
594167 |
ActiveEnterTimestamp=Sat 2021-12-11 15:25:57 CET
|
|
|
594167 |
ActiveEnterTimestampMonotonic=13049273
|
|
|
594167 |
ActiveExitTimestamp=Sat 2021-12-11 15:25:57 CET
|
|
|
594167 |
ActiveExitTimestampMonotonic=12997236
|
|
|
594167 |
InactiveEnterTimestamp=Sat 2021-12-11 15:25:57 CET
|
|
|
594167 |
InactiveEnterTimestampMonotonic=13028890
|
|
|
594167 |
ConditionTimestamp=Sat 2021-12-11 15:25:57 CET
|
|
|
594167 |
ConditionTimestampMonotonic=13029539
|
|
|
594167 |
AssertTimestamp=Sat 2021-12-11 15:25:57 CET
|
|
|
594167 |
AssertTimestampMonotonic=13029540
|
|
|
594167 |
|
|
|
594167 |
$ systemctl show --timestamp=unix systemd-journald | grep Timestamp
|
|
|
594167 |
WatchdogTimestampMonotonic=0
|
|
|
594167 |
ExecMainStartTimestamp=@1639232757
|
|
|
594167 |
ExecMainStartTimestampMonotonic=13030408
|
|
|
594167 |
ExecMainExitTimestampMonotonic=0
|
|
|
594167 |
StateChangeTimestamp=@1639232757
|
|
|
594167 |
StateChangeTimestampMonotonic=13049273
|
|
|
594167 |
InactiveExitTimestamp=@1639232757
|
|
|
594167 |
InactiveExitTimestampMonotonic=13030430
|
|
|
594167 |
ActiveEnterTimestamp=@1639232757
|
|
|
594167 |
ActiveEnterTimestampMonotonic=13049273
|
|
|
594167 |
ActiveExitTimestamp=@1639232757
|
|
|
594167 |
ActiveExitTimestampMonotonic=12997236
|
|
|
594167 |
InactiveEnterTimestamp=@1639232757
|
|
|
594167 |
InactiveEnterTimestampMonotonic=13028890
|
|
|
594167 |
ConditionTimestamp=@1639232757
|
|
|
594167 |
ConditionTimestampMonotonic=13029539
|
|
|
594167 |
AssertTimestamp=@1639232757
|
|
|
594167 |
AssertTimestampMonotonic=13029540
|
|
|
594167 |
```
|
|
|
594167 |
|
|
|
594167 |
(cherry picked from commit ed4a5b434517eeebc508379476cf112704e7981c)
|
|
|
594167 |
|
|
|
594167 |
Related: #2017035
|
|
|
594167 |
---
|
|
|
594167 |
src/basic/time-util.c | 11 +++++++++++
|
|
|
594167 |
src/basic/time-util.h | 1 +
|
|
|
594167 |
src/test/test-time-util.c | 5 +++++
|
|
|
594167 |
3 files changed, 17 insertions(+)
|
|
|
594167 |
|
|
|
594167 |
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
|
|
|
594167 |
index b659d6905d..c0841af8f3 100644
|
|
|
594167 |
--- a/src/basic/time-util.c
|
|
|
594167 |
+++ b/src/basic/time-util.c
|
|
|
594167 |
@@ -320,11 +320,13 @@ char *format_timestamp_style(
|
|
|
594167 |
time_t sec;
|
|
|
594167 |
size_t n;
|
|
|
594167 |
bool utc = false, us = false;
|
|
|
594167 |
+ int r;
|
|
|
594167 |
|
|
|
594167 |
assert(buf);
|
|
|
594167 |
|
|
|
594167 |
switch (style) {
|
|
|
594167 |
case TIMESTAMP_PRETTY:
|
|
|
594167 |
+ case TIMESTAMP_UNIX:
|
|
|
594167 |
break;
|
|
|
594167 |
case TIMESTAMP_US:
|
|
|
594167 |
us = true;
|
|
|
594167 |
@@ -350,6 +352,14 @@ char *format_timestamp_style(
|
|
|
594167 |
if (t <= 0 || t == USEC_INFINITY)
|
|
|
594167 |
return NULL; /* Timestamp is unset */
|
|
|
594167 |
|
|
|
594167 |
+ if (style == TIMESTAMP_UNIX) {
|
|
|
594167 |
+ r = snprintf(buf, l, "@" USEC_FMT, t / USEC_PER_SEC); /* round down µs → s */
|
|
|
594167 |
+ if (r < 0 || (size_t) r >= l)
|
|
|
594167 |
+ return NULL; /* Doesn't fit */
|
|
|
594167 |
+
|
|
|
594167 |
+ return buf;
|
|
|
594167 |
+ }
|
|
|
594167 |
+
|
|
|
594167 |
/* Let's not format times with years > 9999 */
|
|
|
594167 |
if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) {
|
|
|
594167 |
assert(l >= STRLEN("--- XXXX-XX-XX XX:XX:XX") + 1);
|
|
|
594167 |
@@ -1632,6 +1642,7 @@ static const char* const timestamp_style_table[_TIMESTAMP_STYLE_MAX] = {
|
|
|
594167 |
[TIMESTAMP_US] = "us",
|
|
|
594167 |
[TIMESTAMP_UTC] = "utc",
|
|
|
594167 |
[TIMESTAMP_US_UTC] = "us+utc",
|
|
|
594167 |
+ [TIMESTAMP_UNIX] = "unix",
|
|
|
594167 |
};
|
|
|
594167 |
|
|
|
594167 |
/* Use the macro for enum → string to allow for aliases */
|
|
|
594167 |
diff --git a/src/basic/time-util.h b/src/basic/time-util.h
|
|
|
594167 |
index 895af88299..01a72026e3 100644
|
|
|
594167 |
--- a/src/basic/time-util.h
|
|
|
594167 |
+++ b/src/basic/time-util.h
|
|
|
594167 |
@@ -34,6 +34,7 @@ typedef enum TimestampStyle {
|
|
|
594167 |
TIMESTAMP_US,
|
|
|
594167 |
TIMESTAMP_UTC,
|
|
|
594167 |
TIMESTAMP_US_UTC,
|
|
|
594167 |
+ TIMESTAMP_UNIX,
|
|
|
594167 |
_TIMESTAMP_STYLE_MAX,
|
|
|
594167 |
_TIMESTAMP_STYLE_INVALID = -EINVAL,
|
|
|
594167 |
} TimestampStyle;
|
|
|
594167 |
diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c
|
|
|
594167 |
index 554693834b..799d271a44 100644
|
|
|
594167 |
--- a/src/test/test-time-util.c
|
|
|
594167 |
+++ b/src/test/test-time-util.c
|
|
|
594167 |
@@ -325,6 +325,11 @@ TEST(format_timestamp) {
|
|
|
594167 |
assert_se(parse_timestamp(buf, &y) >= 0);
|
|
|
594167 |
assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
|
|
|
594167 |
|
|
|
594167 |
+ assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UNIX));
|
|
|
594167 |
+ log_debug("%s", buf);
|
|
|
594167 |
+ assert_se(parse_timestamp(buf, &y) >= 0);
|
|
|
594167 |
+ assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
|
|
|
594167 |
+
|
|
|
594167 |
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UTC));
|
|
|
594167 |
log_debug("%s", buf);
|
|
|
594167 |
assert_se(parse_timestamp(buf, &y) >= 0);
|