Home | History | Annotate | Line # | Download | only in time
t_strptime.c revision 1.15.4.1
      1  1.15.4.1    martin /* $NetBSD: t_strptime.c,v 1.15.4.1 2024/08/24 16:15:40 martin Exp $ */
      2       1.1  pgoyette 
      3       1.1  pgoyette /*-
      4       1.1  pgoyette  * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
      5       1.1  pgoyette  * All rights reserved.
      6       1.1  pgoyette  *
      7       1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  pgoyette  * by David Laight.
      9       1.1  pgoyette  *
     10       1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11       1.1  pgoyette  * modification, are permitted provided that the following conditions
     12       1.1  pgoyette  * are met:
     13       1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15       1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18       1.1  pgoyette  *
     19       1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  pgoyette  */
     31       1.1  pgoyette 
     32       1.1  pgoyette #include <sys/cdefs.h>
     33       1.1  pgoyette __COPYRIGHT("@(#) Copyright (c) 2008\
     34       1.1  pgoyette  The NetBSD Foundation, inc. All rights reserved.");
     35  1.15.4.1    martin __RCSID("$NetBSD: t_strptime.c,v 1.15.4.1 2024/08/24 16:15:40 martin Exp $");
     36       1.1  pgoyette 
     37  1.15.4.1    martin #include <errno.h>
     38  1.15.4.1    martin #include <inttypes.h>
     39      1.11  christos #include <stdio.h>
     40  1.15.4.1    martin #include <stdlib.h>
     41  1.15.4.1    martin #include <time.h>
     42       1.1  pgoyette 
     43       1.1  pgoyette #include <atf-c.h>
     44       1.1  pgoyette 
     45       1.1  pgoyette static void
     46       1.1  pgoyette h_pass(const char *buf, const char *fmt, int len,
     47       1.1  pgoyette     int tm_sec, int tm_min, int tm_hour, int tm_mday,
     48       1.1  pgoyette     int tm_mon, int tm_year, int tm_wday, int tm_yday)
     49       1.1  pgoyette {
     50       1.1  pgoyette 	struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
     51       1.1  pgoyette 	const char *ret, *exp;
     52       1.1  pgoyette 
     53       1.1  pgoyette 	exp = buf + len;
     54       1.1  pgoyette 	ret = strptime(buf, fmt, &tm);
     55       1.1  pgoyette 
     56      1.15      maya 	ATF_CHECK_MSG(ret == exp,
     57       1.1  pgoyette 	    "strptime(\"%s\", \"%s\", tm): incorrect return code: "
     58       1.1  pgoyette 	    "expected: %p, got: %p", buf, fmt, exp, ret);
     59       1.1  pgoyette 
     60       1.1  pgoyette #define H_REQUIRE_FIELD(field)						\
     61      1.15      maya 		ATF_CHECK_MSG(tm.field == field,			\
     62       1.1  pgoyette 		    "strptime(\"%s\", \"%s\", tm): incorrect %s: "	\
     63       1.1  pgoyette 		    "expected: %d, but got: %d", buf, fmt,		\
     64       1.1  pgoyette 		    ___STRING(field), field, tm.field)
     65       1.1  pgoyette 
     66       1.1  pgoyette 	H_REQUIRE_FIELD(tm_sec);
     67       1.1  pgoyette 	H_REQUIRE_FIELD(tm_min);
     68       1.1  pgoyette 	H_REQUIRE_FIELD(tm_hour);
     69       1.1  pgoyette 	H_REQUIRE_FIELD(tm_mday);
     70       1.1  pgoyette 	H_REQUIRE_FIELD(tm_mon);
     71       1.1  pgoyette 	H_REQUIRE_FIELD(tm_year);
     72       1.1  pgoyette 	H_REQUIRE_FIELD(tm_wday);
     73       1.1  pgoyette 	H_REQUIRE_FIELD(tm_yday);
     74       1.1  pgoyette 
     75       1.1  pgoyette #undef H_REQUIRE_FIELD
     76       1.1  pgoyette }
     77       1.1  pgoyette 
     78       1.1  pgoyette static void
     79       1.1  pgoyette h_fail(const char *buf, const char *fmt)
     80       1.1  pgoyette {
     81       1.1  pgoyette 	struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
     82       1.1  pgoyette 
     83      1.15      maya 	ATF_CHECK_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
     84       1.1  pgoyette 	    "\"%s\", &tm) should fail, but it didn't", buf, fmt);
     85       1.1  pgoyette }
     86       1.1  pgoyette 
     87      1.10  christos static struct {
     88      1.10  christos 	const char *name;
     89      1.10  christos 	long offs;
     90      1.10  christos } zt[] = {
     91      1.10  christos 	{ "Z",				0 },
     92      1.10  christos 	{ "UT",				0 },
     93      1.10  christos 	{ "UTC",			0 },
     94      1.10  christos 	{ "GMT",			0 },
     95      1.10  christos 	{ "EST",			-18000 },
     96      1.10  christos 	{ "EDT",			-14400 },
     97      1.10  christos 	{ "CST",			-21600 },
     98      1.10  christos 	{ "CDT",			-18000 },
     99      1.10  christos 	{ "MST",			-25200 },
    100      1.10  christos 	{ "MDT",			-21600 },
    101      1.10  christos 	{ "PST",			-28800 },
    102      1.10  christos 	{ "PDT",			-25200 },
    103      1.10  christos 
    104      1.10  christos 	{ "VST",			-1 },
    105      1.10  christos 	{ "VDT",			-1 },
    106      1.10  christos 
    107      1.10  christos 	{ "+03",			10800 },
    108      1.10  christos 	{ "-03",			-10800 },
    109      1.10  christos 	{ "+0403",			14580 },
    110      1.10  christos 	{ "-0403",			-14580 },
    111      1.10  christos 	{ "+04:03",			14580 },
    112      1.10  christos 	{ "-04:03",			-14580 },
    113      1.10  christos 	{ "+14:00",			50400 },
    114      1.10  christos 	{ "-14:00",			-50400 },
    115      1.10  christos 	{ "+23:59",			86340 },
    116      1.10  christos 	{ "-23:59",			-86340 },
    117      1.10  christos 
    118      1.10  christos 	{ "1",				-1 },
    119      1.10  christos 	{ "03",				-1 },
    120      1.10  christos 	{ "0304",			-1 },
    121      1.10  christos 	{ "+1",				-1 },
    122      1.10  christos 	{ "-203",			-1 },
    123      1.10  christos 	{ "+12345",			-1 },
    124      1.10  christos 	{ "+12:345",			-1 },
    125      1.10  christos 	{ "+123:45",			-1 },
    126      1.10  christos 	{ "+2400",			-1 },
    127      1.10  christos 	{ "-2400",			-1 },
    128      1.10  christos 	{ "+1060",			-1 },
    129      1.10  christos 	{ "-1060",			-1 },
    130      1.10  christos 
    131      1.13  ginsbach 	{ "A",				3600 },
    132      1.13  ginsbach 	{ "B",				7200 },
    133      1.13  ginsbach 	{ "C",				10800 },
    134      1.13  ginsbach 	{ "D",				14400 },
    135      1.13  ginsbach 	{ "E",				18000 },
    136      1.13  ginsbach 	{ "F",				21600 },
    137      1.13  ginsbach 	{ "G",				25200 },
    138      1.13  ginsbach 	{ "H",				28800 },
    139      1.13  ginsbach 	{ "I",				32400 },
    140      1.13  ginsbach 	{ "L",				39600 },
    141      1.13  ginsbach 	{ "M",				43200 },
    142      1.13  ginsbach 	{ "N",				-3600 },
    143      1.13  ginsbach 	{ "O",				-7200 },
    144      1.13  ginsbach 	{ "P",				-10800 },
    145      1.13  ginsbach 	{ "Q",				-14400 },
    146      1.13  ginsbach 	{ "R",				-18000 },
    147      1.13  ginsbach 	{ "T",				-25200 },
    148      1.13  ginsbach 	{ "U",				-28800 },
    149      1.13  ginsbach 	{ "V",				-32400 },
    150      1.13  ginsbach 	{ "W",				-36000 },
    151      1.13  ginsbach 	{ "X",				-39600 },
    152      1.13  ginsbach 	{ "Y",				-43200 },
    153      1.10  christos 
    154      1.11  christos 	{ "J",				-2 },
    155      1.10  christos 
    156      1.10  christos 	{ "America/Los_Angeles",	-28800 },
    157      1.10  christos 	{ "America/New_York",		-18000 },
    158      1.10  christos 	{ "EST4EDT",			-14400 },
    159      1.10  christos 
    160      1.10  christos 	{ "Bogus",			-1 },
    161      1.10  christos };
    162      1.10  christos 
    163      1.10  christos static void
    164      1.12  christos ztest1(const char *name, const char *fmt, long value)
    165      1.10  christos {
    166      1.10  christos 	struct tm tm;
    167      1.11  christos 	char *rv;
    168      1.10  christos 
    169      1.10  christos 	memset(&tm, 0, sizeof(tm));
    170      1.11  christos 	if ((rv = strptime(name, fmt, &tm)) == NULL)
    171      1.10  christos 		tm.tm_gmtoff = -1;
    172      1.11  christos 	else if (rv == name && fmt[1] == 'Z')
    173      1.11  christos 		value = 0;
    174      1.11  christos 
    175      1.11  christos 	switch (value) {
    176      1.11  christos 	case -2:
    177      1.11  christos 		value = -timezone;
    178      1.11  christos 		break;
    179      1.11  christos 	case -1:
    180      1.11  christos 		if (fmt[1] == 'Z')
    181      1.11  christos 			value = 0;
    182      1.11  christos 		break;
    183      1.11  christos 	default:
    184      1.11  christos 		break;
    185      1.11  christos 	}
    186      1.11  christos 
    187      1.15      maya 	ATF_CHECK_MSG(tm.tm_gmtoff == value,
    188      1.10  christos 	    "strptime(\"%s\", \"%s\", &tm): "
    189      1.10  christos 	    "expected: tm.tm_gmtoff=%ld, got: tm.tm_gmtoff=%ld",
    190      1.10  christos 	    name, fmt, value, tm.tm_gmtoff);
    191      1.11  christos 	printf("%s %s %ld\n", name, fmt, tm.tm_gmtoff);
    192      1.10  christos }
    193      1.10  christos 
    194      1.12  christos static void
    195      1.12  christos ztest(const char *fmt)
    196      1.12  christos {
    197      1.12  christos 	setenv("TZ", "US/Eastern", 1);
    198      1.12  christos 	ztest1("GMT", fmt, 0);
    199      1.12  christos 	ztest1("UTC", fmt, 0);
    200      1.12  christos 	ztest1("US/Eastern", fmt, -18000);
    201      1.12  christos 	for (size_t i = 0; i < __arraycount(zt); i++)
    202      1.12  christos 		ztest1(zt[i].name, fmt, zt[i].offs);
    203      1.12  christos }
    204      1.12  christos 
    205       1.1  pgoyette ATF_TC(common);
    206       1.1  pgoyette 
    207       1.1  pgoyette ATF_TC_HEAD(common, tc)
    208       1.1  pgoyette {
    209       1.1  pgoyette 
    210       1.1  pgoyette 	atf_tc_set_md_var(tc, "descr", "Checks strptime(3): various checks");
    211       1.1  pgoyette }
    212       1.1  pgoyette 
    213       1.1  pgoyette ATF_TC_BODY(common, tc)
    214       1.1  pgoyette {
    215       1.1  pgoyette 
    216       1.1  pgoyette 	h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %T %Y",
    217       1.6  christos 		24, 46, 27, 23, 20, 0, 98, 2, 19);
    218       1.1  pgoyette 	h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %H:%M:%S %Y",
    219       1.6  christos 		24, 46, 27, 23, 20, 0, 98, 2, 19);
    220       1.1  pgoyette 	h_pass("Tue Jan 20 23:27:46 1998", "%c",
    221       1.6  christos 		24, 46, 27, 23, 20, 0, 98, 2, 19);
    222       1.1  pgoyette 	h_pass("Fri Mar  4 20:05:34 2005", "%a %b %e %H:%M:%S %Y",
    223       1.6  christos 		24, 34, 5, 20, 4, 2, 105, 5, 62);
    224       1.1  pgoyette 	h_pass("5\t3  4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y",
    225       1.6  christos 		21, 34, 5, 20, 4, 2, 105, 5, 62);
    226       1.1  pgoyette 	h_pass("Fri Mar  4 20:05:34 2005", "%c",
    227       1.6  christos 		24, 34, 5, 20, 4, 2, 105, 5, 62);
    228       1.1  pgoyette }
    229       1.1  pgoyette 
    230       1.1  pgoyette ATF_TC(day);
    231       1.1  pgoyette 
    232       1.1  pgoyette ATF_TC_HEAD(day, tc)
    233       1.1  pgoyette {
    234       1.1  pgoyette 
    235       1.2  ginsbach 	atf_tc_set_md_var(tc, "descr",
    236       1.2  ginsbach 			  "Checks strptime(3) day name conversions [aA]");
    237       1.1  pgoyette }
    238       1.1  pgoyette 
    239       1.1  pgoyette ATF_TC_BODY(day, tc)
    240       1.1  pgoyette {
    241       1.1  pgoyette 
    242       1.1  pgoyette 	h_pass("Sun", "%a", 3, -1, -1, -1, -1, -1, -1, 0, -1);
    243       1.1  pgoyette 	h_pass("Sunday", "%a", 6, -1, -1, -1, -1, -1, -1, 0, -1);
    244       1.1  pgoyette 	h_pass("Mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
    245       1.1  pgoyette 	h_pass("Monday", "%a", 6, -1, -1, -1, -1, -1, -1, 1, -1);
    246       1.1  pgoyette 	h_pass("Tue", "%a", 3, -1, -1, -1, -1, -1, -1, 2, -1);
    247       1.1  pgoyette 	h_pass("Tuesday", "%a", 7, -1, -1, -1, -1, -1, -1, 2, -1);
    248       1.1  pgoyette 	h_pass("Wed", "%a", 3, -1, -1, -1, -1, -1, -1, 3, -1);
    249       1.1  pgoyette 	h_pass("Wednesday", "%a", 9, -1, -1, -1, -1, -1, -1, 3, -1);
    250       1.1  pgoyette 	h_pass("Thu", "%a", 3, -1, -1, -1, -1, -1, -1, 4, -1);
    251       1.1  pgoyette 	h_pass("Thursday", "%a", 8, -1, -1, -1, -1, -1, -1, 4, -1);
    252       1.1  pgoyette 	h_pass("Fri", "%a", 3, -1, -1, -1, -1, -1, -1, 5, -1);
    253       1.1  pgoyette 	h_pass("Friday", "%a", 6, -1, -1, -1, -1, -1, -1, 5, -1);
    254       1.1  pgoyette 	h_pass("Sat", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
    255       1.1  pgoyette 	h_pass("Saturday", "%a", 8, -1, -1, -1, -1, -1, -1, 6, -1);
    256       1.1  pgoyette 	h_pass("Saturn", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
    257       1.1  pgoyette 	h_fail("Moon", "%a");
    258       1.1  pgoyette 	h_pass("Sun", "%A", 3, -1, -1, -1, -1, -1, -1, 0, -1);
    259       1.1  pgoyette 	h_pass("Sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
    260       1.1  pgoyette 	h_pass("Mon", "%A", 3, -1, -1, -1, -1, -1, -1, 1, -1);
    261       1.1  pgoyette 	h_pass("Monday", "%A", 6, -1, -1, -1, -1, -1, -1, 1, -1);
    262       1.1  pgoyette 	h_pass("Tue", "%A", 3, -1, -1, -1, -1, -1, -1, 2, -1);
    263       1.1  pgoyette 	h_pass("Tuesday", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
    264       1.1  pgoyette 	h_pass("Wed", "%A", 3, -1, -1, -1, -1, -1, -1, 3, -1);
    265       1.1  pgoyette 	h_pass("Wednesday", "%A", 9, -1, -1, -1, -1, -1, -1, 3, -1);
    266       1.1  pgoyette 	h_pass("Thu", "%A", 3, -1, -1, -1, -1, -1, -1, 4, -1);
    267       1.1  pgoyette 	h_pass("Thursday", "%A", 8, -1, -1, -1, -1, -1, -1, 4, -1);
    268       1.1  pgoyette 	h_pass("Fri", "%A", 3, -1, -1, -1, -1, -1, -1, 5, -1);
    269       1.1  pgoyette 	h_pass("Friday", "%A", 6, -1, -1, -1, -1, -1, -1, 5, -1);
    270       1.1  pgoyette 	h_pass("Sat", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
    271       1.1  pgoyette 	h_pass("Saturday", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
    272       1.1  pgoyette 	h_pass("Saturn", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
    273       1.1  pgoyette 	h_fail("Moon", "%A");
    274       1.1  pgoyette 
    275       1.1  pgoyette 	h_pass("mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
    276       1.1  pgoyette 	h_pass("tueSDay", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
    277       1.1  pgoyette 	h_pass("sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
    278       1.1  pgoyette 	h_fail("sunday", "%EA");
    279       1.1  pgoyette 	h_pass("SaturDay", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
    280       1.1  pgoyette 	h_fail("SaturDay", "%OA");
    281       1.1  pgoyette }
    282       1.1  pgoyette 
    283       1.5  ginsbach ATF_TC(hour);
    284       1.5  ginsbach 
    285       1.5  ginsbach ATF_TC_HEAD(hour, tc)
    286       1.5  ginsbach {
    287       1.5  ginsbach 
    288       1.5  ginsbach 	atf_tc_set_md_var(tc, "descr",
    289       1.5  ginsbach 			  "Checks strptime(3) hour conversions [IH]");
    290       1.5  ginsbach }
    291       1.5  ginsbach 
    292       1.5  ginsbach ATF_TC_BODY(hour, tc)
    293       1.5  ginsbach {
    294       1.5  ginsbach 
    295       1.5  ginsbach 	h_fail("00", "%I");
    296       1.5  ginsbach 	h_fail("13", "%I");
    297       1.5  ginsbach 
    298       1.5  ginsbach 	h_pass("00", "%H", 2, -1, -1, 0, -1, -1, -1, -1, -1);
    299       1.5  ginsbach 	h_pass("12", "%H", 2, -1, -1, 12, -1, -1, -1, -1, -1);
    300       1.5  ginsbach 	h_pass("23", "%H", 2, -1, -1, 23, -1, -1, -1, -1, -1);
    301       1.5  ginsbach 	h_fail("24", "%H");
    302       1.5  ginsbach }
    303       1.5  ginsbach 
    304       1.5  ginsbach 
    305       1.1  pgoyette ATF_TC(month);
    306       1.1  pgoyette 
    307       1.1  pgoyette ATF_TC_HEAD(month, tc)
    308       1.1  pgoyette {
    309       1.1  pgoyette 
    310       1.2  ginsbach 	atf_tc_set_md_var(tc, "descr",
    311       1.2  ginsbach 			  "Checks strptime(3) month name conversions [bB]");
    312       1.1  pgoyette }
    313       1.1  pgoyette 
    314       1.1  pgoyette ATF_TC_BODY(month, tc)
    315       1.1  pgoyette {
    316       1.1  pgoyette 
    317       1.1  pgoyette 	h_pass("Jan", "%b", 3, -1, -1, -1, -1, 0, -1, -1, -1);
    318       1.1  pgoyette 	h_pass("January", "%b", 7, -1, -1, -1, -1, 0, -1, -1, -1);
    319       1.1  pgoyette 	h_pass("Feb", "%b", 3, -1, -1, -1, -1, 1, -1, -1, -1);
    320       1.1  pgoyette 	h_pass("February", "%b", 8, -1, -1, -1, -1, 1, -1, -1, -1);
    321       1.1  pgoyette 	h_pass("Mar", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
    322       1.1  pgoyette 	h_pass("March", "%b", 5, -1, -1, -1, -1, 2, -1, -1, -1);
    323       1.1  pgoyette 	h_pass("Apr", "%b", 3, -1, -1, -1, -1, 3, -1, -1, -1);
    324       1.1  pgoyette 	h_pass("April", "%b", 5, -1, -1, -1, -1, 3, -1, -1, -1);
    325       1.1  pgoyette 	h_pass("May", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
    326       1.1  pgoyette 	h_pass("Jun", "%b", 3, -1, -1, -1, -1, 5, -1, -1, -1);
    327       1.1  pgoyette 	h_pass("June", "%b", 4, -1, -1, -1, -1, 5, -1, -1, -1);
    328       1.1  pgoyette 	h_pass("Jul", "%b", 3, -1, -1, -1, -1, 6, -1, -1, -1);
    329       1.1  pgoyette 	h_pass("July", "%b", 4, -1, -1, -1, -1, 6, -1, -1, -1);
    330       1.1  pgoyette 	h_pass("Aug", "%b", 3, -1, -1, -1, -1, 7, -1, -1, -1);
    331       1.1  pgoyette 	h_pass("August", "%b", 6, -1, -1, -1, -1, 7, -1, -1, -1);
    332       1.1  pgoyette 	h_pass("Sep", "%b", 3, -1, -1, -1, -1, 8, -1, -1, -1);
    333       1.1  pgoyette 	h_pass("September", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
    334       1.1  pgoyette 	h_pass("Oct", "%b", 3, -1, -1, -1, -1, 9, -1, -1, -1);
    335       1.1  pgoyette 	h_pass("October", "%b", 7, -1, -1, -1, -1, 9, -1, -1, -1);
    336       1.1  pgoyette 	h_pass("Nov", "%b", 3, -1, -1, -1, -1, 10, -1, -1, -1);
    337       1.1  pgoyette 	h_pass("November", "%b", 8, -1, -1, -1, -1, 10, -1, -1, -1);
    338       1.1  pgoyette 	h_pass("Dec", "%b", 3, -1, -1, -1, -1, 11, -1, -1, -1);
    339       1.1  pgoyette 	h_pass("December", "%b", 8, -1, -1, -1, -1, 11, -1, -1, -1);
    340       1.1  pgoyette 	h_pass("Mayor", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
    341       1.1  pgoyette 	h_pass("Mars", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
    342       1.1  pgoyette 	h_fail("Rover", "%b");
    343       1.1  pgoyette 	h_pass("Jan", "%B", 3, -1, -1, -1, -1, 0, -1, -1, -1);
    344       1.1  pgoyette 	h_pass("January", "%B", 7, -1, -1, -1, -1, 0, -1, -1, -1);
    345       1.1  pgoyette 	h_pass("Feb", "%B", 3, -1, -1, -1, -1, 1, -1, -1, -1);
    346       1.1  pgoyette 	h_pass("February", "%B", 8, -1, -1, -1, -1, 1, -1, -1, -1);
    347       1.1  pgoyette 	h_pass("Mar", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
    348       1.1  pgoyette 	h_pass("March", "%B", 5, -1, -1, -1, -1, 2, -1, -1, -1);
    349       1.1  pgoyette 	h_pass("Apr", "%B", 3, -1, -1, -1, -1, 3, -1, -1, -1);
    350       1.1  pgoyette 	h_pass("April", "%B", 5, -1, -1, -1, -1, 3, -1, -1, -1);
    351       1.1  pgoyette 	h_pass("May", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
    352       1.1  pgoyette 	h_pass("Jun", "%B", 3, -1, -1, -1, -1, 5, -1, -1, -1);
    353       1.1  pgoyette 	h_pass("June", "%B", 4, -1, -1, -1, -1, 5, -1, -1, -1);
    354       1.1  pgoyette 	h_pass("Jul", "%B", 3, -1, -1, -1, -1, 6, -1, -1, -1);
    355       1.1  pgoyette 	h_pass("July", "%B", 4, -1, -1, -1, -1, 6, -1, -1, -1);
    356       1.1  pgoyette 	h_pass("Aug", "%B", 3, -1, -1, -1, -1, 7, -1, -1, -1);
    357       1.1  pgoyette 	h_pass("August", "%B", 6, -1, -1, -1, -1, 7, -1, -1, -1);
    358       1.1  pgoyette 	h_pass("Sep", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
    359       1.1  pgoyette 	h_pass("September", "%B", 9, -1, -1, -1, -1, 8, -1, -1, -1);
    360       1.1  pgoyette 	h_pass("Oct", "%B", 3, -1, -1, -1, -1, 9, -1, -1, -1);
    361       1.1  pgoyette 	h_pass("October", "%B", 7, -1, -1, -1, -1, 9, -1, -1, -1);
    362       1.1  pgoyette 	h_pass("Nov", "%B", 3, -1, -1, -1, -1, 10, -1, -1, -1);
    363       1.1  pgoyette 	h_pass("November", "%B", 8, -1, -1, -1, -1, 10, -1, -1, -1);
    364       1.1  pgoyette 	h_pass("Dec", "%B", 3, -1, -1, -1, -1, 11, -1, -1, -1);
    365       1.1  pgoyette 	h_pass("December", "%B", 8, -1, -1, -1, -1, 11, -1, -1, -1);
    366       1.1  pgoyette 	h_pass("Mayor", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
    367       1.1  pgoyette 	h_pass("Mars", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
    368       1.1  pgoyette 	h_fail("Rover", "%B");
    369       1.1  pgoyette 
    370       1.1  pgoyette 	h_pass("september", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
    371       1.1  pgoyette 	h_pass("septembe", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
    372       1.1  pgoyette }
    373       1.1  pgoyette 
    374       1.3  ginsbach ATF_TC(seconds);
    375       1.3  ginsbach 
    376       1.3  ginsbach ATF_TC_HEAD(seconds, tc)
    377       1.3  ginsbach {
    378       1.3  ginsbach 
    379       1.3  ginsbach 	atf_tc_set_md_var(tc, "descr",
    380       1.3  ginsbach 			  "Checks strptime(3) seconds conversions [S]");
    381       1.3  ginsbach }
    382       1.3  ginsbach 
    383       1.3  ginsbach ATF_TC_BODY(seconds, tc)
    384       1.3  ginsbach {
    385       1.3  ginsbach 
    386       1.3  ginsbach 	h_pass("0", "%S", 1, 0, -1, -1, -1, -1, -1, -1, -1);
    387       1.3  ginsbach 	h_pass("59", "%S", 2, 59, -1, -1, -1, -1, -1, -1, -1);
    388       1.3  ginsbach 	h_pass("60", "%S", 2, 60, -1, -1, -1, -1, -1, -1, -1);
    389       1.3  ginsbach 	h_pass("61", "%S", 2, 61, -1, -1, -1, -1, -1, -1, -1);
    390       1.3  ginsbach 	h_fail("62", "%S");
    391       1.3  ginsbach }
    392       1.3  ginsbach 
    393       1.4  ginsbach ATF_TC(year);
    394       1.4  ginsbach 
    395       1.4  ginsbach ATF_TC_HEAD(year, tc)
    396       1.4  ginsbach {
    397       1.4  ginsbach 
    398       1.4  ginsbach 	atf_tc_set_md_var(tc, "descr",
    399       1.4  ginsbach 			  "Checks strptime(3) century/year conversions [CyY]");
    400       1.4  ginsbach }
    401       1.4  ginsbach 
    402       1.4  ginsbach ATF_TC_BODY(year, tc)
    403       1.4  ginsbach {
    404       1.4  ginsbach 
    405       1.4  ginsbach 	h_pass("x20y", "x%Cy", 4, -1, -1, -1, -1, -1, 100, -1, -1);
    406       1.4  ginsbach 	h_pass("x84y", "x%yy", 4, -1, -1, -1, -1, -1, 84, -1, -1);
    407       1.4  ginsbach 	h_pass("x2084y", "x%C%yy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
    408       1.4  ginsbach 	h_pass("x8420y", "x%y%Cy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
    409       1.4  ginsbach 	h_pass("%20845", "%%%C%y5", 6, -1, -1, -1, -1, -1, 184, -1, -1);
    410       1.4  ginsbach 	h_fail("%", "%E%");
    411       1.4  ginsbach 
    412       1.4  ginsbach 	h_pass("1980", "%Y", 4, -1, -1, -1, -1, -1, 80, -1, -1);
    413       1.4  ginsbach 	h_pass("1980", "%EY", 4, -1, -1, -1, -1, -1, 80, -1, -1);
    414       1.4  ginsbach }
    415       1.4  ginsbach 
    416       1.7  christos ATF_TC(zone);
    417       1.7  christos 
    418       1.7  christos ATF_TC_HEAD(zone, tc)
    419       1.7  christos {
    420       1.7  christos 
    421       1.7  christos 	atf_tc_set_md_var(tc, "descr",
    422       1.7  christos 			  "Checks strptime(3) timezone conversion [z]");
    423       1.7  christos }
    424       1.7  christos 
    425       1.7  christos 
    426      1.10  christos ATF_TC_BODY(zone, tc)
    427      1.10  christos {
    428      1.12  christos 	ztest("%z");
    429      1.10  christos }
    430       1.7  christos 
    431      1.10  christos ATF_TC(Zone);
    432       1.7  christos 
    433      1.10  christos ATF_TC_HEAD(Zone, tc)
    434      1.10  christos {
    435       1.7  christos 
    436      1.10  christos 	atf_tc_set_md_var(tc, "descr",
    437      1.10  christos 			  "Checks strptime(3) timezone conversion [Z]");
    438      1.10  christos }
    439       1.7  christos 
    440       1.7  christos 
    441      1.10  christos ATF_TC_BODY(Zone, tc)
    442       1.7  christos {
    443      1.14       kre 	ztest("%Z");
    444       1.7  christos }
    445       1.7  christos 
    446  1.15.4.1    martin ATF_TC(posixtime_overflow);
    447  1.15.4.1    martin 
    448  1.15.4.1    martin ATF_TC_HEAD(posixtime_overflow, tc)
    449  1.15.4.1    martin {
    450  1.15.4.1    martin 
    451  1.15.4.1    martin 	atf_tc_set_md_var(tc, "descr",
    452  1.15.4.1    martin 	    "Checks strptime(3) safely rejects POSIX time overfow");
    453  1.15.4.1    martin }
    454  1.15.4.1    martin 
    455  1.15.4.1    martin ATF_TC_BODY(posixtime_overflow, tc)
    456  1.15.4.1    martin {
    457  1.15.4.1    martin 	static const uint64_t P[] = { /* cases that should pass round-trip */
    458  1.15.4.1    martin 		[0] = 0,
    459  1.15.4.1    martin 		[1] = 1,
    460  1.15.4.1    martin 		[2] = 2,
    461  1.15.4.1    martin 		[3] = 0x7ffffffe,
    462  1.15.4.1    martin 		[4] = 0x7fffffff,
    463  1.15.4.1    martin 		[5] = 0x80000000,
    464  1.15.4.1    martin 		[6] = 0x80000001,
    465  1.15.4.1    martin 		[7] = 0xfffffffe,
    466  1.15.4.1    martin 		[8] = 0xffffffff,
    467  1.15.4.1    martin 		[9] = 0x100000000,
    468  1.15.4.1    martin 		[10] = 0x100000001,
    469  1.15.4.1    martin 		[11] = 67767976233532799, /* 2147483647-12-31T23:59:59 */
    470  1.15.4.1    martin 		/*
    471  1.15.4.1    martin 		 * Beyond this point, the year (.tm_year + 1900)
    472  1.15.4.1    martin 		 * overflows the signed 32-bit range, so we won't be
    473  1.15.4.1    martin 		 * able to test round-trips:
    474  1.15.4.1    martin 		 */
    475  1.15.4.1    martin #if 0	/* localtime can't handle these years in 9.x */
    476  1.15.4.1    martin 		[12] = 67767976233532800,
    477  1.15.4.1    martin 		[13] = 67767976233532801,
    478  1.15.4.1    martin 		[14] = 67768036191676799,
    479  1.15.4.1    martin #endif
    480  1.15.4.1    martin 		/*
    481  1.15.4.1    martin 		 * Beyond this point, .tm_year itself overflows the
    482  1.15.4.1    martin 		 * signed 32-bit range, so strptime won't work at all;
    483  1.15.4.1    martin 		 * the output can't be represented in struct tm.
    484  1.15.4.1    martin 		 */
    485  1.15.4.1    martin #if 0
    486  1.15.4.1    martin 		[15] = 67768036191676800,
    487  1.15.4.1    martin 		[16] = 67768036191676801,
    488  1.15.4.1    martin 		[17] = 0x7ffffffffffffffe,
    489  1.15.4.1    martin 		[18] = 0x7fffffffffffffff,
    490  1.15.4.1    martin #endif
    491  1.15.4.1    martin 	};
    492  1.15.4.1    martin 	static const uint64_t F[] = { /* cases strptime should reject */
    493  1.15.4.1    martin 		[0] = 67768036191676800,
    494  1.15.4.1    martin 		[1] = 67768036191676801,
    495  1.15.4.1    martin 		[2] = 0x7ffffffffffffffe,
    496  1.15.4.1    martin 		[3] = 0x7fffffffffffffff,
    497  1.15.4.1    martin 		[4] = 0x8000000000000000,
    498  1.15.4.1    martin 		[5] = 0x8000000000000001,
    499  1.15.4.1    martin 		[6] = 0xfffffffffffffffe,
    500  1.15.4.1    martin 		[7] = 0xffffffffffffffff,
    501  1.15.4.1    martin 	};
    502  1.15.4.1    martin 	size_t i;
    503  1.15.4.1    martin 
    504  1.15.4.1    martin 	/*
    505  1.15.4.1    martin 	 * Verify time_t fits in uint64_t, with space to spare since
    506  1.15.4.1    martin 	 * it's signed.
    507  1.15.4.1    martin 	 */
    508  1.15.4.1    martin 	__CTASSERT(__type_max(time_t) < __type_max(uint64_t));
    509  1.15.4.1    martin 
    510  1.15.4.1    martin 	/*
    511  1.15.4.1    martin 	 * Make sure we work in UTC so this test doesn't depend on
    512  1.15.4.1    martin 	 * which time zone your machine is configured for.
    513  1.15.4.1    martin 	 */
    514  1.15.4.1    martin 	setenv("TZ", "UTC", 1);
    515  1.15.4.1    martin 
    516  1.15.4.1    martin 	/*
    517  1.15.4.1    martin 	 * Check the should-pass cases.
    518  1.15.4.1    martin 	 */
    519  1.15.4.1    martin 	for (i = 0; i < __arraycount(P); i++) {
    520  1.15.4.1    martin 		char buf[sizeof("18446744073709551616")];
    521  1.15.4.1    martin 		int n;
    522  1.15.4.1    martin 		struct tm tm;
    523  1.15.4.1    martin 		time_t t;
    524  1.15.4.1    martin 		int error;
    525  1.15.4.1    martin 
    526  1.15.4.1    martin 		/*
    527  1.15.4.1    martin 		 * Format the integer in decimal.
    528  1.15.4.1    martin 		 */
    529  1.15.4.1    martin 		n = snprintf(buf, sizeof(buf), "%"PRIu64, P[i]);
    530  1.15.4.1    martin 		ATF_CHECK_MSG(n >= 0 && (unsigned)n < sizeof(buf),
    531  1.15.4.1    martin 		    "P[%zu]: 64-bit requires %d digits", i, n);
    532  1.15.4.1    martin 
    533  1.15.4.1    martin 		/*
    534  1.15.4.1    martin 		 * Parse the time into components.
    535  1.15.4.1    martin 		 */
    536  1.15.4.1    martin 		fprintf(stderr, "# P[%zu]: %"PRId64"\n", i, P[i]);
    537  1.15.4.1    martin 		if (strptime(buf, "%s", &tm) == NULL) {
    538  1.15.4.1    martin 			atf_tc_fail_nonfatal("P[%zu]: strptime failed", i);
    539  1.15.4.1    martin 			continue;
    540  1.15.4.1    martin 		}
    541  1.15.4.1    martin 		fprintf(stderr, "tm_sec=%d\n", tm.tm_sec);
    542  1.15.4.1    martin 		fprintf(stderr, "tm_min=%d\n", tm.tm_min);
    543  1.15.4.1    martin 		fprintf(stderr, "tm_hour=%d\n", tm.tm_hour);
    544  1.15.4.1    martin 		fprintf(stderr, "tm_mday=%d\n", tm.tm_mday);
    545  1.15.4.1    martin 		fprintf(stderr, "tm_mon=%d\n", tm.tm_mon);
    546  1.15.4.1    martin 		fprintf(stderr, "tm_year=%d\n", tm.tm_year);
    547  1.15.4.1    martin 		fprintf(stderr, "tm_wday=%d\n", tm.tm_wday);
    548  1.15.4.1    martin 		fprintf(stderr, "tm_yday=%d\n", tm.tm_yday);
    549  1.15.4.1    martin 		fprintf(stderr, "tm_isdst=%d\n", tm.tm_isdst);
    550  1.15.4.1    martin 		fprintf(stderr, "tm_gmtoff=%ld\n", tm.tm_gmtoff);
    551  1.15.4.1    martin 		fprintf(stderr, "tm_zone=%s\n", tm.tm_zone);
    552  1.15.4.1    martin 
    553  1.15.4.1    martin 		/*
    554  1.15.4.1    martin 		 * Convert back to POSIX seconds since epoch -- unless
    555  1.15.4.1    martin 		 * the year number overflows signed 32-bit, in which
    556  1.15.4.1    martin 		 * case stop here because we can't test further.
    557  1.15.4.1    martin 		 */
    558  1.15.4.1    martin 		if (tm.tm_year > 0x7fffffff - 1900)
    559  1.15.4.1    martin 			continue;
    560  1.15.4.1    martin 		t = mktime(&tm);
    561  1.15.4.1    martin 		error = errno;
    562  1.15.4.1    martin 		ATF_CHECK_MSG(t != -1, "P[%zu]: mktime failed: %d, %s",
    563  1.15.4.1    martin 		    i, error, strerror(error));
    564  1.15.4.1    martin 
    565  1.15.4.1    martin 		/*
    566  1.15.4.1    martin 		 * Verify the round-trip.
    567  1.15.4.1    martin 		 */
    568  1.15.4.1    martin 		ATF_CHECK_EQ_MSG(P[i], (uint64_t)t,
    569  1.15.4.1    martin 		    "P[%zu]: %"PRId64" -> %"PRId64, i, P[i], (int64_t)t);
    570  1.15.4.1    martin 	}
    571  1.15.4.1    martin 
    572  1.15.4.1    martin 	/*
    573  1.15.4.1    martin 	 * Check the should-fail cases.
    574  1.15.4.1    martin 	 */
    575  1.15.4.1    martin 	for (i = 0; i < __arraycount(F); i++) {
    576  1.15.4.1    martin 		char buf[sizeof("18446744073709551616")];
    577  1.15.4.1    martin 		int n;
    578  1.15.4.1    martin 
    579  1.15.4.1    martin 		/*
    580  1.15.4.1    martin 		 * Format the integer in decimal.
    581  1.15.4.1    martin 		 */
    582  1.15.4.1    martin 		n = snprintf(buf, sizeof(buf), "%"PRIu64, F[i]);
    583  1.15.4.1    martin 		ATF_CHECK_MSG(n >= 0 && (unsigned)n < sizeof(buf),
    584  1.15.4.1    martin 		    "F[%zu]: 64-bit requires %d digits", i, n);
    585  1.15.4.1    martin 
    586  1.15.4.1    martin 		/*
    587  1.15.4.1    martin 		 * Verify strptime rejects this.
    588  1.15.4.1    martin 		 */
    589  1.15.4.1    martin 		h_fail(buf, "%s");
    590  1.15.4.1    martin 	}
    591  1.15.4.1    martin }
    592  1.15.4.1    martin 
    593       1.1  pgoyette ATF_TP_ADD_TCS(tp)
    594       1.1  pgoyette {
    595       1.1  pgoyette 
    596       1.1  pgoyette 	ATF_TP_ADD_TC(tp, common);
    597       1.1  pgoyette 	ATF_TP_ADD_TC(tp, day);
    598       1.5  ginsbach 	ATF_TP_ADD_TC(tp, hour);
    599       1.1  pgoyette 	ATF_TP_ADD_TC(tp, month);
    600       1.3  ginsbach 	ATF_TP_ADD_TC(tp, seconds);
    601       1.4  ginsbach 	ATF_TP_ADD_TC(tp, year);
    602       1.7  christos 	ATF_TP_ADD_TC(tp, zone);
    603      1.10  christos 	ATF_TP_ADD_TC(tp, Zone);
    604  1.15.4.1    martin 	ATF_TP_ADD_TC(tp, posixtime_overflow);
    605       1.1  pgoyette 
    606       1.1  pgoyette 	return atf_no_error();
    607       1.1  pgoyette }
    608