1 /* $NetBSD: t_strptime.c,v 1.12 2015/10/31 02:25:11 christos Exp $ */
4 * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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.12 2015/10/31 02:25:11 christos Exp $");
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)
48 struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
49 const char *ret, *exp;
52 ret = strptime(buf, fmt, &tm);
55 ATF_CHECK_MSG(ret == exp,
56 "strptime(\"%s\", \"%s\", tm): incorrect return code: "
57 "expected: %p, got: %p", buf, fmt, exp, ret);
59 #define H_REQUIRE_FIELD(field) \
60 ATF_CHECK_MSG(tm.field == field, \
61 "strptime(\"%s\", \"%s\", tm): incorrect %s: " \
62 "expected: %d, but got: %d", buf, fmt, \
63 ___STRING(field), field, tm.field)
65 ATF_REQUIRE_MSG(ret == exp,
66 "strptime(\"%s\", \"%s\", tm): incorrect return code: "
67 "expected: %p, got: %p", buf, fmt, exp, ret);
69 #define H_REQUIRE_FIELD(field) \
70 ATF_REQUIRE_MSG(tm.field == field, \
71 "strptime(\"%s\", \"%s\", tm): incorrect %s: " \
72 "expected: %d, but got: %d", buf, fmt, \
73 ___STRING(field), field, tm.field)
76 H_REQUIRE_FIELD(tm_sec);
77 H_REQUIRE_FIELD(tm_min);
78 H_REQUIRE_FIELD(tm_hour);
79 H_REQUIRE_FIELD(tm_mday);
80 H_REQUIRE_FIELD(tm_mon);
81 H_REQUIRE_FIELD(tm_year);
82 H_REQUIRE_FIELD(tm_wday);
83 H_REQUIRE_FIELD(tm_yday);
85 #undef H_REQUIRE_FIELD
89 h_fail(const char *buf, const char *fmt)
91 struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
94 ATF_CHECK_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
95 "\"%s\", &tm) should fail, but it didn't", buf, fmt);
97 ATF_REQUIRE_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
98 "\"%s\", &tm) should fail, but it didn't", buf, fmt);
127 { "-04:03", -14580 },
129 { "-14:00", -50400 },
131 { "-23:59", -86340 },
171 { "America/Los_Angeles", -28800 },
172 { "America/New_York", -18000 },
173 { "EST4EDT", -14400 },
179 ztest1(const char *name, const char *fmt, long value)
184 memset(&tm, 0, sizeof(tm));
185 if ((rv = strptime(name, fmt, &tm)) == NULL)
187 else if (rv == name && fmt[1] == 'Z')
202 ATF_REQUIRE_MSG(tm.tm_gmtoff == value,
203 "strptime(\"%s\", \"%s\", &tm): "
204 "expected: tm.tm_gmtoff=%ld, got: tm.tm_gmtoff=%ld",
205 name, fmt, value, tm.tm_gmtoff);
206 printf("%s %s %ld\n", name, fmt, tm.tm_gmtoff);
210 ztest(const char *fmt)
212 setenv("TZ", "US/Eastern", 1);
213 ztest1("GMT", fmt, 0);
214 ztest1("UTC", fmt, 0);
215 ztest1("US/Eastern", fmt, -18000);
216 for (size_t i = 0; i < __arraycount(zt); i++)
217 ztest1(zt[i].name, fmt, zt[i].offs);
222 ATF_TC_HEAD(common, tc)
225 atf_tc_set_md_var(tc, "descr", "Checks strptime(3): various checks");
228 ATF_TC_BODY(common, tc)
232 atf_tc_expect_fail("There are various issues with strptime on FreeBSD");
235 h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %T %Y",
236 24, 46, 27, 23, 20, 0, 98, 2, 19);
237 h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %H:%M:%S %Y",
238 24, 46, 27, 23, 20, 0, 98, 2, 19);
239 h_pass("Tue Jan 20 23:27:46 1998", "%c",
240 24, 46, 27, 23, 20, 0, 98, 2, 19);
241 h_pass("Fri Mar 4 20:05:34 2005", "%a %b %e %H:%M:%S %Y",
242 24, 34, 5, 20, 4, 2, 105, 5, 62);
243 h_pass("5\t3 4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y",
244 21, 34, 5, 20, 4, 2, 105, 5, 62);
245 h_pass("Fri Mar 4 20:05:34 2005", "%c",
246 24, 34, 5, 20, 4, 2, 105, 5, 62);
254 atf_tc_set_md_var(tc, "descr",
255 "Checks strptime(3) day name conversions [aA]");
261 h_pass("Sun", "%a", 3, -1, -1, -1, -1, -1, -1, 0, -1);
262 h_pass("Sunday", "%a", 6, -1, -1, -1, -1, -1, -1, 0, -1);
263 h_pass("Mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
264 h_pass("Monday", "%a", 6, -1, -1, -1, -1, -1, -1, 1, -1);
265 h_pass("Tue", "%a", 3, -1, -1, -1, -1, -1, -1, 2, -1);
266 h_pass("Tuesday", "%a", 7, -1, -1, -1, -1, -1, -1, 2, -1);
267 h_pass("Wed", "%a", 3, -1, -1, -1, -1, -1, -1, 3, -1);
268 h_pass("Wednesday", "%a", 9, -1, -1, -1, -1, -1, -1, 3, -1);
269 h_pass("Thu", "%a", 3, -1, -1, -1, -1, -1, -1, 4, -1);
270 h_pass("Thursday", "%a", 8, -1, -1, -1, -1, -1, -1, 4, -1);
271 h_pass("Fri", "%a", 3, -1, -1, -1, -1, -1, -1, 5, -1);
272 h_pass("Friday", "%a", 6, -1, -1, -1, -1, -1, -1, 5, -1);
273 h_pass("Sat", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
274 h_pass("Saturday", "%a", 8, -1, -1, -1, -1, -1, -1, 6, -1);
275 h_pass("Saturn", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
276 h_fail("Moon", "%a");
277 h_pass("Sun", "%A", 3, -1, -1, -1, -1, -1, -1, 0, -1);
278 h_pass("Sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
279 h_pass("Mon", "%A", 3, -1, -1, -1, -1, -1, -1, 1, -1);
280 h_pass("Monday", "%A", 6, -1, -1, -1, -1, -1, -1, 1, -1);
281 h_pass("Tue", "%A", 3, -1, -1, -1, -1, -1, -1, 2, -1);
282 h_pass("Tuesday", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
283 h_pass("Wed", "%A", 3, -1, -1, -1, -1, -1, -1, 3, -1);
284 h_pass("Wednesday", "%A", 9, -1, -1, -1, -1, -1, -1, 3, -1);
285 h_pass("Thu", "%A", 3, -1, -1, -1, -1, -1, -1, 4, -1);
286 h_pass("Thursday", "%A", 8, -1, -1, -1, -1, -1, -1, 4, -1);
287 h_pass("Fri", "%A", 3, -1, -1, -1, -1, -1, -1, 5, -1);
288 h_pass("Friday", "%A", 6, -1, -1, -1, -1, -1, -1, 5, -1);
289 h_pass("Sat", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
290 h_pass("Saturday", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
291 h_pass("Saturn", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
292 h_fail("Moon", "%A");
294 h_pass("mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
295 h_pass("tueSDay", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
296 h_pass("sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
298 h_fail("sunday", "%EA");
300 h_pass("Sunday", "%EA", 6, -1, -1, -1, -1, -1, -1, 0, -1);
302 h_pass("SaturDay", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
304 h_fail("SaturDay", "%OA");
306 h_pass("SaturDay", "%OA", 8, -1, -1, -1, -1, -1, -1, 6, -1);
312 ATF_TC_HEAD(hour, tc)
315 atf_tc_set_md_var(tc, "descr",
316 "Checks strptime(3) hour conversions [IH]");
319 ATF_TC_BODY(hour, tc)
325 h_pass("00", "%H", 2, -1, -1, 0, -1, -1, -1, -1, -1);
326 h_pass("12", "%H", 2, -1, -1, 12, -1, -1, -1, -1, -1);
327 h_pass("23", "%H", 2, -1, -1, 23, -1, -1, -1, -1, -1);
334 ATF_TC_HEAD(month, tc)
337 atf_tc_set_md_var(tc, "descr",
338 "Checks strptime(3) month name conversions [bB]");
341 ATF_TC_BODY(month, tc)
344 h_pass("Jan", "%b", 3, -1, -1, -1, -1, 0, -1, -1, -1);
345 h_pass("January", "%b", 7, -1, -1, -1, -1, 0, -1, -1, -1);
346 h_pass("Feb", "%b", 3, -1, -1, -1, -1, 1, -1, -1, -1);
347 h_pass("February", "%b", 8, -1, -1, -1, -1, 1, -1, -1, -1);
348 h_pass("Mar", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
349 h_pass("March", "%b", 5, -1, -1, -1, -1, 2, -1, -1, -1);
350 h_pass("Apr", "%b", 3, -1, -1, -1, -1, 3, -1, -1, -1);
351 h_pass("April", "%b", 5, -1, -1, -1, -1, 3, -1, -1, -1);
352 h_pass("May", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
353 h_pass("Jun", "%b", 3, -1, -1, -1, -1, 5, -1, -1, -1);
354 h_pass("June", "%b", 4, -1, -1, -1, -1, 5, -1, -1, -1);
355 h_pass("Jul", "%b", 3, -1, -1, -1, -1, 6, -1, -1, -1);
356 h_pass("July", "%b", 4, -1, -1, -1, -1, 6, -1, -1, -1);
357 h_pass("Aug", "%b", 3, -1, -1, -1, -1, 7, -1, -1, -1);
358 h_pass("August", "%b", 6, -1, -1, -1, -1, 7, -1, -1, -1);
359 h_pass("Sep", "%b", 3, -1, -1, -1, -1, 8, -1, -1, -1);
360 h_pass("September", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
361 h_pass("Oct", "%b", 3, -1, -1, -1, -1, 9, -1, -1, -1);
362 h_pass("October", "%b", 7, -1, -1, -1, -1, 9, -1, -1, -1);
363 h_pass("Nov", "%b", 3, -1, -1, -1, -1, 10, -1, -1, -1);
364 h_pass("November", "%b", 8, -1, -1, -1, -1, 10, -1, -1, -1);
365 h_pass("Dec", "%b", 3, -1, -1, -1, -1, 11, -1, -1, -1);
366 h_pass("December", "%b", 8, -1, -1, -1, -1, 11, -1, -1, -1);
367 h_pass("Mayor", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
368 h_pass("Mars", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
369 h_fail("Rover", "%b");
370 h_pass("Jan", "%B", 3, -1, -1, -1, -1, 0, -1, -1, -1);
371 h_pass("January", "%B", 7, -1, -1, -1, -1, 0, -1, -1, -1);
372 h_pass("Feb", "%B", 3, -1, -1, -1, -1, 1, -1, -1, -1);
373 h_pass("February", "%B", 8, -1, -1, -1, -1, 1, -1, -1, -1);
374 h_pass("Mar", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
375 h_pass("March", "%B", 5, -1, -1, -1, -1, 2, -1, -1, -1);
376 h_pass("Apr", "%B", 3, -1, -1, -1, -1, 3, -1, -1, -1);
377 h_pass("April", "%B", 5, -1, -1, -1, -1, 3, -1, -1, -1);
378 h_pass("May", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
379 h_pass("Jun", "%B", 3, -1, -1, -1, -1, 5, -1, -1, -1);
380 h_pass("June", "%B", 4, -1, -1, -1, -1, 5, -1, -1, -1);
381 h_pass("Jul", "%B", 3, -1, -1, -1, -1, 6, -1, -1, -1);
382 h_pass("July", "%B", 4, -1, -1, -1, -1, 6, -1, -1, -1);
383 h_pass("Aug", "%B", 3, -1, -1, -1, -1, 7, -1, -1, -1);
384 h_pass("August", "%B", 6, -1, -1, -1, -1, 7, -1, -1, -1);
385 h_pass("Sep", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
386 h_pass("September", "%B", 9, -1, -1, -1, -1, 8, -1, -1, -1);
387 h_pass("Oct", "%B", 3, -1, -1, -1, -1, 9, -1, -1, -1);
388 h_pass("October", "%B", 7, -1, -1, -1, -1, 9, -1, -1, -1);
389 h_pass("Nov", "%B", 3, -1, -1, -1, -1, 10, -1, -1, -1);
390 h_pass("November", "%B", 8, -1, -1, -1, -1, 10, -1, -1, -1);
391 h_pass("Dec", "%B", 3, -1, -1, -1, -1, 11, -1, -1, -1);
392 h_pass("December", "%B", 8, -1, -1, -1, -1, 11, -1, -1, -1);
393 h_pass("Mayor", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
394 h_pass("Mars", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
395 h_fail("Rover", "%B");
397 h_pass("september", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
398 h_pass("septembe", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
403 ATF_TC_HEAD(seconds, tc)
406 atf_tc_set_md_var(tc, "descr",
407 "Checks strptime(3) seconds conversions [S]");
410 ATF_TC_BODY(seconds, tc)
413 h_pass("0", "%S", 1, 0, -1, -1, -1, -1, -1, -1, -1);
414 h_pass("59", "%S", 2, 59, -1, -1, -1, -1, -1, -1, -1);
415 h_pass("60", "%S", 2, 60, -1, -1, -1, -1, -1, -1, -1);
416 h_pass("61", "%S", 2, 61, -1, -1, -1, -1, -1, -1, -1);
422 ATF_TC_HEAD(year, tc)
425 atf_tc_set_md_var(tc, "descr",
426 "Checks strptime(3) century/year conversions [CyY]");
429 ATF_TC_BODY(year, tc)
432 h_pass("x20y", "x%Cy", 4, -1, -1, -1, -1, -1, 100, -1, -1);
433 h_pass("x84y", "x%yy", 4, -1, -1, -1, -1, -1, 84, -1, -1);
434 h_pass("x2084y", "x%C%yy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
435 h_pass("x8420y", "x%y%Cy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
436 h_pass("%20845", "%%%C%y5", 6, -1, -1, -1, -1, -1, 184, -1, -1);
439 h_pass("1980", "%Y", 4, -1, -1, -1, -1, -1, 80, -1, -1);
440 h_pass("1980", "%EY", 4, -1, -1, -1, -1, -1, 80, -1, -1);
445 ATF_TC_HEAD(zone, tc)
448 atf_tc_set_md_var(tc, "descr",
449 "Checks strptime(3) timezone conversion [z]");
453 ATF_TC_BODY(zone, tc)
460 ATF_TC_HEAD(Zone, tc)
463 atf_tc_set_md_var(tc, "descr",
464 "Checks strptime(3) timezone conversion [Z]");
468 ATF_TC_BODY(Zone, tc)
476 ATF_TP_ADD_TC(tp, common);
477 ATF_TP_ADD_TC(tp, day);
478 ATF_TP_ADD_TC(tp, hour);
479 ATF_TP_ADD_TC(tp, month);
480 ATF_TP_ADD_TC(tp, seconds);
481 ATF_TP_ADD_TC(tp, year);
482 ATF_TP_ADD_TC(tp, zone);
483 ATF_TP_ADD_TC(tp, Zone);
485 return atf_no_error();