]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-date.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / test / test-date.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6 ***/
7
8 #include <string.h>
9
10 #include "alloc-util.h"
11 #include "string-util.h"
12 #include "util.h"
13
14 static void test_should_pass(const char *p) {
15 usec_t t, q;
16 char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];
17
18 log_info("Test: %s", p);
19 assert_se(parse_timestamp(p, &t) >= 0);
20 assert_se(format_timestamp_us(buf, sizeof(buf), t));
21 log_info("\"%s\" \"%s\"", p, buf);
22
23 assert_se(parse_timestamp(buf, &q) >= 0);
24 if (q != t) {
25 char tmp[FORMAT_TIMESTAMP_MAX];
26
27 log_error("round-trip failed: \"%s\" \"%s\"",
28 buf, format_timestamp_us(tmp, sizeof(tmp), q));
29 }
30 assert_se(q == t);
31
32 assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
33 log_info("%s", strna(buf_relative));
34 }
35
36 static void test_should_parse(const char *p) {
37 usec_t t;
38
39 log_info("Test: %s", p);
40 assert_se(parse_timestamp(p, &t) >= 0);
41 log_info("\"%s\" \"@%" PRI_USEC "\"", p, t);
42 }
43
44 static void test_should_fail(const char *p) {
45 usec_t t;
46 int r;
47
48 log_info("Test: %s", p);
49 r = parse_timestamp(p, &t);
50 if (r >= 0)
51 log_info("\"%s\" \"@%" PRI_USEC "\" (unexpected)", p, t);
52 else
53 log_info("parse_timestamp() returns %d (expected)", r);
54 assert_se(r < 0);
55 }
56
57 static void test_one(const char *p) {
58 _cleanup_free_ char *with_utc;
59
60 with_utc = strjoin(p, " UTC");
61 test_should_pass(p);
62 test_should_pass(with_utc);
63 }
64
65 static void test_one_noutc(const char *p) {
66 _cleanup_free_ char *with_utc;
67
68 with_utc = strjoin(p, " UTC");
69 test_should_pass(p);
70 test_should_fail(with_utc);
71 }
72
73 int main(int argc, char *argv[]) {
74 log_set_max_level(LOG_DEBUG);
75 log_parse_environment();
76 log_open();
77
78 test_one("17:41");
79 test_one("18:42:44");
80 test_one("18:42:44.0");
81 test_one("18:42:44.999999999999");
82 test_one("12-10-02 12:13:14");
83 test_one("12-10-2 12:13:14");
84 test_one("12-10-03 12:13");
85 test_one("2012-12-30 18:42");
86 test_one("2012-10-02");
87 test_one("Tue 2012-10-02");
88 test_one("yesterday");
89 test_one("today");
90 test_one("tomorrow");
91 test_one_noutc("16:20 UTC");
92 test_one_noutc("16:20 Asia/Seoul");
93 test_one_noutc("tomorrow Asia/Seoul");
94 test_one_noutc("2012-12-30 18:42 Asia/Seoul");
95 test_one_noutc("now");
96 test_one_noutc("+2d");
97 test_one_noutc("+2y 4d");
98 test_one_noutc("5months ago");
99 test_one_noutc("@1395716396");
100 test_should_parse("1970-1-1 UTC");
101 test_should_pass("1970-1-1 00:00:01 UTC");
102 test_should_fail("1969-12-31 UTC");
103 test_should_fail("-100y");
104 test_should_fail("today UTC UTC");
105 test_should_fail("now Asia/Seoul");
106 test_should_fail("+2d Asia/Seoul");
107 test_should_fail("@1395716396 Asia/Seoul");
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-19 03:14:07 UTC");
114 test_should_fail("2038-01-19 03:14:08 UTC");
115 #endif
116
117 return 0;
118 }