chiark / gitweb /
e80e683bbfc133a594f96cc746a8318ce9bfaf34
[disorder] / libtests / t-dateparse.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2008 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
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  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include "test.h"
19 #include <time.h>
20 #include "dateparse.h"
21
22 static void check_date(time_t when,
23                        const char *fmt,
24                        struct tm *(*convert)(const time_t *)) {
25   char buffer[128];
26   time_t parsed;
27
28   strftime(buffer, sizeof buffer, fmt, convert(&when));
29   parsed = dateparse(buffer);
30   check_integer(parsed, when);
31   if(parsed != when)
32     fprintf(stderr, "format=%s formatted=%s\n", fmt, buffer);
33 }
34
35 static 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);
49 #if __FreeBSD__
50   fprintf(stderr, "strptime() is broken on FreeBSD - skipping further tests\n");
51   ++skipped;
52 #else
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"));
60 #endif
61 }
62
63 TEST(dateparse);
64
65 /*
66 Local Variables:
67 c-basic-offset:2
68 comment-column:40
69 fill-column:79
70 indent-tabs-mode:nil
71 End:
72 */