]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
663996b3 MS |
2 | |
3 | #include <string.h> | |
4 | ||
db2df898 MP |
5 | #include "alloc-util.h" |
6 | #include "string-util.h" | |
6e866b33 | 7 | #include "tests.h" |
bb4f798a | 8 | #include "time-util.h" |
663996b3 | 9 | |
db2df898 | 10 | static void test_should_pass(const char *p) { |
663996b3 | 11 | usec_t t, q; |
81c58355 | 12 | char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX]; |
663996b3 | 13 | |
81c58355 | 14 | log_info("Test: %s", p); |
663996b3 | 15 | assert_se(parse_timestamp(p, &t) >= 0); |
81c58355 MB |
16 | assert_se(format_timestamp_us(buf, sizeof(buf), t)); |
17 | log_info("\"%s\" → \"%s\"", p, buf); | |
663996b3 MS |
18 | |
19 | assert_se(parse_timestamp(buf, &q) >= 0); | |
f5e65279 MB |
20 | if (q != t) { |
21 | char tmp[FORMAT_TIMESTAMP_MAX]; | |
22 | ||
23 | log_error("round-trip failed: \"%s\" → \"%s\"", | |
24 | buf, format_timestamp_us(tmp, sizeof(tmp), q)); | |
25 | } | |
663996b3 MS |
26 | assert_se(q == t); |
27 | ||
81c58355 | 28 | assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t)); |
5eef597e | 29 | log_info("%s", strna(buf_relative)); |
663996b3 MS |
30 | } |
31 | ||
db2df898 MP |
32 | static void test_should_parse(const char *p) { |
33 | usec_t t; | |
34 | ||
81c58355 | 35 | log_info("Test: %s", p); |
db2df898 | 36 | assert_se(parse_timestamp(p, &t) >= 0); |
81c58355 | 37 | log_info("\"%s\" → \"@%" PRI_USEC "\"", p, t); |
db2df898 MP |
38 | } |
39 | ||
40 | static void test_should_fail(const char *p) { | |
41 | usec_t t; | |
81c58355 | 42 | int r; |
db2df898 | 43 | |
81c58355 MB |
44 | log_info("Test: %s", p); |
45 | r = parse_timestamp(p, &t); | |
46 | if (r >= 0) | |
47 | log_info("\"%s\" → \"@%" PRI_USEC "\" (unexpected)", p, t); | |
48 | else | |
49 | log_info("parse_timestamp() returns %d (expected)", r); | |
50 | assert_se(r < 0); | |
db2df898 MP |
51 | } |
52 | ||
53 | static void test_one(const char *p) { | |
54 | _cleanup_free_ char *with_utc; | |
55 | ||
2897b343 | 56 | with_utc = strjoin(p, " UTC"); |
db2df898 MP |
57 | test_should_pass(p); |
58 | test_should_pass(with_utc); | |
59 | } | |
60 | ||
61 | static void test_one_noutc(const char *p) { | |
62 | _cleanup_free_ char *with_utc; | |
63 | ||
2897b343 | 64 | with_utc = strjoin(p, " UTC"); |
db2df898 MP |
65 | test_should_pass(p); |
66 | test_should_fail(with_utc); | |
67 | } | |
68 | ||
663996b3 | 69 | int main(int argc, char *argv[]) { |
6e866b33 | 70 | test_setup_logging(LOG_DEBUG); |
f5e65279 | 71 | |
663996b3 MS |
72 | test_one("17:41"); |
73 | test_one("18:42:44"); | |
db2df898 MP |
74 | test_one("18:42:44.0"); |
75 | test_one("18:42:44.999999999999"); | |
663996b3 MS |
76 | test_one("12-10-02 12:13:14"); |
77 | test_one("12-10-2 12:13:14"); | |
78 | test_one("12-10-03 12:13"); | |
79 | test_one("2012-12-30 18:42"); | |
80 | test_one("2012-10-02"); | |
81 | test_one("Tue 2012-10-02"); | |
663996b3 MS |
82 | test_one("yesterday"); |
83 | test_one("today"); | |
84 | test_one("tomorrow"); | |
f5e65279 MB |
85 | test_one_noutc("16:20 UTC"); |
86 | test_one_noutc("16:20 Asia/Seoul"); | |
87 | test_one_noutc("tomorrow Asia/Seoul"); | |
88 | test_one_noutc("2012-12-30 18:42 Asia/Seoul"); | |
81c58355 | 89 | test_one_noutc("now"); |
db2df898 MP |
90 | test_one_noutc("+2d"); |
91 | test_one_noutc("+2y 4d"); | |
92 | test_one_noutc("5months ago"); | |
93 | test_one_noutc("@1395716396"); | |
2897b343 | 94 | test_should_parse("1970-1-1 UTC"); |
81c58355 MB |
95 | test_should_pass("1970-1-1 00:00:01 UTC"); |
96 | test_should_fail("1969-12-31 UTC"); | |
97 | test_should_fail("-100y"); | |
98 | test_should_fail("today UTC UTC"); | |
f5e65279 MB |
99 | test_should_fail("now Asia/Seoul"); |
100 | test_should_fail("+2d Asia/Seoul"); | |
101 | test_should_fail("@1395716396 Asia/Seoul"); | |
2897b343 | 102 | #if SIZEOF_TIME_T == 8 |
81c58355 | 103 | test_should_pass("9999-12-30 23:59:59 UTC"); |
2897b343 MP |
104 | test_should_fail("9999-12-31 00:00:00 UTC"); |
105 | test_should_fail("10000-01-01 00:00:00 UTC"); | |
106 | #elif SIZEOF_TIME_T == 4 | |
81c58355 MB |
107 | test_should_pass("2038-01-19 03:14:07 UTC"); |
108 | test_should_fail("2038-01-19 03:14:08 UTC"); | |
2897b343 | 109 | #endif |
663996b3 MS |
110 | |
111 | return 0; | |
112 | } |