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