]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-date.c
basic: use the return value of endswith
[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"
07630cea 25#include "string-util.h"
cfbc22ab 26
063bc364 27static 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
063bc364
HV
46static void test_should_fail(const char *p) {
47 usec_t t;
48
49 assert_se(parse_timestamp(p, &t) < 0);
50}
51
52static void test_one(const char *p) {
53 _cleanup_free_ char *with_utc;
54
55 log_info("Test: %s", p);
56 with_utc = strjoin(p, " UTC", NULL);
57 test_should_pass(p);
58 test_should_pass(with_utc);
59}
60
61static void test_one_noutc(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_fail(with_utc);
68}
69
92134489
LP
70int main(int argc, char *argv[]) {
71 test_one("17:41");
72 test_one("18:42:44");
063bc364
HV
73 test_one("18:42:44.0");
74 test_one("18:42:44.999999999999");
92134489
LP
75 test_one("12-10-02 12:13:14");
76 test_one("12-10-2 12:13:14");
77 test_one("12-10-03 12:13");
78 test_one("2012-12-30 18:42");
79 test_one("2012-10-02");
80 test_one("Tue 2012-10-02");
063bc364 81 test_one_noutc("now");
92134489
LP
82 test_one("yesterday");
83 test_one("today");
84 test_one("tomorrow");
063bc364
HV
85 test_one_noutc("+2d");
86 test_one_noutc("+2y 4d");
87 test_one_noutc("5months ago");
88 test_one_noutc("@1395716396");
89 test_one_noutc("today UTC");
decad910 90
cfbc22ab
LP
91 return 0;
92}