]>
Commit | Line | Data |
---|---|---|
cfbc22ab LP |
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 | ||
92134489 | 20 | #include <string.h> |
cfbc22ab | 21 | |
b5efdb8a | 22 | #include "alloc-util.h" |
07630cea | 23 | #include "string-util.h" |
b5efdb8a | 24 | #include "util.h" |
cfbc22ab | 25 | |
063bc364 | 26 | static void test_should_pass(const char *p) { |
92134489 | 27 | usec_t t, q; |
b07eb677 | 28 | char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *sp; |
cfbc22ab | 29 | |
92134489 | 30 | assert_se(parse_timestamp(p, &t) >= 0); |
063bc364 | 31 | format_timestamp_us(buf, sizeof(buf), t); |
d80e5b74 | 32 | log_info("\"%s\" → \"%s\"", p, buf); |
cfbc22ab | 33 | |
92134489 | 34 | /* Chop off timezone */ |
b07eb677 ZJS |
35 | sp = strrchr(buf, ' '); |
36 | assert_se(sp); | |
37 | *sp = 0; | |
cfbc22ab | 38 | |
92134489 LP |
39 | assert_se(parse_timestamp(buf, &q) >= 0); |
40 | assert_se(q == t); | |
cfbc22ab | 41 | |
14c35ce7 TA |
42 | format_timestamp_relative(buf_relative, sizeof(buf_relative), t); |
43 | log_info("%s", strna(buf_relative)); | |
92134489 LP |
44 | assert_se(parse_timestamp(buf, &q) >= 0); |
45 | } | |
cfbc22ab | 46 | |
b1c51766 HV |
47 | static void test_should_parse(const char *p) { |
48 | usec_t t; | |
49 | ||
50 | assert_se(parse_timestamp(p, &t) >= 0); | |
51 | } | |
52 | ||
063bc364 HV |
53 | static void test_should_fail(const char *p) { |
54 | usec_t t; | |
55 | ||
56 | assert_se(parse_timestamp(p, &t) < 0); | |
57 | } | |
58 | ||
59 | static void test_one(const char *p) { | |
60 | _cleanup_free_ char *with_utc; | |
61 | ||
62 | log_info("Test: %s", p); | |
605405c6 | 63 | with_utc = strjoin(p, " UTC"); |
063bc364 HV |
64 | test_should_pass(p); |
65 | test_should_pass(with_utc); | |
66 | } | |
67 | ||
68 | static void test_one_noutc(const char *p) { | |
69 | _cleanup_free_ char *with_utc; | |
70 | ||
71 | log_info("Test: %s", p); | |
605405c6 | 72 | with_utc = strjoin(p, " UTC"); |
063bc364 HV |
73 | test_should_pass(p); |
74 | test_should_fail(with_utc); | |
75 | } | |
76 | ||
92134489 LP |
77 | int main(int argc, char *argv[]) { |
78 | test_one("17:41"); | |
79 | test_one("18:42:44"); | |
063bc364 HV |
80 | test_one("18:42:44.0"); |
81 | test_one("18:42:44.999999999999"); | |
92134489 LP |
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"); | |
063bc364 | 88 | test_one_noutc("now"); |
92134489 LP |
89 | test_one("yesterday"); |
90 | test_one("today"); | |
91 | test_one("tomorrow"); | |
063bc364 HV |
92 | test_one_noutc("+2d"); |
93 | test_one_noutc("+2y 4d"); | |
94 | test_one_noutc("5months ago"); | |
95 | test_one_noutc("@1395716396"); | |
b1c51766 HV |
96 | test_should_parse("today UTC"); |
97 | test_should_fail("today UTC UTC"); | |
1bb4b028 LP |
98 | test_should_parse("1970-1-1 UTC"); |
99 | test_should_fail("1969-1-1 UTC"); | |
100 | #if SIZEOF_TIME_T == 8 | |
101 | test_should_parse("9999-12-30 23:59:59 UTC"); | |
102 | test_should_fail("9999-12-31 00:00:00 UTC"); | |
103 | test_should_fail("10000-01-01 00:00:00 UTC"); | |
104 | #elif SIZEOF_TIME_T == 4 | |
105 | test_should_parse("2038-01-19 03:14:07 UTC"); | |
106 | test_should_fail( "2038-01-19 03:14:08 UTC"); | |
107 | #endif | |
decad910 | 108 | |
cfbc22ab LP |
109 | return 0; |
110 | } |