summaryrefslogtreecommitdiff
path: root/src/test/test-time-util.c
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/test/test-time-util.c
parent81cfea95e51c72da6765b517e7038b3e7e3dec9f (diff)
downloadsystemd-d5e6f36c7a5a2702c6bcf750cbe9815ff98cac48.tar.gz
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/test/test-time-util.c')
-rw-r--r--src/test/test-time-util.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c
index 0fb76391fd..dee012fa2e 100644
--- a/src/test/test-time-util.c
+++ b/src/test/test-time-util.c
@@ -401,6 +401,74 @@ TEST(FORMAT_TIMESTAMP) {
}
}
+TEST(format_timestamp_relative_full) {
+ char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
+ usec_t x;
+
+ /* 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));
+ log_debug("%s", buf);
+ assert_se(streq(buf, "1 year 1 month ago"));
+
+ x = now(CLOCK_REALTIME) - (1*USEC_PER_YEAR + 2*USEC_PER_MONTH);
+ assert_se(format_timestamp_relative_full(buf, sizeof(buf), x, 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));
+ 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));
+ 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));
+ 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));
+ 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));
+ 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));
+ 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));
+ 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));
+ 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));
+ 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));
+ log_debug("%s", buf);
+ assert_se(streq(buf, "2 weeks 2 days ago"));
+}
+
TEST(format_timestamp_relative) {
char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
usec_t x;