From: Mike Yuan Date: Thu, 25 May 2023 09:47:57 +0000 (+0800) Subject: time-util,format-table: add relative_monotonic variant for timestamp X-Git-Tag: v254-rc1~361^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d65c289fbfc5f1881c3e789874704ca077d6bc4d;p=thirdparty%2Fsystemd.git time-util,format-table: add relative_monotonic variant for timestamp --- diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 657e231b0d4..f124e6f0162 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -422,7 +422,7 @@ char *format_timestamp_style( return buf; } -char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, bool implicit_left) { +char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, clockid_t clock, bool implicit_left) { const char *s; usec_t n, d; @@ -431,7 +431,7 @@ char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, bool implici if (!timestamp_is_set(t)) return NULL; - n = now(CLOCK_REALTIME); + n = now(clock); if (n > t) { d = n - t; s = " ago"; diff --git a/src/basic/time-util.h b/src/basic/time-util.h index 79a6715656a..b49137d5c3f 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -123,12 +123,16 @@ struct timeval* timeval_store(struct timeval *tv, usec_t u); #define TIMEVAL_STORE(u) timeval_store(&(struct timeval) {}, (u)) char* format_timestamp_style(char *buf, size_t l, usec_t t, TimestampStyle style) _warn_unused_result_; -char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, bool implicit_left) _warn_unused_result_; +char* format_timestamp_relative_full(char *buf, size_t l, usec_t t, clockid_t clock, bool implicit_left) _warn_unused_result_; char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) _warn_unused_result_; _warn_unused_result_ -static inline char* format_timestamp_relative(char *buf, size_t l, usec_t t) { - return format_timestamp_relative_full(buf, l, t, false); +static inline char* format_timestamp_relative(char *buf, size_t l, usec_t t) { + return format_timestamp_relative_full(buf, l, t, CLOCK_REALTIME, /* implicit_left = */ false); +} +_warn_unused_result_ +static inline char* format_timestamp_relative_monotonic(char *buf, size_t l, usec_t t) { + return format_timestamp_relative_full(buf, l, t, CLOCK_MONOTONIC, /* implicit_left = */ false); } _warn_unused_result_ @@ -142,6 +146,8 @@ static inline char* format_timestamp(char *buf, size_t l, usec_t t) { #define FORMAT_TIMESTAMP(t) format_timestamp((char[FORMAT_TIMESTAMP_MAX]){}, FORMAT_TIMESTAMP_MAX, t) #define FORMAT_TIMESTAMP_RELATIVE(t) \ format_timestamp_relative((char[FORMAT_TIMESTAMP_RELATIVE_MAX]){}, FORMAT_TIMESTAMP_RELATIVE_MAX, t) +#define FORMAT_TIMESTAMP_RELATIVE_MONOTONIC(t) \ + format_timestamp_relative_monotonic((char[FORMAT_TIMESTAMP_RELATIVE_MAX]){}, FORMAT_TIMESTAMP_RELATIVE_MAX, t) #define FORMAT_TIMESPAN(t, accuracy) format_timespan((char[FORMAT_TIMESPAN_MAX]){}, FORMAT_TIMESPAN_MAX, t, accuracy) #define FORMAT_TIMESTAMP_STYLE(t, style) \ format_timestamp_style((char[FORMAT_TIMESTAMP_MAX]){}, FORMAT_TIMESTAMP_MAX, t, style) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 83b749d677a..22efd042e2d 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -300,6 +300,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_RELATIVE_MONOTONIC: case TABLE_TIMESTAMP_LEFT: case TABLE_TIMESTAMP_DATE: case TABLE_TIMESPAN: @@ -905,6 +906,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_RELATIVE_MONOTONIC: case TABLE_TIMESTAMP_LEFT: case TABLE_TIMESTAMP_DATE: case TABLE_TIMESPAN: @@ -1322,6 +1324,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_RELATIVE_MONOTONIC: case TABLE_TIMESTAMP_LEFT: case TABLE_TIMESTAMP_DATE: return CMP(a->timestamp, b->timestamp); @@ -1564,13 +1567,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_RELATIVE_MONOTONIC: case TABLE_TIMESTAMP_LEFT: case TABLE_TIMESTAMP_DATE: { _cleanup_free_ char *p = NULL; char *ret; p = new(char, - IN_SET(d->type, TABLE_TIMESTAMP_RELATIVE, TABLE_TIMESTAMP_LEFT) ? + IN_SET(d->type, TABLE_TIMESTAMP_RELATIVE, TABLE_TIMESTAMP_RELATIVE_MONOTONIC, TABLE_TIMESTAMP_LEFT) ? FORMAT_TIMESTAMP_RELATIVE_MAX : FORMAT_TIMESTAMP_MAX); if (!p) return NULL; @@ -1581,10 +1585,14 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas ret = format_timestamp_style(p, FORMAT_TIMESTAMP_MAX, d->timestamp, TIMESTAMP_UTC); else if (d->type == TABLE_TIMESTAMP_DATE) ret = format_timestamp_style(p, FORMAT_TIMESTAMP_MAX, d->timestamp, TIMESTAMP_DATE); + else if (d->type == TABLE_TIMESTAMP_RELATIVE_MONOTONIC) + ret = format_timestamp_relative_full(p, FORMAT_TIMESTAMP_RELATIVE_MAX, + d->timestamp, CLOCK_MONOTONIC, + /* implicit_left = */ false); else - ret = format_timestamp_relative_full( - p, FORMAT_TIMESTAMP_RELATIVE_MAX, d->timestamp, - /* implicit_left= */ d->type == TABLE_TIMESTAMP_LEFT); + ret = format_timestamp_relative_full(p, FORMAT_TIMESTAMP_RELATIVE_MAX, + d->timestamp, CLOCK_REALTIME, + /* implicit_left = */ d->type == TABLE_TIMESTAMP_LEFT); if (!ret) return "-"; @@ -2640,6 +2648,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_RELATIVE_MONOTONIC: case TABLE_TIMESTAMP_LEFT: case TABLE_TIMESTAMP_DATE: if (d->timestamp == USEC_INFINITY) diff --git a/src/shared/format-table.h b/src/shared/format-table.h index 148418c70f6..742d5e3ae25 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_RELATIVE_MONOTONIC, TABLE_TIMESTAMP_LEFT, TABLE_TIMESTAMP_DATE, TABLE_TIMESPAN, diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index e80922901e0..235c6cf1642 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -464,64 +464,69 @@ TEST(format_timestamp_relative_full) { /* Years and months */ x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 1*USEC_PER_MONTH); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "1 year 1 month ago")); + x = now(CLOCK_MONOTONIC) + (1*USEC_PER_YEAR + 1.5*USEC_PER_MONTH); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_MONOTONIC, false)); + log_debug("%s", buf); + assert_se(streq(buf, "1 year 1 month left")); + x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "1 year 2 months ago")); x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 1*USEC_PER_MONTH); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "2 years 1 month ago")); x = now(CLOCK_REALTIME) - (2*USEC_PER_YEAR + 2*USEC_PER_MONTH); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "2 years 2 months ago")); /* Months and days */ x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 1*USEC_PER_DAY); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "1 month 1 day ago")); x = now(CLOCK_REALTIME) - (1*USEC_PER_MONTH + 2*USEC_PER_DAY); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "1 month 2 days ago")); x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 1*USEC_PER_DAY); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "2 months 1 day ago")); x = now(CLOCK_REALTIME) - (2*USEC_PER_MONTH + 2*USEC_PER_DAY); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "2 months 2 days ago")); /* Weeks and days */ x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 1*USEC_PER_DAY); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "1 week 1 day ago")); x = now(CLOCK_REALTIME) - (1*USEC_PER_WEEK + 2*USEC_PER_DAY); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "1 week 2 days ago")); x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 1*USEC_PER_DAY); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "2 weeks 1 day ago")); x = now(CLOCK_REALTIME) - (2*USEC_PER_WEEK + 2*USEC_PER_DAY); - assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, true)); + assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, CLOCK_REALTIME, true)); log_debug("%s", buf); assert_se(streq(buf, "2 weeks 2 days ago")); }