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