1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
4 #include "string-util.h"
8 static void test_should_pass(const char *p
) {
10 char buf
[FORMAT_TIMESTAMP_MAX
], buf_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
12 log_info("Test: %s", p
);
13 assert_se(parse_timestamp(p
, &t
) >= 0);
14 assert_se(format_timestamp_style(buf
, sizeof(buf
), t
, TIMESTAMP_US
));
15 log_info("\"%s\" → \"%s\"", p
, buf
);
17 assert_se(parse_timestamp(buf
, &q
) >= 0);
19 log_error("round-trip failed: \"%s\" → \"%s\"",
20 buf
, FORMAT_TIMESTAMP_STYLE(q
, TIMESTAMP_US
));
23 assert_se(format_timestamp_relative(buf_relative
, sizeof(buf_relative
), t
));
24 log_info("%s", strna(buf_relative
));
27 static void test_should_parse(const char *p
) {
30 log_info("Test: %s", p
);
31 assert_se(parse_timestamp(p
, &t
) >= 0);
32 log_info("\"%s\" → \"@%" PRI_USEC
"\"", p
, t
);
35 static void test_should_fail(const char *p
) {
39 log_info("Test: %s", p
);
40 r
= parse_timestamp(p
, &t
);
42 log_info("\"%s\" → \"@%" PRI_USEC
"\" (unexpected)", p
, t
);
44 log_info("parse_timestamp() returns %d (expected)", r
);
48 static void test_one(const char *p
) {
49 _cleanup_free_
char *with_utc
= NULL
;
51 with_utc
= strjoin(p
, " UTC");
53 test_should_pass(with_utc
);
56 static void test_one_noutc(const char *p
) {
57 _cleanup_free_
char *with_utc
= NULL
;
59 with_utc
= strjoin(p
, " UTC");
61 test_should_fail(with_utc
);
64 int main(int argc
, char *argv
[]) {
65 /* Tests have hard-coded results that do not expect a specific timezone to be set by the caller */
66 assert_se(unsetenv("TZ") >= 0);
68 test_setup_logging(LOG_DEBUG
);
72 test_one("18:42:44.0");
73 test_one("18:42:44.999999999999");
74 test_one("12-10-02 12:13:14");
75 test_one("12-10-2 12:13:14");
76 test_one("12-10-03 12:13");
77 test_one("2012-12-30 18:42");
78 test_one("2012-10-02");
79 test_one("Mar 12 12:01:01");
80 test_one("Mar 12 12:01:01.687197");
81 test_one("Tue 2012-10-02");
82 test_one("yesterday");
85 test_one_noutc("16:20 UTC");
86 if (access("/usr/share/zoneinfo/Asia/Seoul", F_OK
) >= 0) {
87 test_one_noutc("16:20 Asia/Seoul");
88 test_one_noutc("tomorrow Asia/Seoul");
89 test_one_noutc("2012-12-30 18:42 Asia/Seoul");
91 test_one_noutc("now");
92 test_one_noutc("+2d");
93 test_one_noutc("+2y 4d");
94 test_one_noutc("5months ago");
95 test_one_noutc("@1395716396");
96 test_should_parse("1970-1-1 UTC");
97 test_should_pass("1970-1-1 00:00:01 UTC");
98 test_should_fail("1969-12-31 UTC");
99 test_should_fail("-1000y");
100 test_should_fail("today UTC UTC");
101 if (access("/usr/share/zoneinfo/Asia/Seoul", F_OK
) >= 0) {
102 test_should_fail("now Asia/Seoul");
103 test_should_fail("+2d Asia/Seoul");
104 test_should_fail("@1395716396 Asia/Seoul");
106 #if SIZEOF_TIME_T == 8
107 test_should_pass("9999-12-30 23:59:59 UTC");
108 test_should_fail("9999-12-31 00:00:00 UTC");
109 test_should_fail("10000-01-01 00:00:00 UTC");
110 #elif SIZEOF_TIME_T == 4
111 test_should_pass("2038-01-18 03:14:07 UTC");
112 test_should_fail("2038-01-18 03:14:08 UTC");