t_strptime.c revision 1.11 1 /* $NetBSD: t_strptime.c,v 1.11 2015/10/31 02:13:41 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Laight.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34 The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_strptime.c,v 1.11 2015/10/31 02:13:41 christos Exp $");
36
37 #include <time.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40
41 #include <atf-c.h>
42
43 static void
44 h_pass(const char *buf, const char *fmt, int len,
45 int tm_sec, int tm_min, int tm_hour, int tm_mday,
46 int tm_mon, int tm_year, int tm_wday, int tm_yday)
47 {
48 struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
49 const char *ret, *exp;
50
51 exp = buf + len;
52 ret = strptime(buf, fmt, &tm);
53
54 ATF_REQUIRE_MSG(ret == exp,
55 "strptime(\"%s\", \"%s\", tm): incorrect return code: "
56 "expected: %p, got: %p", buf, fmt, exp, ret);
57
58 #define H_REQUIRE_FIELD(field) \
59 ATF_REQUIRE_MSG(tm.field == field, \
60 "strptime(\"%s\", \"%s\", tm): incorrect %s: " \
61 "expected: %d, but got: %d", buf, fmt, \
62 ___STRING(field), field, tm.field)
63
64 H_REQUIRE_FIELD(tm_sec);
65 H_REQUIRE_FIELD(tm_min);
66 H_REQUIRE_FIELD(tm_hour);
67 H_REQUIRE_FIELD(tm_mday);
68 H_REQUIRE_FIELD(tm_mon);
69 H_REQUIRE_FIELD(tm_year);
70 H_REQUIRE_FIELD(tm_wday);
71 H_REQUIRE_FIELD(tm_yday);
72
73 #undef H_REQUIRE_FIELD
74 }
75
76 static void
77 h_fail(const char *buf, const char *fmt)
78 {
79 struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
80
81 ATF_REQUIRE_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
82 "\"%s\", &tm) should fail, but it didn't", buf, fmt);
83 }
84
85 static struct {
86 const char *name;
87 long offs;
88 } zt[] = {
89 { "Z", 0 },
90 { "UT", 0 },
91 { "UTC", 0 },
92 { "GMT", 0 },
93 { "EST", -18000 },
94 { "EDT", -14400 },
95 { "CST", -21600 },
96 { "CDT", -18000 },
97 { "MST", -25200 },
98 { "MDT", -21600 },
99 { "PST", -28800 },
100 { "PDT", -25200 },
101
102 { "VST", -1 },
103 { "VDT", -1 },
104
105 { "+03", 10800 },
106 { "-03", -10800 },
107 { "+0403", 14580 },
108 { "-0403", -14580 },
109 { "+04:03", 14580 },
110 { "-04:03", -14580 },
111 { "+14:00", 50400 },
112 { "-14:00", -50400 },
113 { "+23:59", 86340 },
114 { "-23:59", -86340 },
115
116 { "1", -1 },
117 { "03", -1 },
118 { "0304", -1 },
119 { "+1", -1 },
120 { "-203", -1 },
121 { "+12345", -1 },
122 { "+12:345", -1 },
123 { "+123:45", -1 },
124 { "+2400", -1 },
125 { "-2400", -1 },
126 { "+1060", -1 },
127 { "-1060", -1 },
128
129 { "A", -3600 },
130 { "B", -7200 },
131 { "C", -10800 },
132 { "D", -14400 },
133 { "E", -18000 },
134 { "F", -21600 },
135 { "G", -25200 },
136 { "H", -28800 },
137 { "I", -32400 },
138 { "L", -39600 },
139 { "M", -43200 },
140 { "N", 3600 },
141 { "O", 7200 },
142 { "P", 10800 },
143 { "Q", 14400 },
144 { "R", 18000 },
145 { "T", 25200 },
146 { "U", 28800 },
147 { "V", 32400 },
148 { "W", 36000 },
149 { "X", 39600 },
150 { "Y", 43200 },
151
152 { "J", -2 },
153
154 { "America/Los_Angeles", -28800 },
155 { "America/New_York", -18000 },
156 { "EST4EDT", -14400 },
157
158 { "Bogus", -1 },
159 };
160
161 static void
162 ztest(const char *name, const char *fmt, long value)
163 {
164 struct tm tm;
165 char *rv;
166
167 memset(&tm, 0, sizeof(tm));
168 if ((rv = strptime(name, fmt, &tm)) == NULL)
169 tm.tm_gmtoff = -1;
170 else if (rv == name && fmt[1] == 'Z')
171 value = 0;
172
173 switch (value) {
174 case -2:
175 value = -timezone;
176 break;
177 case -1:
178 if (fmt[1] == 'Z')
179 value = 0;
180 break;
181 default:
182 break;
183 }
184
185 ATF_REQUIRE_MSG(tm.tm_gmtoff == value,
186 "strptime(\"%s\", \"%s\", &tm): "
187 "expected: tm.tm_gmtoff=%ld, got: tm.tm_gmtoff=%ld",
188 name, fmt, value, tm.tm_gmtoff);
189 printf("%s %s %ld\n", name, fmt, tm.tm_gmtoff);
190 }
191
192 ATF_TC(common);
193
194 ATF_TC_HEAD(common, tc)
195 {
196
197 atf_tc_set_md_var(tc, "descr", "Checks strptime(3): various checks");
198 }
199
200 ATF_TC_BODY(common, tc)
201 {
202
203 h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %T %Y",
204 24, 46, 27, 23, 20, 0, 98, 2, 19);
205 h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %H:%M:%S %Y",
206 24, 46, 27, 23, 20, 0, 98, 2, 19);
207 h_pass("Tue Jan 20 23:27:46 1998", "%c",
208 24, 46, 27, 23, 20, 0, 98, 2, 19);
209 h_pass("Fri Mar 4 20:05:34 2005", "%a %b %e %H:%M:%S %Y",
210 24, 34, 5, 20, 4, 2, 105, 5, 62);
211 h_pass("5\t3 4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y",
212 21, 34, 5, 20, 4, 2, 105, 5, 62);
213 h_pass("Fri Mar 4 20:05:34 2005", "%c",
214 24, 34, 5, 20, 4, 2, 105, 5, 62);
215 }
216
217 ATF_TC(day);
218
219 ATF_TC_HEAD(day, tc)
220 {
221
222 atf_tc_set_md_var(tc, "descr",
223 "Checks strptime(3) day name conversions [aA]");
224 }
225
226 ATF_TC_BODY(day, tc)
227 {
228
229 h_pass("Sun", "%a", 3, -1, -1, -1, -1, -1, -1, 0, -1);
230 h_pass("Sunday", "%a", 6, -1, -1, -1, -1, -1, -1, 0, -1);
231 h_pass("Mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
232 h_pass("Monday", "%a", 6, -1, -1, -1, -1, -1, -1, 1, -1);
233 h_pass("Tue", "%a", 3, -1, -1, -1, -1, -1, -1, 2, -1);
234 h_pass("Tuesday", "%a", 7, -1, -1, -1, -1, -1, -1, 2, -1);
235 h_pass("Wed", "%a", 3, -1, -1, -1, -1, -1, -1, 3, -1);
236 h_pass("Wednesday", "%a", 9, -1, -1, -1, -1, -1, -1, 3, -1);
237 h_pass("Thu", "%a", 3, -1, -1, -1, -1, -1, -1, 4, -1);
238 h_pass("Thursday", "%a", 8, -1, -1, -1, -1, -1, -1, 4, -1);
239 h_pass("Fri", "%a", 3, -1, -1, -1, -1, -1, -1, 5, -1);
240 h_pass("Friday", "%a", 6, -1, -1, -1, -1, -1, -1, 5, -1);
241 h_pass("Sat", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
242 h_pass("Saturday", "%a", 8, -1, -1, -1, -1, -1, -1, 6, -1);
243 h_pass("Saturn", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
244 h_fail("Moon", "%a");
245 h_pass("Sun", "%A", 3, -1, -1, -1, -1, -1, -1, 0, -1);
246 h_pass("Sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
247 h_pass("Mon", "%A", 3, -1, -1, -1, -1, -1, -1, 1, -1);
248 h_pass("Monday", "%A", 6, -1, -1, -1, -1, -1, -1, 1, -1);
249 h_pass("Tue", "%A", 3, -1, -1, -1, -1, -1, -1, 2, -1);
250 h_pass("Tuesday", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
251 h_pass("Wed", "%A", 3, -1, -1, -1, -1, -1, -1, 3, -1);
252 h_pass("Wednesday", "%A", 9, -1, -1, -1, -1, -1, -1, 3, -1);
253 h_pass("Thu", "%A", 3, -1, -1, -1, -1, -1, -1, 4, -1);
254 h_pass("Thursday", "%A", 8, -1, -1, -1, -1, -1, -1, 4, -1);
255 h_pass("Fri", "%A", 3, -1, -1, -1, -1, -1, -1, 5, -1);
256 h_pass("Friday", "%A", 6, -1, -1, -1, -1, -1, -1, 5, -1);
257 h_pass("Sat", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
258 h_pass("Saturday", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
259 h_pass("Saturn", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
260 h_fail("Moon", "%A");
261
262 h_pass("mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
263 h_pass("tueSDay", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
264 h_pass("sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
265 h_fail("sunday", "%EA");
266 h_pass("SaturDay", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
267 h_fail("SaturDay", "%OA");
268 }
269
270 ATF_TC(hour);
271
272 ATF_TC_HEAD(hour, tc)
273 {
274
275 atf_tc_set_md_var(tc, "descr",
276 "Checks strptime(3) hour conversions [IH]");
277 }
278
279 ATF_TC_BODY(hour, tc)
280 {
281
282 h_fail("00", "%I");
283 h_fail("13", "%I");
284
285 h_pass("00", "%H", 2, -1, -1, 0, -1, -1, -1, -1, -1);
286 h_pass("12", "%H", 2, -1, -1, 12, -1, -1, -1, -1, -1);
287 h_pass("23", "%H", 2, -1, -1, 23, -1, -1, -1, -1, -1);
288 h_fail("24", "%H");
289 }
290
291
292 ATF_TC(month);
293
294 ATF_TC_HEAD(month, tc)
295 {
296
297 atf_tc_set_md_var(tc, "descr",
298 "Checks strptime(3) month name conversions [bB]");
299 }
300
301 ATF_TC_BODY(month, tc)
302 {
303
304 h_pass("Jan", "%b", 3, -1, -1, -1, -1, 0, -1, -1, -1);
305 h_pass("January", "%b", 7, -1, -1, -1, -1, 0, -1, -1, -1);
306 h_pass("Feb", "%b", 3, -1, -1, -1, -1, 1, -1, -1, -1);
307 h_pass("February", "%b", 8, -1, -1, -1, -1, 1, -1, -1, -1);
308 h_pass("Mar", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
309 h_pass("March", "%b", 5, -1, -1, -1, -1, 2, -1, -1, -1);
310 h_pass("Apr", "%b", 3, -1, -1, -1, -1, 3, -1, -1, -1);
311 h_pass("April", "%b", 5, -1, -1, -1, -1, 3, -1, -1, -1);
312 h_pass("May", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
313 h_pass("Jun", "%b", 3, -1, -1, -1, -1, 5, -1, -1, -1);
314 h_pass("June", "%b", 4, -1, -1, -1, -1, 5, -1, -1, -1);
315 h_pass("Jul", "%b", 3, -1, -1, -1, -1, 6, -1, -1, -1);
316 h_pass("July", "%b", 4, -1, -1, -1, -1, 6, -1, -1, -1);
317 h_pass("Aug", "%b", 3, -1, -1, -1, -1, 7, -1, -1, -1);
318 h_pass("August", "%b", 6, -1, -1, -1, -1, 7, -1, -1, -1);
319 h_pass("Sep", "%b", 3, -1, -1, -1, -1, 8, -1, -1, -1);
320 h_pass("September", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
321 h_pass("Oct", "%b", 3, -1, -1, -1, -1, 9, -1, -1, -1);
322 h_pass("October", "%b", 7, -1, -1, -1, -1, 9, -1, -1, -1);
323 h_pass("Nov", "%b", 3, -1, -1, -1, -1, 10, -1, -1, -1);
324 h_pass("November", "%b", 8, -1, -1, -1, -1, 10, -1, -1, -1);
325 h_pass("Dec", "%b", 3, -1, -1, -1, -1, 11, -1, -1, -1);
326 h_pass("December", "%b", 8, -1, -1, -1, -1, 11, -1, -1, -1);
327 h_pass("Mayor", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
328 h_pass("Mars", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
329 h_fail("Rover", "%b");
330 h_pass("Jan", "%B", 3, -1, -1, -1, -1, 0, -1, -1, -1);
331 h_pass("January", "%B", 7, -1, -1, -1, -1, 0, -1, -1, -1);
332 h_pass("Feb", "%B", 3, -1, -1, -1, -1, 1, -1, -1, -1);
333 h_pass("February", "%B", 8, -1, -1, -1, -1, 1, -1, -1, -1);
334 h_pass("Mar", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
335 h_pass("March", "%B", 5, -1, -1, -1, -1, 2, -1, -1, -1);
336 h_pass("Apr", "%B", 3, -1, -1, -1, -1, 3, -1, -1, -1);
337 h_pass("April", "%B", 5, -1, -1, -1, -1, 3, -1, -1, -1);
338 h_pass("May", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
339 h_pass("Jun", "%B", 3, -1, -1, -1, -1, 5, -1, -1, -1);
340 h_pass("June", "%B", 4, -1, -1, -1, -1, 5, -1, -1, -1);
341 h_pass("Jul", "%B", 3, -1, -1, -1, -1, 6, -1, -1, -1);
342 h_pass("July", "%B", 4, -1, -1, -1, -1, 6, -1, -1, -1);
343 h_pass("Aug", "%B", 3, -1, -1, -1, -1, 7, -1, -1, -1);
344 h_pass("August", "%B", 6, -1, -1, -1, -1, 7, -1, -1, -1);
345 h_pass("Sep", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
346 h_pass("September", "%B", 9, -1, -1, -1, -1, 8, -1, -1, -1);
347 h_pass("Oct", "%B", 3, -1, -1, -1, -1, 9, -1, -1, -1);
348 h_pass("October", "%B", 7, -1, -1, -1, -1, 9, -1, -1, -1);
349 h_pass("Nov", "%B", 3, -1, -1, -1, -1, 10, -1, -1, -1);
350 h_pass("November", "%B", 8, -1, -1, -1, -1, 10, -1, -1, -1);
351 h_pass("Dec", "%B", 3, -1, -1, -1, -1, 11, -1, -1, -1);
352 h_pass("December", "%B", 8, -1, -1, -1, -1, 11, -1, -1, -1);
353 h_pass("Mayor", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
354 h_pass("Mars", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
355 h_fail("Rover", "%B");
356
357 h_pass("september", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
358 h_pass("septembe", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
359 }
360
361 ATF_TC(seconds);
362
363 ATF_TC_HEAD(seconds, tc)
364 {
365
366 atf_tc_set_md_var(tc, "descr",
367 "Checks strptime(3) seconds conversions [S]");
368 }
369
370 ATF_TC_BODY(seconds, tc)
371 {
372
373 h_pass("0", "%S", 1, 0, -1, -1, -1, -1, -1, -1, -1);
374 h_pass("59", "%S", 2, 59, -1, -1, -1, -1, -1, -1, -1);
375 h_pass("60", "%S", 2, 60, -1, -1, -1, -1, -1, -1, -1);
376 h_pass("61", "%S", 2, 61, -1, -1, -1, -1, -1, -1, -1);
377 h_fail("62", "%S");
378 }
379
380 ATF_TC(year);
381
382 ATF_TC_HEAD(year, tc)
383 {
384
385 atf_tc_set_md_var(tc, "descr",
386 "Checks strptime(3) century/year conversions [CyY]");
387 }
388
389 ATF_TC_BODY(year, tc)
390 {
391
392 h_pass("x20y", "x%Cy", 4, -1, -1, -1, -1, -1, 100, -1, -1);
393 h_pass("x84y", "x%yy", 4, -1, -1, -1, -1, -1, 84, -1, -1);
394 h_pass("x2084y", "x%C%yy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
395 h_pass("x8420y", "x%y%Cy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
396 h_pass("%20845", "%%%C%y5", 6, -1, -1, -1, -1, -1, 184, -1, -1);
397 h_fail("%", "%E%");
398
399 h_pass("1980", "%Y", 4, -1, -1, -1, -1, -1, 80, -1, -1);
400 h_pass("1980", "%EY", 4, -1, -1, -1, -1, -1, 80, -1, -1);
401 }
402
403 ATF_TC(zone);
404
405 ATF_TC_HEAD(zone, tc)
406 {
407
408 atf_tc_set_md_var(tc, "descr",
409 "Checks strptime(3) timezone conversion [z]");
410 }
411
412
413 ATF_TC_BODY(zone, tc)
414 {
415 for (size_t i = 0; i < __arraycount(zt); i++)
416 ztest(zt[i].name, "%z", zt[i].offs);
417 }
418
419 ATF_TC(Zone);
420
421 ATF_TC_HEAD(Zone, tc)
422 {
423
424 atf_tc_set_md_var(tc, "descr",
425 "Checks strptime(3) timezone conversion [Z]");
426 }
427
428
429 ATF_TC_BODY(Zone, tc)
430 {
431 /* Test the hard-coded stuff */
432 setenv("TZ", "US/Eastern", 1);
433 ztest("GMT", "%Z", 0);
434 ztest("UTC", "%Z", 0);
435 ztest("US/Eastern", "%Z", -18000);
436
437 /* This is all handled by tzalloc for %Z */
438 for (size_t i = 0; i < __arraycount(zt); i++)
439 ztest(zt[i].name, "%Z", zt[i].offs);
440 }
441
442 ATF_TP_ADD_TCS(tp)
443 {
444
445 ATF_TP_ADD_TC(tp, common);
446 ATF_TP_ADD_TC(tp, day);
447 ATF_TP_ADD_TC(tp, hour);
448 ATF_TP_ADD_TC(tp, month);
449 ATF_TP_ADD_TC(tp, seconds);
450 ATF_TP_ADD_TC(tp, year);
451 ATF_TP_ADD_TC(tp, zone);
452 ATF_TP_ADD_TC(tp, Zone);
453
454 return atf_no_error();
455 }
456