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/>.
26 #define pass() do { ; } while(0)
27 #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
28 __FILE__,__LINE__, (a)); \
34 #define INVALID ((time_t)(-1))
38 test_isotime2epoch (void)
40 struct { const char *string; time_t expected; } array [] = {
41 { "19700101T000001", 1 },
42 { "19700101T235959", 86399 },
43 { "19980815T143712", 903191832 },
44 { "19700101T000000", 0 },
45 { "19691231T235959", INVALID },
46 { "19000101T000000", INVALID },
48 { "19000101T00000", INVALID },
49 { "20010101t123456", INVALID },
50 { "20010101T123456", 978352496 },
51 { "20070629T160000", 1183132800 },
52 { "20070629T160000:", 1183132800 },
53 { "20070629T160000,", 1183132800 },
54 { "20070629T160000 ", 1183132800 },
55 { "20070629T160000\n", 1183132800 },
56 { "20070629T160000.", INVALID },
58 { "21060207T062815", (time_t)0x0ffffffff },
59 { "21060207T062816", (time_t)0x100000000 },
60 { "21060207T062817", (time_t)0x100000001 },
61 { "21060711T120001", (time_t)4308292801 },
62 #endif /*SIZEOF_TIME_T > 4*/
69 for (idx=0; array[idx].string; idx++)
71 val = isotime2epoch (array[idx].string);
72 if (val != array[idx].expected )
76 fprintf (stderr, "string '%s' exp: %ld got: %ld\n",
77 array[idx].string, (long)array[idx].expected,
80 if (array[idx].expected != INVALID)
82 epoch2isotime (tbuf, val);
83 if (strlen (tbuf) != 15)
86 fprintf (stderr, "string '%s', time-t %ld, revert: '%s'\n",
87 array[idx].string, (long)val, tbuf);
90 if (strncmp (array[idx].string, tbuf, 15))
99 test_string2isotime (void)
104 const char *expected;
106 { "19700101T000001", 15, "19700101T000001" },
107 { "19700101T235959", 15, "19700101T235959" },
108 { "19980815T143712", 15, "19980815T143712" },
109 { "19700101T000000", 15, "19700101T000000" },
110 { "19691231T235959", 15, "19691231T235959" },
111 { "19000101T000000", 15, "19000101T000000" },
113 { "19000101T00000", 0, "" },
114 { "20010101t123456", 0, "" },
115 { "20010101T123456", 15, "20010101T123456" },
116 { "20070629T160000", 15, "20070629T160000" },
117 { "20070629T160000:", 15, "20070629T160000" },
118 { "20070629T160000,", 15, "20070629T160000" },
119 { "20070629T160000 ", 15, "20070629T160000" },
120 { "20070629T160000\n", 15,"20070629T160000" },
121 { "20070629T160000.", 0, "" },
122 { "1066-03-20", 10, "10660320T000000" },
123 { "1066-03-20,", 10, "10660320T000000" },
124 { "1066-03-20:", 0, "" },
125 { "1066-03-20 00", 13, "10660320T000000" },
126 { "1066-03-20 01", 13, "10660320T010000" },
127 { "1066-03-20 23", 13, "10660320T230000" },
128 { "1066-03-20 24", 0, "" },
129 { "1066-03-20 00:", 0, "" },
130 { "1066-03-20 00:3", 0, "" },
131 { "1066-03-20 00:31", 16, "10660320T003100" },
132 { "1066-03-20 00:31:47", 19, "10660320T003147" },
133 { "1066-03-20 00:31:47 ", 19, "10660320T003147" },
134 { "1066-03-20 00:31:47,", 19, "10660320T003147" },
135 { "1066-03-20 00:31:47:", 0, "" },
136 { "1-03-20 00:31:47:", 0, "" },
137 { "10-03-20 00:31:47:", 0, "" },
138 { "106-03-20 00:31:47:", 0, "" },
139 { "1066-23-20 00:31:47:", 0, "" },
140 { "1066-00-20 00:31:47:", 0, "" },
141 { "1066-0-20 00:31:47:", 0, "" },
142 { "1066-01-2 00:31:47:", 0, "" },
143 { "1066-01-2 00:31:47:", 0, "" },
144 { "1066-01-32 00:31:47:", 0, "" },
145 { "1066-01-00 00:31:47:", 0, "" },
146 { "1066-03-20 00:31:47:",11, "10660320T000000" },
147 { "1066-03-2000:31:47:", 0, "" },
148 { "10666-03-20 00:31:47:", 0, "" },
153 gnupg_isotime_t tbuf;
155 for (idx=0; array[idx].string; idx++)
157 result = string2isotime (tbuf, array[idx].string);
158 if (result != array[idx].result)
162 fprintf (stderr, "string '%s' expected: %d, got: %d\n",
163 array[idx].string, (int)array[idx].result, (int)result);
165 else if (result && strlen (tbuf) != 15)
169 fprintf (stderr, "string '%s' invalid isotime returned\n",
172 else if (result && strcmp (array[idx].expected, tbuf))
176 fprintf (stderr, "string '%s' bad isotime '%s' returned\n",
177 array[idx].string, tbuf);
184 test_isodate_human_to_tm (void)
191 { "1970-01-01", 1, 1970, 1, 1 },
192 { "1970-02-01", 1, 1970, 2, 1 },
193 { "1970-12-31", 1, 1970, 12, 31 },
194 { "1971-01-01", 1, 1971, 1, 1 },
195 { "1998-08-15", 1, 1998, 8, 15 },
196 { "2015-04-10", 1, 2015, 4, 10 },
197 { "2015-04-10 11:30",1, 2015, 4, 10 },
198 { "1969-12-31", 0, 0, 0, 0 },
199 { "1900-01-01", 0, 0, 0, 0 },
201 { "1970-12-32", 0, 0, 0, 0 },
202 { "1970-13-01", 0, 0, 0, 0 },
203 { "1970-01-00", 0, 0, 0, 0 },
204 { "1970-00-01", 0, 0, 0, 0 },
205 { "1970-00-01", 0, 0, 0, 0 },
206 { "1970", 0, 0, 0, 0 },
207 { "1970-01", 0, 0, 0, 0 },
208 { "1970-01-1", 0, 0, 0, 0 },
209 { "1970-1--01", 0, 0, 0, 0 },
210 { "1970-01-01,", 1, 1970, 1, 1 },
211 { "1970-01-01 ", 1, 1970, 1, 1 },
212 { "1970-01-01\t", 1, 1970, 1, 1 },
213 { "1970-01-01;", 0, 0, 0, 0 },
214 { "1970-01-01:", 0, 0, 0, 0 },
215 { "1970_01-01", 0, 0, 0, 0 },
216 { "1970-01_01", 0, 0, 0, 0 },
223 for (idx=0; array[idx].string; idx++)
225 okay = !isodate_human_to_tm (array[idx].string, &tmbuf);
226 if (okay != array[idx].okay)
230 fprintf (stderr, "string '%s' expected: %d, got: %d\n",
231 array[idx].string, (int)array[idx].okay, okay);
235 else if (tmbuf.tm_year + 1900 != array[idx].year
236 || tmbuf.tm_mon +1 != array[idx].mon
237 || tmbuf.tm_mday != array[idx].mday)
241 fprintf (stderr, "string '%s' returned %04d-%02d-%02d\n",
243 tmbuf.tm_year + 1900, tmbuf.tm_mon + 1, tmbuf.tm_mday);
245 else if (tmbuf.tm_sec || tmbuf.tm_min || tmbuf.tm_hour
246 || tmbuf.tm_isdst != -1)
250 fprintf (stderr, "string '%s' returned bad time part\n",
258 main (int argc, char **argv)
260 if (argc > 1 && !strcmp (argv[1], "--verbose"))
263 test_isotime2epoch ();
264 test_string2isotime ();
265 test_isodate_human_to_tm ();