| 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 | /* 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")); |
| 56 | } |
| 57 | |
| 58 | TEST(dateparse); |
| 59 | |
| 60 | /* |
| 61 | Local Variables: |
| 62 | c-basic-offset:2 |
| 63 | comment-column:40 |
| 64 | fill-column:79 |
| 65 | indent-tabs-mode:nil |
| 66 | End: |
| 67 | */ |