time-util: add timestamp output style that shows dates only, no times

This commit is contained in:
Lennart Poettering
2023-01-20 17:45:40 +01:00
parent 9b9ea806ad
commit 64f3419ec1
3 changed files with 47 additions and 17 deletions
+16 -6
View File
@@ -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 */
+1
View File
@@ -35,6 +35,7 @@ typedef enum TimestampStyle {
TIMESTAMP_UTC,
TIMESTAMP_US_UTC,
TIMESTAMP_UNIX,
TIMESTAMP_DATE,
_TIMESTAMP_STYLE_MAX,
_TIMESTAMP_STYLE_INVALID = -EINVAL,
} TimestampStyle;