]>
Commit | Line | Data |
---|---|---|
cfbc22ab LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2012 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
92134489 | 22 | #include <string.h> |
cfbc22ab | 23 | |
92134489 | 24 | #include "util.h" |
07630cea | 25 | #include "string-util.h" |
cfbc22ab | 26 | |
063bc364 | 27 | static void test_should_pass(const char *p) { |
92134489 LP |
28 | usec_t t, q; |
29 | char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX]; | |
cfbc22ab | 30 | |
92134489 | 31 | assert_se(parse_timestamp(p, &t) >= 0); |
063bc364 | 32 | format_timestamp_us(buf, sizeof(buf), t); |
14c35ce7 | 33 | log_info("%s", buf); |
cfbc22ab | 34 | |
92134489 LP |
35 | /* Chop off timezone */ |
36 | *strrchr(buf, ' ') = 0; | |
cfbc22ab | 37 | |
92134489 LP |
38 | assert_se(parse_timestamp(buf, &q) >= 0); |
39 | assert_se(q == t); | |
cfbc22ab | 40 | |
14c35ce7 TA |
41 | format_timestamp_relative(buf_relative, sizeof(buf_relative), t); |
42 | log_info("%s", strna(buf_relative)); | |
92134489 LP |
43 | assert_se(parse_timestamp(buf, &q) >= 0); |
44 | } | |
cfbc22ab | 45 | |
b1c51766 HV |
46 | static void test_should_parse(const char *p) { |
47 | usec_t t; | |
48 | ||
49 | assert_se(parse_timestamp(p, &t) >= 0); | |
50 | } | |
51 | ||
063bc364 HV |
52 | static void test_should_fail(const char *p) { |
53 | usec_t t; | |
54 | ||
55 | assert_se(parse_timestamp(p, &t) < 0); | |
56 | } | |
57 | ||
58 | static void test_one(const char *p) { | |
59 | _cleanup_free_ char *with_utc; | |
60 | ||
61 | log_info("Test: %s", p); | |
62 | with_utc = strjoin(p, " UTC", NULL); | |
63 | test_should_pass(p); | |
64 | test_should_pass(with_utc); | |
65 | } | |
66 | ||
67 | static void test_one_noutc(const char *p) { | |
68 | _cleanup_free_ char *with_utc; | |
69 | ||
70 | log_info("Test: %s", p); | |
71 | with_utc = strjoin(p, " UTC", NULL); | |
72 | test_should_pass(p); | |
73 | test_should_fail(with_utc); | |
74 | } | |
75 | ||
92134489 LP |
76 | int main(int argc, char *argv[]) { |
77 | test_one("17:41"); | |
78 | test_one("18:42:44"); | |
063bc364 HV |
79 | test_one("18:42:44.0"); |
80 | test_one("18:42:44.999999999999"); | |
92134489 LP |
81 | test_one("12-10-02 12:13:14"); |
82 | test_one("12-10-2 12:13:14"); | |
83 | test_one("12-10-03 12:13"); | |
84 | test_one("2012-12-30 18:42"); | |
85 | test_one("2012-10-02"); | |
86 | test_one("Tue 2012-10-02"); | |
063bc364 | 87 | test_one_noutc("now"); |
92134489 LP |
88 | test_one("yesterday"); |
89 | test_one("today"); | |
90 | test_one("tomorrow"); | |
063bc364 HV |
91 | test_one_noutc("+2d"); |
92 | test_one_noutc("+2y 4d"); | |
93 | test_one_noutc("5months ago"); | |
94 | test_one_noutc("@1395716396"); | |
b1c51766 HV |
95 | test_should_parse("today UTC"); |
96 | test_should_fail("today UTC UTC"); | |
decad910 | 97 | |
cfbc22ab LP |
98 | return 0; |
99 | } |