Merge pull request #22573 from mrc0mmand/epoch-timestamp

time-util: introduce TIMESTAMP_UNIX
This commit is contained in:
Lennart Poettering
2022-02-21 16:13:47 +01:00
committed by GitHub
5 changed files with 26 additions and 5 deletions
+11
View File
@@ -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 */
+1
View File
@@ -34,6 +34,7 @@ typedef enum TimestampStyle {
TIMESTAMP_US,
TIMESTAMP_UTC,
TIMESTAMP_US_UTC,
TIMESTAMP_UNIX,
_TIMESTAMP_STYLE_MAX,
_TIMESTAMP_STYLE_INVALID = -EINVAL,
} TimestampStyle;