chiark / gitweb /
Merge playlist branch against trunk to date.
[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);
3943e9ec
RK
49 /* Reject invalid formats */
50 check_fatal(dateparse("12"));
51 check_fatal(dateparse("12:34:56:23"));
52 /* Reject invalid values */
53 check_fatal(dateparse("25:34"));
54 check_fatal(dateparse("23:61"));
55 check_fatal(dateparse("23:23:62"));
d436bd52
RK
56}
57
58TEST(dateparse);
59
60/*
61Local Variables:
62c-basic-offset:2
63comment-column:40
64fill-column:79
65indent-tabs-mode:nil
66End:
67*/