basic/time-util: add function to format timestamps with different styles
Instead of a multiple fixed format helper functions, add an enum and a single helper, so that it's easier to extend in the future.
This commit is contained in:
@@ -58,7 +58,7 @@ static void test_next(const char *input, const char *new_tz, usec_t after, usec_
|
||||
|
||||
u = after;
|
||||
r = calendar_spec_next_usec(c, after, &u);
|
||||
printf("At: %s\n", r < 0 ? strerror_safe(r) : format_timestamp_us(buf, sizeof buf, u));
|
||||
printf("At: %s\n", r < 0 ? strerror_safe(r) : format_timestamp_style(buf, sizeof buf, u, TIMESTAMP_US));
|
||||
if (expect != (usec_t)-1)
|
||||
assert_se(r >= 0 && u == expect);
|
||||
else
|
||||
@@ -83,7 +83,7 @@ static void test_timestamp(void) {
|
||||
|
||||
x = now(CLOCK_REALTIME);
|
||||
|
||||
assert_se(format_timestamp_us(buf, sizeof(buf), x));
|
||||
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US));
|
||||
printf("%s\n", buf);
|
||||
assert_se(calendar_spec_from_string(buf, &c) >= 0);
|
||||
assert_se(calendar_spec_to_string(c, &t) >= 0);
|
||||
@@ -104,11 +104,11 @@ static void test_hourly_bug_4031(void) {
|
||||
n = now(CLOCK_REALTIME);
|
||||
assert_se((r = calendar_spec_next_usec(c, n, &u)) >= 0);
|
||||
|
||||
printf("Now: %s (%"PRIu64")\n", format_timestamp_us(buf, sizeof buf, n), n);
|
||||
printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror_safe(r) : format_timestamp_us(buf, sizeof buf, u), u);
|
||||
printf("Now: %s (%"PRIu64")\n", format_timestamp_style(buf, sizeof buf, n, TIMESTAMP_US), n);
|
||||
printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror_safe(r) : format_timestamp_style(buf, sizeof buf, u, TIMESTAMP_US), u);
|
||||
|
||||
assert_se((r = calendar_spec_next_usec(c, u, &w)) >= 0);
|
||||
printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror_safe(r) : format_timestamp_us(zaf, sizeof zaf, w), w);
|
||||
printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror_safe(r) : format_timestamp_style(zaf, sizeof zaf, w, TIMESTAMP_US), w);
|
||||
|
||||
assert_se(n < u);
|
||||
assert_se(u <= n + USEC_PER_HOUR);
|
||||
|
||||
@@ -11,7 +11,7 @@ static void test_should_pass(const char *p) {
|
||||
|
||||
log_info("Test: %s", p);
|
||||
assert_se(parse_timestamp(p, &t) >= 0);
|
||||
assert_se(format_timestamp_us(buf, sizeof(buf), t));
|
||||
assert_se(format_timestamp_style(buf, sizeof(buf), t, TIMESTAMP_US));
|
||||
log_info("\"%s\" → \"%s\"", p, buf);
|
||||
|
||||
assert_se(parse_timestamp(buf, &q) >= 0);
|
||||
@@ -19,7 +19,7 @@ static void test_should_pass(const char *p) {
|
||||
char tmp[FORMAT_TIMESTAMP_MAX];
|
||||
|
||||
log_error("round-trip failed: \"%s\" → \"%s\"",
|
||||
buf, format_timestamp_us(tmp, sizeof(tmp), q));
|
||||
buf, format_timestamp_style(tmp, sizeof(tmp), q, TIMESTAMP_US));
|
||||
}
|
||||
assert_se(q == t);
|
||||
|
||||
|
||||
@@ -333,17 +333,17 @@ static void test_format_timestamp(void) {
|
||||
assert_se(parse_timestamp(buf, &y) >= 0);
|
||||
assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
|
||||
|
||||
assert_se(format_timestamp_utc(buf, sizeof(buf), x));
|
||||
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_UTC));
|
||||
log_info("%s", buf);
|
||||
assert_se(parse_timestamp(buf, &y) >= 0);
|
||||
assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
|
||||
|
||||
assert_se(format_timestamp_us(buf, sizeof(buf), x));
|
||||
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US));
|
||||
log_info("%s", buf);
|
||||
assert_se(parse_timestamp(buf, &y) >= 0);
|
||||
assert_se(x == y);
|
||||
|
||||
assert_se(format_timestamp_us_utc(buf, sizeof(buf), x));
|
||||
assert_se(format_timestamp_style(buf, sizeof(buf), x, TIMESTAMP_US_UTC));
|
||||
log_info("%s", buf);
|
||||
assert_se(parse_timestamp(buf, &y) >= 0);
|
||||
assert_se(x == y);
|
||||
@@ -364,7 +364,7 @@ static void test_format_timestamp_utc_one(usec_t val, const char *result) {
|
||||
char buf[FORMAT_TIMESTAMP_MAX];
|
||||
const char *t;
|
||||
|
||||
t = format_timestamp_utc(buf, sizeof(buf), val);
|
||||
t = format_timestamp_style(buf, sizeof(buf), val, TIMESTAMP_UTC);
|
||||
assert_se(streq_ptr(t, result));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user