]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-date.c
2b96983e2cb612fd101db7aed2f80e6629e30e2d
[thirdparty/systemd.git] / src / test / test-date.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "string-util.h"
5 #include "tests.h"
6 #include "time-util.h"
7
8 static void test_should_pass(const char *p) {
9 usec_t t, q;
10 char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];
11
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);
16
17 assert_se(parse_timestamp(buf, &q) >= 0);
18 if (q != t)
19 log_error("round-trip failed: \"%s\" \"%s\"",
20 buf, FORMAT_TIMESTAMP_STYLE(q, TIMESTAMP_US));
21 assert_se(q == t);
22
23 assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
24 log_info("%s", strna(buf_relative));
25 }
26
27 static void test_should_parse(const char *p) {
28 usec_t t;
29
30 log_info("Test: %s", p);
31 assert_se(parse_timestamp(p, &t) >= 0);
32 log_info("\"%s\" \"@%" PRI_USEC "\"", p, t);
33 }
34
35 static void test_should_fail(const char *p) {
36 usec_t t;
37 int r;
38
39 log_info("Test: %s", p);
40 r = parse_timestamp(p, &t);
41 if (r >= 0)
42 log_info("\"%s\" \"@%" PRI_USEC "\" (unexpected)", p, t);
43 else
44 log_info("parse_timestamp() returns %d (expected)", r);
45 assert_se(r < 0);
46 }
47
48 static void test_one(const char *p) {
49 _cleanup_free_ char *with_utc = NULL;
50
51 with_utc = strjoin(p, " UTC");
52 test_should_pass(p);
53 test_should_pass(with_utc);
54 }
55
56 static void test_one_noutc(const char *p) {
57 _cleanup_free_ char *with_utc = NULL;
58
59 with_utc = strjoin(p, " UTC");
60 test_should_pass(p);
61 test_should_fail(with_utc);
62 }
63
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);
67
68 test_setup_logging(LOG_DEBUG);
69
70 test_one("17:41");
71 test_one("18:42:44");
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");
83 test_one("today");
84 test_one("tomorrow");
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");
90 }
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");
105 }
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");
113 #endif
114
115 return 0;
116 }