summaryrefslogtreecommitdiffstats
path: root/src/shared
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-02-22 23:40:04 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-02-22 23:43:33 +0100
commitd5e6f36c7a5a2702c6bcf750cbe9815ff98cac48 (patch)
tree785c7c6033d55a505aa72f2aef105c5f43ee6abe /src/shared
parenthwdb: fix swapped buttons for Logitech Lift left (diff)
downloadsystemd-d5e6f36c7a5a2702c6bcf750cbe9815ff98cac48.tar.xz
systemd-d5e6f36c7a5a2702c6bcf750cbe9815ff98cac48.zip
shared/format-table: optionally print timestamps without "left"
This just adds the base functionality and some unit tests. With TABLE_TIMESTAMP_RELATIVE we print "5s ago" and "5s left", with the new TABLE_TIMESTAMP_LEFT, we print "5s ago" but "5s". This is more useful for cases where we generally only want to print timestamps in the future.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/format-table.c13
-rw-r--r--src/shared/format-table.h1
2 files changed, 12 insertions, 2 deletions
diff --git a/src/shared/format-table.c b/src/shared/format-table.c
index ee45d4fd9e..351a5ede11 100644
--- a/src/shared/format-table.c
+++ b/src/shared/format-table.c
@@ -297,6 +297,7 @@ static size_t table_data_size(TableDataType type, const void *data) {
case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
+ case TABLE_TIMESTAMP_LEFT:
case TABLE_TIMESTAMP_DATE:
case TABLE_TIMESPAN:
case TABLE_TIMESPAN_MSEC:
@@ -896,6 +897,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
+ case TABLE_TIMESTAMP_LEFT:
case TABLE_TIMESTAMP_DATE:
case TABLE_TIMESPAN:
case TABLE_TIMESPAN_MSEC:
@@ -1304,6 +1306,7 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
+ case TABLE_TIMESTAMP_LEFT:
case TABLE_TIMESTAMP_DATE:
return CMP(a->timestamp, b->timestamp);
@@ -1537,11 +1540,14 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
+ case TABLE_TIMESTAMP_LEFT:
case TABLE_TIMESTAMP_DATE: {
_cleanup_free_ char *p = NULL;
char *ret;
- p = new(char, d->type == TABLE_TIMESTAMP_RELATIVE ? FORMAT_TIMESTAMP_RELATIVE_MAX : FORMAT_TIMESTAMP_MAX);
+ p = new(char,
+ IN_SET(d->type, TABLE_TIMESTAMP_RELATIVE, TABLE_TIMESTAMP_LEFT) ?
+ FORMAT_TIMESTAMP_RELATIVE_MAX : FORMAT_TIMESTAMP_MAX);
if (!p)
return NULL;
@@ -1552,7 +1558,9 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
else if (d->type == TABLE_TIMESTAMP_DATE)
ret = format_timestamp_style(p, FORMAT_TIMESTAMP_MAX, d->timestamp, TIMESTAMP_DATE);
else
- ret = format_timestamp_relative(p, FORMAT_TIMESTAMP_RELATIVE_MAX, d->timestamp);
+ ret = format_timestamp_relative_full(
+ p, FORMAT_TIMESTAMP_RELATIVE_MAX, d->timestamp,
+ /* implicit_left= */ d->type == TABLE_TIMESTAMP_LEFT);
if (!ret)
return "-";
@@ -2589,6 +2597,7 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
+ case TABLE_TIMESTAMP_LEFT:
case TABLE_TIMESTAMP_DATE:
if (d->timestamp == USEC_INFINITY)
return json_variant_new_null(ret);
diff --git a/src/shared/format-table.h b/src/shared/format-table.h
index 97255f5aef..5a2b366b59 100644
--- a/src/shared/format-table.h
+++ b/src/shared/format-table.h
@@ -23,6 +23,7 @@ typedef enum TableDataType {
TABLE_TIMESTAMP,
TABLE_TIMESTAMP_UTC,
TABLE_TIMESTAMP_RELATIVE,
+ TABLE_TIMESTAMP_LEFT,
TABLE_TIMESTAMP_DATE,
TABLE_TIMESPAN,
TABLE_TIMESPAN_MSEC,