1 /* t-gettime.c - Module test for gettime.c
2 * Copyright (C) 2007, 2011 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
29 /* In case we do not have stdint.h and no other version of that
30 * conversion macro provide shortcut it. */
32 #define UINTMAX_C (c) (c)
35 #define pass() do { ; } while(0)
36 #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
37 __FILE__,__LINE__, (a)); \
43 #define INVALID ((time_t)(-1))
47 test_isotime2epoch (void)
49 struct { const char *string; time_t expected; } array [] = {
50 { "19700101T000001", 1 },
51 { "19700101T235959", 86399 },
52 { "19980815T143712", 903191832 },
53 { "19700101T000000", 0 },
54 { "19691231T235959", INVALID },
55 { "19000101T000000", INVALID },
57 { "19000101T00000", INVALID },
58 { "20010101t123456", INVALID },
59 { "20010101T123456", 978352496 },
60 { "20070629T160000", 1183132800 },
61 { "20070629T160000:", 1183132800 },
62 { "20070629T160000,", 1183132800 },
63 { "20070629T160000 ", 1183132800 },
64 { "20070629T160000\n", 1183132800 },
65 { "20070629T160000.", INVALID },
67 { "21060207T062815", (time_t)UINTMAX_C(0x0ffffffff) },
68 { "21060207T062816", (time_t)UINTMAX_C(0x100000000) },
69 { "21060207T062817", (time_t)UINTMAX_C(0x100000001) },
70 { "21060711T120001", (time_t)UINTMAX_C(4308292801) },
71 #endif /*SIZEOF_TIME_T > 4*/
78 for (idx=0; array[idx].string; idx++)
80 val = isotime2epoch (array[idx].string);
81 if (val != array[idx].expected )
85 fprintf (stderr, "string '%s' exp: %ld got: %ld\n",
86 array[idx].string, (long)array[idx].expected,
89 if (array[idx].expected != INVALID)
91 epoch2isotime (tbuf, val);
92 if (strlen (tbuf) != 15)
95 fprintf (stderr, "string '%s', time-t %ld, revert: '%s'\n",
96 array[idx].string, (long)val, tbuf);
99 if (strncmp (array[idx].string, tbuf, 15))
108 test_string2isotime (void)
113 const char *expected;
115 { "19700101T000001", 15, "19700101T000001" },
116 { "19700101T235959", 15, "19700101T235959" },
117 { "19980815T143712", 15, "19980815T143712" },
118 { "19700101T000000", 15, "19700101T000000" },
119 { "19691231T235959", 15, "19691231T235959" },
120 { "19000101T000000", 15, "19000101T000000" },
122 { "19000101T00000", 0, "" },
123 { "20010101t123456", 0, "" },
124 { "20010101T123456", 15, "20010101T123456" },
125 { "20070629T160000", 15, "20070629T160000" },
126 { "20070629T160000:", 15, "20070629T160000" },
127 { "20070629T160000,", 15, "20070629T160000" },
128 { "20070629T160000 ", 15, "20070629T160000" },
129 { "20070629T160000\n", 15,"20070629T160000" },
130 { "20070629T160000.", 0, "" },
131 { "1066-03-20", 10, "10660320T000000" },
132 { "1066-03-20,", 10, "10660320T000000" },
133 { "1066-03-20:", 0, "" },
134 { "1066-03-20 00", 13, "10660320T000000" },
135 { "1066-03-20 01", 13, "10660320T010000" },
136 { "1066-03-20 23", 13, "10660320T230000" },
137 { "1066-03-20 24", 0, "" },
138 { "1066-03-20 00:", 0, "" },
139 { "1066-03-20 00:3", 0, "" },
140 { "1066-03-20 00:31", 16, "10660320T003100" },
141 { "1066-03-20 00:31:47", 19, "10660320T003147" },
142 { "1066-03-20 00:31:47 ", 19, "10660320T003147" },
143 { "1066-03-20 00:31:47,", 19, "10660320T003147" },
144 { "1066-03-20 00:31:47:", 0, "" },
145 { "1-03-20 00:31:47:", 0, "" },
146 { "10-03-20 00:31:47:", 0, "" },
147 { "106-03-20 00:31:47:", 0, "" },
148 { "1066-23-20 00:31:47:", 0, "" },
149 { "1066-00-20 00:31:47:", 0, "" },
150 { "1066-0-20 00:31:47:", 0, "" },
151 { "1066-01-2 00:31:47:", 0, "" },
152 { "1066-01-2 00:31:47:", 0, "" },
153 { "1066-01-32 00:31:47:", 0, "" },
154 { "1066-01-00 00:31:47:", 0, "" },
155 { "1066-03-20 00:31:47:",11, "10660320T000000" },
156 { "1066-03-2000:31:47:", 0, "" },
157 { "10666-03-20 00:31:47:", 0, "" },
162 gnupg_isotime_t tbuf;
164 for (idx=0; array[idx].string; idx++)
166 result = string2isotime (tbuf, array[idx].string);
167 if (result != array[idx].result)
171 fprintf (stderr, "string '%s' expected: %d, got: %d\n",
172 array[idx].string, (int)array[idx].result, (int)result);
174 else if (result && strlen (tbuf) != 15)
178 fprintf (stderr, "string '%s' invalid isotime returned\n",
181 else if (result && strcmp (array[idx].expected, tbuf))
185 fprintf (stderr, "string '%s' bad isotime '%s' returned\n",
186 array[idx].string, tbuf);
193 test_isodate_human_to_tm (void)
200 { "1970-01-01", 1, 1970, 1, 1 },
201 { "1970-02-01", 1, 1970, 2, 1 },
202 { "1970-12-31", 1, 1970, 12, 31 },
203 { "1971-01-01", 1, 1971, 1, 1 },
204 { "1998-08-15", 1, 1998, 8, 15 },
205 { "2015-04-10", 1, 2015, 4, 10 },
206 { "2015-04-10 11:30",1, 2015, 4, 10 },
207 { "1969-12-31", 0, 0, 0, 0 },
208 { "1900-01-01", 0, 0, 0, 0 },
210 { "1970-12-32", 0, 0, 0, 0 },
211 { "1970-13-01", 0, 0, 0, 0 },
212 { "1970-01-00", 0, 0, 0, 0 },
213 { "1970-00-01", 0, 0, 0, 0 },
214 { "1970-00-01", 0, 0, 0, 0 },
215 { "1970", 0, 0, 0, 0 },
216 { "1970-01", 0, 0, 0, 0 },
217 { "1970-01-1", 0, 0, 0, 0 },
218 { "1970-1--01", 0, 0, 0, 0 },
219 { "1970-01-01,", 1, 1970, 1, 1 },
220 { "1970-01-01 ", 1, 1970, 1, 1 },
221 { "1970-01-01\t", 1, 1970, 1, 1 },
222 { "1970-01-01;", 0, 0, 0, 0 },
223 { "1970-01-01:", 0, 0, 0, 0 },
224 { "1970_01-01", 0, 0, 0, 0 },
225 { "1970-01_01", 0, 0, 0, 0 },
232 for (idx=0; array[idx].string; idx++)
234 okay = !isodate_human_to_tm (array[idx].string, &tmbuf);
235 if (okay != array[idx].okay)
239 fprintf (stderr, "string '%s' expected: %d, got: %d\n",
240 array[idx].string, (int)array[idx].okay, okay);
244 else if (tmbuf.tm_year + 1900 != array[idx].year
245 || tmbuf.tm_mon +1 != array[idx].mon
246 || tmbuf.tm_mday != array[idx].mday)
250 fprintf (stderr, "string '%s' returned %04d-%02d-%02d\n",
252 tmbuf.tm_year + 1900, tmbuf.tm_mon + 1, tmbuf.tm_mday);
254 else if (tmbuf.tm_sec || tmbuf.tm_min || tmbuf.tm_hour
255 || tmbuf.tm_isdst != -1)
259 fprintf (stderr, "string '%s' returned bad time part\n",
267 main (int argc, char **argv)
269 if (argc > 1 && !strcmp (argv[1], "--verbose"))
272 test_isotime2epoch ();
273 test_string2isotime ();
274 test_isodate_human_to_tm ();