2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "string-util.h"
26 static void test_should_pass(const char *p
) {
28 char buf
[FORMAT_TIMESTAMP_MAX
], buf_relative
[FORMAT_TIMESTAMP_RELATIVE_MAX
];
30 log_info("Test: %s", p
);
31 assert_se(parse_timestamp(p
, &t
) >= 0);
32 assert_se(format_timestamp_us(buf
, sizeof(buf
), t
));
33 log_info("\"%s\" → \"%s\"", p
, buf
);
35 assert_se(parse_timestamp(buf
, &q
) >= 0);
37 char tmp
[FORMAT_TIMESTAMP_MAX
];
39 log_error("round-trip failed: \"%s\" → \"%s\"",
40 buf
, format_timestamp_us(tmp
, sizeof(tmp
), q
));
44 assert_se(format_timestamp_relative(buf_relative
, sizeof(buf_relative
), t
));
45 log_info("%s", strna(buf_relative
));
48 static void test_should_parse(const char *p
) {
51 log_info("Test: %s", p
);
52 assert_se(parse_timestamp(p
, &t
) >= 0);
53 log_info("\"%s\" → \"@%" PRI_USEC
"\"", p
, t
);
56 static void test_should_fail(const char *p
) {
60 log_info("Test: %s", p
);
61 r
= parse_timestamp(p
, &t
);
63 log_info("\"%s\" → \"@%" PRI_USEC
"\" (unexpected)", p
, t
);
65 log_info("parse_timestamp() returns %d (expected)", r
);
69 static void test_one(const char *p
) {
70 _cleanup_free_
char *with_utc
;
72 with_utc
= strjoin(p
, " UTC");
74 test_should_pass(with_utc
);
77 static void test_one_noutc(const char *p
) {
78 _cleanup_free_
char *with_utc
;
80 with_utc
= strjoin(p
, " UTC");
82 test_should_fail(with_utc
);
85 int main(int argc
, char *argv
[]) {
86 log_set_max_level(LOG_DEBUG
);
87 log_parse_environment();
92 test_one("18:42:44.0");
93 test_one("18:42:44.999999999999");
94 test_one("12-10-02 12:13:14");
95 test_one("12-10-2 12:13:14");
96 test_one("12-10-03 12:13");
97 test_one("2012-12-30 18:42");
98 test_one("2012-10-02");
99 test_one("Tue 2012-10-02");
100 test_one("yesterday");
102 test_one("tomorrow");
103 test_one_noutc("16:20 UTC");
104 test_one_noutc("16:20 Asia/Seoul");
105 test_one_noutc("tomorrow Asia/Seoul");
106 test_one_noutc("2012-12-30 18:42 Asia/Seoul");
107 test_one_noutc("now");
108 test_one_noutc("+2d");
109 test_one_noutc("+2y 4d");
110 test_one_noutc("5months ago");
111 test_one_noutc("@1395716396");
112 test_should_parse("1970-1-1 UTC");
113 test_should_pass("1970-1-1 00:00:01 UTC");
114 test_should_fail("1969-12-31 UTC");
115 test_should_fail("-100y");
116 test_should_fail("today UTC UTC");
117 test_should_fail("now Asia/Seoul");
118 test_should_fail("+2d Asia/Seoul");
119 test_should_fail("@1395716396 Asia/Seoul");
120 #if SIZEOF_TIME_T == 8
121 test_should_pass("9999-12-30 23:59:59 UTC");
122 test_should_fail("9999-12-31 00:00:00 UTC");
123 test_should_fail("10000-01-01 00:00:00 UTC");
124 #elif SIZEOF_TIME_T == 4
125 test_should_pass("2038-01-19 03:14:07 UTC");
126 test_should_fail("2038-01-19 03:14:08 UTC");