]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-date.c
test-date: check return value
[thirdparty/systemd.git] / src / test / test-date.c
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
22 #include <string.h>
23
24 #include "alloc-util.h"
25 #include "string-util.h"
26 #include "util.h"
27
28 static void test_should_pass(const char *p) {
29 usec_t t, q;
30 char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *sp;
31
32 assert_se(parse_timestamp(p, &t) >= 0);
33 format_timestamp_us(buf, sizeof(buf), t);
34 log_info("%s", buf);
35
36 /* Chop off timezone */
37 sp = strrchr(buf, ' ');
38 assert_se(sp);
39 *sp = 0;
40
41 assert_se(parse_timestamp(buf, &q) >= 0);
42 assert_se(q == t);
43
44 format_timestamp_relative(buf_relative, sizeof(buf_relative), t);
45 log_info("%s", strna(buf_relative));
46 assert_se(parse_timestamp(buf, &q) >= 0);
47 }
48
49 static void test_should_parse(const char *p) {
50 usec_t t;
51
52 assert_se(parse_timestamp(p, &t) >= 0);
53 }
54
55 static void test_should_fail(const char *p) {
56 usec_t t;
57
58 assert_se(parse_timestamp(p, &t) < 0);
59 }
60
61 static void test_one(const char *p) {
62 _cleanup_free_ char *with_utc;
63
64 log_info("Test: %s", p);
65 with_utc = strjoin(p, " UTC", NULL);
66 test_should_pass(p);
67 test_should_pass(with_utc);
68 }
69
70 static void test_one_noutc(const char *p) {
71 _cleanup_free_ char *with_utc;
72
73 log_info("Test: %s", p);
74 with_utc = strjoin(p, " UTC", NULL);
75 test_should_pass(p);
76 test_should_fail(with_utc);
77 }
78
79 int main(int argc, char *argv[]) {
80 test_one("17:41");
81 test_one("18:42:44");
82 test_one("18:42:44.0");
83 test_one("18:42:44.999999999999");
84 test_one("12-10-02 12:13:14");
85 test_one("12-10-2 12:13:14");
86 test_one("12-10-03 12:13");
87 test_one("2012-12-30 18:42");
88 test_one("2012-10-02");
89 test_one("Tue 2012-10-02");
90 test_one_noutc("now");
91 test_one("yesterday");
92 test_one("today");
93 test_one("tomorrow");
94 test_one_noutc("+2d");
95 test_one_noutc("+2y 4d");
96 test_one_noutc("5months ago");
97 test_one_noutc("@1395716396");
98 test_should_parse("today UTC");
99 test_should_fail("today UTC UTC");
100
101 return 0;
102 }