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
], *sp
;
30 assert_se(parse_timestamp(p
, &t
) >= 0);
31 format_timestamp_us(buf
, sizeof(buf
), t
);
34 /* Chop off timezone */
35 sp
= strrchr(buf
, ' ');
39 assert_se(parse_timestamp(buf
, &q
) >= 0);
42 format_timestamp_relative(buf_relative
, sizeof(buf_relative
), t
);
43 log_info("%s", strna(buf_relative
));
44 assert_se(parse_timestamp(buf
, &q
) >= 0);
47 static void test_should_parse(const char *p
) {
50 assert_se(parse_timestamp(p
, &t
) >= 0);
53 static void test_should_fail(const char *p
) {
56 assert_se(parse_timestamp(p
, &t
) < 0);
59 static void test_one(const char *p
) {
60 _cleanup_free_
char *with_utc
;
62 log_info("Test: %s", p
);
63 with_utc
= strjoin(p
, " UTC");
65 test_should_pass(with_utc
);
68 static void test_one_noutc(const char *p
) {
69 _cleanup_free_
char *with_utc
;
71 log_info("Test: %s", p
);
72 with_utc
= strjoin(p
, " UTC");
74 test_should_fail(with_utc
);
77 int main(int argc
, char *argv
[]) {
80 test_one("18:42:44.0");
81 test_one("18:42:44.999999999999");
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");
88 test_one_noutc("now");
89 test_one("yesterday");
92 test_one_noutc("+2d");
93 test_one_noutc("+2y 4d");
94 test_one_noutc("5months ago");
95 test_one_noutc("@1395716396");
96 test_should_parse("today UTC");
97 test_should_fail("today UTC UTC");