]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-date.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / test / test-date.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
cfbc22ab 2
92134489 3#include <string.h>
cfbc22ab 4
b5efdb8a 5#include "alloc-util.h"
07630cea 6#include "string-util.h"
b5efdb8a 7#include "util.h"
cfbc22ab 8
063bc364 9static void test_should_pass(const char *p) {
92134489 10 usec_t t, q;
68bdd2d2 11 char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];
cfbc22ab 12
7635ab8e 13 log_info("Test: %s", p);
92134489 14 assert_se(parse_timestamp(p, &t) >= 0);
68bdd2d2 15 assert_se(format_timestamp_us(buf, sizeof(buf), t));
d80e5b74 16 log_info("\"%s\" → \"%s\"", p, buf);
cfbc22ab 17
92134489 18 assert_se(parse_timestamp(buf, &q) >= 0);
a3da4a3d
ZJS
19 if (q != t) {
20 char tmp[FORMAT_TIMESTAMP_MAX];
21
22 log_error("round-trip failed: \"%s\" → \"%s\"",
23 buf, format_timestamp_us(tmp, sizeof(tmp), q));
24 }
92134489 25 assert_se(q == t);
cfbc22ab 26
68bdd2d2 27 assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
14c35ce7 28 log_info("%s", strna(buf_relative));
92134489 29}
cfbc22ab 30
b1c51766
HV
31static void test_should_parse(const char *p) {
32 usec_t t;
33
7635ab8e 34 log_info("Test: %s", p);
b1c51766 35 assert_se(parse_timestamp(p, &t) >= 0);
7635ab8e 36 log_info("\"%s\" → \"@%" PRI_USEC "\"", p, t);
b1c51766
HV
37}
38
063bc364
HV
39static void test_should_fail(const char *p) {
40 usec_t t;
7635ab8e 41 int r;
063bc364 42
7635ab8e
YW
43 log_info("Test: %s", p);
44 r = parse_timestamp(p, &t);
45 if (r >= 0)
46 log_info("\"%s\" → \"@%" PRI_USEC "\" (unexpected)", p, t);
47 else
48 log_info("parse_timestamp() returns %d (expected)", r);
49 assert_se(r < 0);
063bc364
HV
50}
51
52static void test_one(const char *p) {
53 _cleanup_free_ char *with_utc;
54
605405c6 55 with_utc = strjoin(p, " UTC");
063bc364
HV
56 test_should_pass(p);
57 test_should_pass(with_utc);
58}
59
60static void test_one_noutc(const char *p) {
61 _cleanup_free_ char *with_utc;
62
605405c6 63 with_utc = strjoin(p, " UTC");
063bc364
HV
64 test_should_pass(p);
65 test_should_fail(with_utc);
66}
67
92134489 68int main(int argc, char *argv[]) {
a3da4a3d
ZJS
69 log_set_max_level(LOG_DEBUG);
70 log_parse_environment();
71 log_open();
72
92134489
LP
73 test_one("17:41");
74 test_one("18:42:44");
063bc364
HV
75 test_one("18:42:44.0");
76 test_one("18:42:44.999999999999");
92134489
LP
77 test_one("12-10-02 12:13:14");
78 test_one("12-10-2 12:13:14");
79 test_one("12-10-03 12:13");
80 test_one("2012-12-30 18:42");
81 test_one("2012-10-02");
82 test_one("Tue 2012-10-02");
92134489
LP
83 test_one("yesterday");
84 test_one("today");
85 test_one("tomorrow");
48d26c01
IK
86 test_one_noutc("16:20 UTC");
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");
68bdd2d2 90 test_one_noutc("now");
063bc364
HV
91 test_one_noutc("+2d");
92 test_one_noutc("+2y 4d");
93 test_one_noutc("5months ago");
94 test_one_noutc("@1395716396");
1bb4b028 95 test_should_parse("1970-1-1 UTC");
68bdd2d2
YW
96 test_should_pass("1970-1-1 00:00:01 UTC");
97 test_should_fail("1969-12-31 UTC");
98 test_should_fail("-100y");
99 test_should_fail("today UTC UTC");
48d26c01
IK
100 test_should_fail("now Asia/Seoul");
101 test_should_fail("+2d Asia/Seoul");
102 test_should_fail("@1395716396 Asia/Seoul");
1bb4b028 103#if SIZEOF_TIME_T == 8
7635ab8e 104 test_should_pass("9999-12-30 23:59:59 UTC");
1bb4b028
LP
105 test_should_fail("9999-12-31 00:00:00 UTC");
106 test_should_fail("10000-01-01 00:00:00 UTC");
107#elif SIZEOF_TIME_T == 4
7635ab8e
YW
108 test_should_pass("2038-01-19 03:14:07 UTC");
109 test_should_fail("2038-01-19 03:14:08 UTC");
1bb4b028 110#endif
decad910 111
cfbc22ab
LP
112 return 0;
113}