]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-date.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-date.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
cfbc22ab
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
92134489 21#include <string.h>
cfbc22ab 22
b5efdb8a 23#include "alloc-util.h"
07630cea 24#include "string-util.h"
b5efdb8a 25#include "util.h"
cfbc22ab 26
063bc364 27static void test_should_pass(const char *p) {
92134489 28 usec_t t, q;
68bdd2d2 29 char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];
cfbc22ab 30
7635ab8e 31 log_info("Test: %s", p);
92134489 32 assert_se(parse_timestamp(p, &t) >= 0);
68bdd2d2 33 assert_se(format_timestamp_us(buf, sizeof(buf), t));
d80e5b74 34 log_info("\"%s\" → \"%s\"", p, buf);
cfbc22ab 35
92134489 36 assert_se(parse_timestamp(buf, &q) >= 0);
a3da4a3d
ZJS
37 if (q != t) {
38 char tmp[FORMAT_TIMESTAMP_MAX];
39
40 log_error("round-trip failed: \"%s\" → \"%s\"",
41 buf, format_timestamp_us(tmp, sizeof(tmp), q));
42 }
92134489 43 assert_se(q == t);
cfbc22ab 44
68bdd2d2 45 assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
14c35ce7 46 log_info("%s", strna(buf_relative));
92134489 47}
cfbc22ab 48
b1c51766
HV
49static void test_should_parse(const char *p) {
50 usec_t t;
51
7635ab8e 52 log_info("Test: %s", p);
b1c51766 53 assert_se(parse_timestamp(p, &t) >= 0);
7635ab8e 54 log_info("\"%s\" → \"@%" PRI_USEC "\"", p, t);
b1c51766
HV
55}
56
063bc364
HV
57static void test_should_fail(const char *p) {
58 usec_t t;
7635ab8e 59 int r;
063bc364 60
7635ab8e
YW
61 log_info("Test: %s", p);
62 r = parse_timestamp(p, &t);
63 if (r >= 0)
64 log_info("\"%s\" → \"@%" PRI_USEC "\" (unexpected)", p, t);
65 else
66 log_info("parse_timestamp() returns %d (expected)", r);
67 assert_se(r < 0);
063bc364
HV
68}
69
70static void test_one(const char *p) {
71 _cleanup_free_ char *with_utc;
72
605405c6 73 with_utc = strjoin(p, " UTC");
063bc364
HV
74 test_should_pass(p);
75 test_should_pass(with_utc);
76}
77
78static void test_one_noutc(const char *p) {
79 _cleanup_free_ char *with_utc;
80
605405c6 81 with_utc = strjoin(p, " UTC");
063bc364
HV
82 test_should_pass(p);
83 test_should_fail(with_utc);
84}
85
92134489 86int main(int argc, char *argv[]) {
a3da4a3d
ZJS
87 log_set_max_level(LOG_DEBUG);
88 log_parse_environment();
89 log_open();
90
92134489
LP
91 test_one("17:41");
92 test_one("18:42:44");
063bc364
HV
93 test_one("18:42:44.0");
94 test_one("18:42:44.999999999999");
92134489
LP
95 test_one("12-10-02 12:13:14");
96 test_one("12-10-2 12:13:14");
97 test_one("12-10-03 12:13");
98 test_one("2012-12-30 18:42");
99 test_one("2012-10-02");
100 test_one("Tue 2012-10-02");
92134489
LP
101 test_one("yesterday");
102 test_one("today");
103 test_one("tomorrow");
48d26c01
IK
104 test_one_noutc("16:20 UTC");
105 test_one_noutc("16:20 Asia/Seoul");
106 test_one_noutc("tomorrow Asia/Seoul");
107 test_one_noutc("2012-12-30 18:42 Asia/Seoul");
68bdd2d2 108 test_one_noutc("now");
063bc364
HV
109 test_one_noutc("+2d");
110 test_one_noutc("+2y 4d");
111 test_one_noutc("5months ago");
112 test_one_noutc("@1395716396");
1bb4b028 113 test_should_parse("1970-1-1 UTC");
68bdd2d2
YW
114 test_should_pass("1970-1-1 00:00:01 UTC");
115 test_should_fail("1969-12-31 UTC");
116 test_should_fail("-100y");
117 test_should_fail("today UTC UTC");
48d26c01
IK
118 test_should_fail("now Asia/Seoul");
119 test_should_fail("+2d Asia/Seoul");
120 test_should_fail("@1395716396 Asia/Seoul");
1bb4b028 121#if SIZEOF_TIME_T == 8
7635ab8e 122 test_should_pass("9999-12-30 23:59:59 UTC");
1bb4b028
LP
123 test_should_fail("9999-12-31 00:00:00 UTC");
124 test_should_fail("10000-01-01 00:00:00 UTC");
125#elif SIZEOF_TIME_T == 4
7635ab8e
YW
126 test_should_pass("2038-01-19 03:14:07 UTC");
127 test_should_fail("2038-01-19 03:14:08 UTC");
1bb4b028 128#endif
decad910 129
cfbc22ab
LP
130 return 0;
131}