Home | History | Annotate | Line # | Download | only in time
t_strptime.c revision 1.16
      1  1.16  riastrad /* $NetBSD: t_strptime.c,v 1.16 2024/03/16 00:06:37 riastradh 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.16  riastrad __RCSID("$NetBSD: t_strptime.c,v 1.16 2024/03/16 00:06:37 riastradh Exp $");
     36   1.1  pgoyette 
     37  1.16  riastrad #include <errno.h>
     38  1.16  riastrad #include <inttypes.h>
     39  1.16  riastrad #include <stdio.h>
     40  1.16  riastrad #include <stdlib.h>
     41   1.1  pgoyette #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.16  riastrad ATF_TC(posixtime_overflow);
    447  1.16  riastrad 
    448  1.16  riastrad ATF_TC_HEAD(posixtime_overflow, tc)
    449  1.16  riastrad {
    450  1.16  riastrad 
    451  1.16  riastrad 	atf_tc_set_md_var(tc, "descr",
    452  1.16  riastrad 	    "Checks strptime(3) safely rejects POSIX time overfow");
    453  1.16  riastrad }
    454  1.16  riastrad 
    455  1.16  riastrad ATF_TC_BODY(posixtime_overflow, tc)
    456  1.16  riastrad {
    457  1.16  riastrad 	static const uint64_t P[] = { /* cases that should pass round-trip */
    458  1.16  riastrad 		[0] = 0,
    459  1.16  riastrad 		[1] = 1,
    460  1.16  riastrad 		[2] = 2,
    461  1.16  riastrad 		[3] = 0x7ffffffe,
    462  1.16  riastrad 		[4] = 0x7fffffff,
    463  1.16  riastrad 		[5] = 0x80000000,
    464  1.16  riastrad 		[6] = 0x80000001,
    465  1.16  riastrad 		[7] = 0xfffffffe,
    466  1.16  riastrad 		[8] = 0xffffffff,
    467  1.16  riastrad 		[9] = 0x100000000,
    468  1.16  riastrad 		[10] = 0x100000001,
    469  1.16  riastrad 		[11] = 67767976233532799, /* 2147483647-12-31T23:59:59 */
    470  1.16  riastrad 		/*
    471  1.16  riastrad 		 * Beyond this point, the year (.tm_year + 1900)
    472  1.16  riastrad 		 * overflows the signed 32-bit range, so we won't be
    473  1.16  riastrad 		 * able to test round-trips:
    474  1.16  riastrad 		 */
    475  1.16  riastrad 		[12] = 67767976233532800,
    476  1.16  riastrad 		[13] = 67767976233532801,
    477  1.16  riastrad 		[14] = 67768036191676799,
    478  1.16  riastrad 		/*
    479  1.16  riastrad 		 * Beyond this point, .tm_year itself overflows the
    480  1.16  riastrad 		 * signed 32-bit range, so strptime won't work at all;
    481  1.16  riastrad 		 * the output can't be represented in struct tm.
    482  1.16  riastrad 		 */
    483  1.16  riastrad #if 0
    484  1.16  riastrad 		[15] = 67768036191676800,
    485  1.16  riastrad 		[16] = 67768036191676801,
    486  1.16  riastrad 		[17] = 0x7ffffffffffffffe,
    487  1.16  riastrad 		[18] = 0x7fffffffffffffff,
    488  1.16  riastrad #endif
    489  1.16  riastrad 	};
    490  1.16  riastrad 	static const uint64_t F[] = { /* cases strptime should reject */
    491  1.16  riastrad 		[0] = 67768036191676800,
    492  1.16  riastrad 		[1] = 67768036191676801,
    493  1.16  riastrad 		[2] = 0x7ffffffffffffffe,
    494  1.16  riastrad 		[3] = 0x7fffffffffffffff,
    495  1.16  riastrad 		[4] = 0x8000000000000000,
    496  1.16  riastrad 		[5] = 0x8000000000000001,
    497  1.16  riastrad 		[6] = 0xfffffffffffffffe,
    498  1.16  riastrad 		[7] = 0xffffffffffffffff,
    499  1.16  riastrad 	};
    500  1.16  riastrad 	size_t i;
    501  1.16  riastrad 
    502  1.16  riastrad 	/*
    503  1.16  riastrad 	 * Verify time_t fits in uint64_t, with space to spare since
    504  1.16  riastrad 	 * it's signed.
    505  1.16  riastrad 	 */
    506  1.16  riastrad 	__CTASSERT(__type_max(time_t) < __type_max(uint64_t));
    507  1.16  riastrad 
    508  1.16  riastrad 	/*
    509  1.16  riastrad 	 * Make sure we work in UTC so this test doesn't depend on
    510  1.16  riastrad 	 * which time zone your machine is configured for.
    511  1.16  riastrad 	 */
    512  1.16  riastrad 	setenv("TZ", "UTC", 1);
    513  1.16  riastrad 
    514  1.16  riastrad 	/*
    515  1.16  riastrad 	 * Check the should-pass cases.
    516  1.16  riastrad 	 */
    517  1.16  riastrad 	for (i = 0; i < __arraycount(P); i++) {
    518  1.16  riastrad 		char buf[sizeof("18446744073709551616")];
    519  1.16  riastrad 		int n;
    520  1.16  riastrad 		struct tm tm;
    521  1.16  riastrad 		time_t t;
    522  1.16  riastrad 		int error;
    523  1.16  riastrad 
    524  1.16  riastrad 		/*
    525  1.16  riastrad 		 * Format the integer in decimal.
    526  1.16  riastrad 		 */
    527  1.16  riastrad 		n = snprintf(buf, sizeof(buf), "%"PRIu64, P[i]);
    528  1.16  riastrad 		ATF_CHECK_MSG(n >= 0 && (unsigned)n < sizeof(buf),
    529  1.16  riastrad 		    "P[%zu]: 64-bit requires %d digits", i, n);
    530  1.16  riastrad 
    531  1.16  riastrad 		/*
    532  1.16  riastrad 		 * Parse the time into components.
    533  1.16  riastrad 		 */
    534  1.16  riastrad 		fprintf(stderr, "# P[%zu]: %"PRId64"\n", i, P[i]);
    535  1.16  riastrad 		if (strptime(buf, "%s", &tm) == NULL) {
    536  1.16  riastrad 			atf_tc_fail_nonfatal("P[%zu]: strptime failed", i);
    537  1.16  riastrad 			continue;
    538  1.16  riastrad 		}
    539  1.16  riastrad 		fprintf(stderr, "tm_sec=%d\n", tm.tm_sec);
    540  1.16  riastrad 		fprintf(stderr, "tm_min=%d\n", tm.tm_min);
    541  1.16  riastrad 		fprintf(stderr, "tm_hour=%d\n", tm.tm_hour);
    542  1.16  riastrad 		fprintf(stderr, "tm_mday=%d\n", tm.tm_mday);
    543  1.16  riastrad 		fprintf(stderr, "tm_mon=%d\n", tm.tm_mon);
    544  1.16  riastrad 		fprintf(stderr, "tm_year=%d\n", tm.tm_year);
    545  1.16  riastrad 		fprintf(stderr, "tm_wday=%d\n", tm.tm_wday);
    546  1.16  riastrad 		fprintf(stderr, "tm_yday=%d\n", tm.tm_yday);
    547  1.16  riastrad 		fprintf(stderr, "tm_isdst=%d\n", tm.tm_isdst);
    548  1.16  riastrad 		fprintf(stderr, "tm_gmtoff=%ld\n", tm.tm_gmtoff);
    549  1.16  riastrad 		fprintf(stderr, "tm_zone=%s\n", tm.tm_zone);
    550  1.16  riastrad 
    551  1.16  riastrad 		/*
    552  1.16  riastrad 		 * Convert back to POSIX seconds since epoch -- unless
    553  1.16  riastrad 		 * the year number overflows signed 32-bit, in which
    554  1.16  riastrad 		 * case stop here because we can't test further.
    555  1.16  riastrad 		 */
    556  1.16  riastrad 		if (tm.tm_year > 0x7fffffff - 1900)
    557  1.16  riastrad 			continue;
    558  1.16  riastrad 		t = mktime(&tm);
    559  1.16  riastrad 		error = errno;
    560  1.16  riastrad 		ATF_CHECK_MSG(t != -1, "P[%zu]: mktime failed: %d, %s",
    561  1.16  riastrad 		    i, error, strerror(error));
    562  1.16  riastrad 
    563  1.16  riastrad 		/*
    564  1.16  riastrad 		 * Verify the round-trip.
    565  1.16  riastrad 		 */
    566  1.16  riastrad 		ATF_CHECK_EQ_MSG(P[i], (uint64_t)t,
    567  1.16  riastrad 		    "P[%zu]: %"PRId64" -> %"PRId64, i, P[i], (int64_t)t);
    568  1.16  riastrad 	}
    569  1.16  riastrad 
    570  1.16  riastrad 	/*
    571  1.16  riastrad 	 * Check the should-fail cases.
    572  1.16  riastrad 	 */
    573  1.16  riastrad 	for (i = 0; i < __arraycount(F); i++) {
    574  1.16  riastrad 		char buf[sizeof("18446744073709551616")];
    575  1.16  riastrad 		int n;
    576  1.16  riastrad 
    577  1.16  riastrad 		/*
    578  1.16  riastrad 		 * Format the integer in decimal.
    579  1.16  riastrad 		 */
    580  1.16  riastrad 		n = snprintf(buf, sizeof(buf), "%"PRIu64, F[i]);
    581  1.16  riastrad 		ATF_CHECK_MSG(n >= 0 && (unsigned)n < sizeof(buf),
    582  1.16  riastrad 		    "F[%zu]: 64-bit requires %d digits", i, n);
    583  1.16  riastrad 
    584  1.16  riastrad 		/*
    585  1.16  riastrad 		 * Verify strptime rejects this.
    586  1.16  riastrad 		 */
    587  1.16  riastrad 		h_fail(buf, "%s");
    588  1.16  riastrad 	}
    589  1.16  riastrad }
    590  1.16  riastrad 
    591   1.1  pgoyette ATF_TP_ADD_TCS(tp)
    592   1.1  pgoyette {
    593   1.1  pgoyette 
    594   1.1  pgoyette 	ATF_TP_ADD_TC(tp, common);
    595   1.1  pgoyette 	ATF_TP_ADD_TC(tp, day);
    596   1.5  ginsbach 	ATF_TP_ADD_TC(tp, hour);
    597   1.1  pgoyette 	ATF_TP_ADD_TC(tp, month);
    598   1.3  ginsbach 	ATF_TP_ADD_TC(tp, seconds);
    599   1.4  ginsbach 	ATF_TP_ADD_TC(tp, year);
    600   1.7  christos 	ATF_TP_ADD_TC(tp, zone);
    601  1.10  christos 	ATF_TP_ADD_TC(tp, Zone);
    602  1.16  riastrad 	ATF_TP_ADD_TC(tp, posixtime_overflow);
    603   1.1  pgoyette 
    604   1.1  pgoyette 	return atf_no_error();
    605   1.1  pgoyette }
    606