]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-date.c
Merge pull request #31648 from neighbourhoodie/review-content
[thirdparty/systemd.git] / src / test / test-date.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "string-util.h"
7 #include "tests.h"
8 #include "time-util.h"
9
10 static void test_should_pass(const char *p) {
11 usec_t t, q;
12 char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];
13
14 log_info("Test: %s", p);
15 assert_se(parse_timestamp(p, &t) >= 0);
16 assert_se(format_timestamp_style(buf, sizeof(buf), t, TIMESTAMP_US));
17 log_info("\"%s\" \"%s\"", p, buf);
18
19 assert_se(parse_timestamp(buf, &q) >= 0);
20 if (q != t)
21 log_error("round-trip failed: \"%s\" \"%s\"",
22 buf, FORMAT_TIMESTAMP_STYLE(q, TIMESTAMP_US));
23 assert_se(q == t);
24
25 assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
26 log_info("%s", strna(buf_relative));
27 }
28
29 static void test_should_parse(const char *p) {
30 usec_t t;
31
32 log_info("Test: %s", p);
33 assert_se(parse_timestamp(p, &t) >= 0);
34 log_info("\"%s\" \"@%" PRI_USEC "\"", p, t);
35 }
36
37 static void test_should_fail(const char *p) {
38 usec_t t;
39 int r;
40
41 log_info("Test: %s", p);
42 r = parse_timestamp(p, &t);
43 if (r >= 0)
44 log_info("\"%s\" \"@%" PRI_USEC "\" (unexpected)", p, t);
45 else
46 log_info("parse_timestamp() returns %d (expected)", r);
47 assert_se(r < 0);
48 }
49
50 static void test_one(const char *p) {
51 _cleanup_free_ char *with_utc = NULL;
52
53 with_utc = strjoin(p, " UTC");
54 test_should_pass(p);
55 test_should_pass(with_utc);
56 }
57
58 static void test_one_noutc(const char *p) {
59 _cleanup_free_ char *with_utc = NULL;
60
61 with_utc = strjoin(p, " UTC");
62 test_should_pass(p);
63 test_should_fail(with_utc);
64 }
65
66 int main(int argc, char *argv[]) {
67 /* Tests have hard-coded results that do not expect a specific timezone to be set by the caller */
68 assert_se(unsetenv("TZ") >= 0);
69
70 test_setup_logging(LOG_DEBUG);
71
72 test_one("17:41");
73 test_one("18:42:44");
74 test_one("18:42:44.0");
75 test_one("18:42:44.999999999999");
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("Mar 12 12:01:01");
82 test_one("Mar 12 12:01:01.687197");
83 test_one("Tue 2012-10-02");
84 test_one("yesterday");
85 test_one("today");
86 test_one("tomorrow");
87 test_one_noutc("16:20 UTC");
88 if (access("/usr/share/zoneinfo/Asia/Seoul", F_OK) >= 0) {
89 test_one_noutc("16:20 Asia/Seoul");
90 test_one_noutc("tomorrow Asia/Seoul");
91 test_one_noutc("2012-12-30 18:42 Asia/Seoul");
92 }
93 test_one_noutc("now");
94 test_one_noutc("+2d");
95 test_one_noutc("+2y 4d");
96 test_one_noutc("5months ago");
97 test_one_noutc("@1395716396");
98 test_should_parse("1970-1-1 UTC");
99 test_should_pass("1970-1-1 00:00:01 UTC");
100 test_should_fail("1969-12-31 UTC");
101 test_should_fail("-1000y");
102 test_should_fail("today UTC UTC");
103 if (access("/usr/share/zoneinfo/Asia/Seoul", F_OK) >= 0) {
104 test_should_fail("now Asia/Seoul");
105 test_should_fail("+2d Asia/Seoul");
106 test_should_fail("@1395716396 Asia/Seoul");
107 }
108 #if SIZEOF_TIME_T == 8
109 test_should_pass("9999-12-30 23:59:59 UTC");
110 test_should_fail("9999-12-31 00:00:00 UTC");
111 test_should_fail("10000-01-01 00:00:00 UTC");
112 #elif SIZEOF_TIME_T == 4
113 test_should_pass("2038-01-18 03:14:07 UTC");
114 test_should_fail("2038-01-18 03:14:08 UTC");
115 #endif
116
117 return 0;
118 }