chiark / gitweb /
Doxygen-clean
[disorder] / libtests / t-dateparse.c
CommitLineData
d436bd52
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
d436bd52 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
d436bd52 8 * (at your option) any later version.
e7eb3a27
RK
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
d436bd52 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d436bd52
RK
17 */
18#include "test.h"
19#include <time.h>
20#include "dateparse.h"
21
22static void check_date(time_t when,
835a5b33 23 const char *fmt,
d436bd52
RK
24 struct tm *(*convert)(const time_t *)) {
25 char buffer[128];
26 time_t parsed;
27
835a5b33 28 strftime(buffer, sizeof buffer, fmt, convert(&when));
d436bd52
RK
29 parsed = dateparse(buffer);
30 check_integer(parsed, when);
31 if(parsed != when)
835a5b33 32 fprintf(stderr, "format=%s formatted=%s\n", fmt, buffer);
d436bd52
RK
33}
34
35static void test_dateparse(void) {
36 time_t now = time(0);
37 check_date(now, "%Y-%m-%d %H:%M:%S", localtime);
38#if 0 /* see dateparse.c */
39 check_date(now, "%Y-%m-%d %H:%M:%S %Z", localtime);
40 check_date(now, "%Y-%m-%d %H:%M:%S %Z", gmtime);
41#endif
42 check_date(now, "%c", localtime);
43 check_date(now, "%Ec", localtime);
44 check_date(now, "%X", localtime);
45 check_date(now, "%EX", localtime);
46 check_date(now, "%H:%M:%S", localtime);
47 /* This one needs a bodge: */
48 check_date(now - now % 60, "%H:%M", localtime);
cca956b1
RK
49#if __FreeBSD__
50 fprintf(stderr, "strptime() is broken on FreeBSD - skipping further tests\n");
51 ++skipped;
52#else
3943e9ec
RK
53 /* Reject invalid formats */
54 check_fatal(dateparse("12"));
55 check_fatal(dateparse("12:34:56:23"));
56 /* Reject invalid values */
57 check_fatal(dateparse("25:34"));
58 check_fatal(dateparse("23:61"));
59 check_fatal(dateparse("23:23:62"));
cca956b1 60#endif
d436bd52
RK
61}
62
63TEST(dateparse);
64
65/*
66Local Variables:
67c-basic-offset:2
68comment-column:40
69fill-column:79
70indent-tabs-mode:nil
71End:
72*/