Home | History | Annotate | Line # | Download | only in libutil
t_parsedate.c revision 1.7.8.4
      1  1.7.8.4       snj /* $NetBSD: t_parsedate.c,v 1.7.8.4 2014/10/11 16:47:00 snj Exp $ */
      2      1.1     njoly /*-
      3      1.1     njoly  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      4      1.1     njoly  * All rights reserved.
      5      1.1     njoly  *
      6      1.1     njoly  * Redistribution and use in source and binary forms, with or without
      7      1.1     njoly  * modification, are permitted provided that the following conditions
      8      1.1     njoly  * are met:
      9      1.1     njoly  *
     10      1.1     njoly  * 1. Redistributions of source code must retain the above copyright
     11      1.1     njoly  *    notice, this list of conditions and the following disclaimer.
     12      1.1     njoly  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1     njoly  *    notice, this list of conditions and the following disclaimer in
     14      1.1     njoly  *    the documentation and/or other materials provided with the
     15      1.1     njoly  *    distribution.
     16      1.1     njoly  *
     17      1.1     njoly  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18      1.1     njoly  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19      1.1     njoly  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     20      1.1     njoly  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     21      1.1     njoly  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     22      1.1     njoly  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23      1.1     njoly  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24      1.1     njoly  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25      1.1     njoly  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26      1.1     njoly  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     27      1.1     njoly  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28      1.1     njoly  * SUCH DAMAGE.
     29      1.1     njoly  */
     30      1.1     njoly 
     31      1.1     njoly #include <sys/cdefs.h>
     32  1.7.8.4       snj __RCSID("$NetBSD: t_parsedate.c,v 1.7.8.4 2014/10/11 16:47:00 snj Exp $");
     33      1.1     njoly 
     34      1.1     njoly #include <atf-c.h>
     35      1.6       apb #include <errno.h>
     36      1.6       apb #include <stdlib.h>
     37      1.6       apb #include <time.h>
     38      1.1     njoly #include <util.h>
     39      1.1     njoly 
     40  1.7.8.1       snj /*
     41  1.7.8.1       snj  * ANY is used as a placeholder for values that do not need to be
     42  1.7.8.1       snj  * checked.  The actual value is arbitrary.  We don't use -1
     43  1.7.8.1       snj  * because some tests might want to use -1 as a literal value.
     44  1.7.8.1       snj  */
     45  1.7.8.1       snj #define ANY -30215
     46  1.7.8.1       snj 
     47  1.7.8.1       snj /* parsecheck --
     48  1.7.8.1       snj  * call parsedate(), then call time_to_tm() on the result,
     49  1.7.8.1       snj  * and check that year/month/day/hour/minute/second are as expected.
     50  1.7.8.1       snj  *
     51  1.7.8.1       snj  * time_to_tm should usually be localtime_r or gmtime_r.
     52  1.7.8.1       snj  *
     53  1.7.8.1       snj  * Don't check values specified as ANY.
     54  1.7.8.1       snj  */
     55  1.7.8.1       snj static void
     56  1.7.8.1       snj parsecheck(const char *datestr, const time_t *reftime, const int *zoff,
     57  1.7.8.1       snj 	struct tm * time_to_tm(const time_t *, struct tm *),
     58  1.7.8.1       snj 	int year, int month, int day, int hour, int minute, int second)
     59  1.7.8.1       snj {
     60  1.7.8.1       snj 	time_t t;
     61  1.7.8.1       snj 	struct tm tm;
     62  1.7.8.1       snj 
     63  1.7.8.1       snj 	ATF_CHECK_MSG((t = parsedate(datestr, reftime, zoff)) != -1,
     64  1.7.8.1       snj 	    "parsedate(\"%s\",,) returned -1\n", datestr);
     65  1.7.8.1       snj 	ATF_CHECK(time_to_tm(&t, &tm) != NULL);
     66  1.7.8.1       snj 	if (year != ANY)
     67  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_year + 1900 == year,
     68  1.7.8.1       snj 		    "parsedate(\"%s\",,) expected year %d got %d (+1900)\n",
     69  1.7.8.1       snj 		    datestr, year, (int)tm.tm_year);
     70  1.7.8.1       snj 	if (month != ANY)
     71  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_mon + 1 == month,
     72  1.7.8.1       snj 		    "parsedate(\"%s\",,) expected month %d got %d (+1)\n",
     73  1.7.8.1       snj 		    datestr, month, (int)tm.tm_mon);
     74  1.7.8.1       snj 	if (day != ANY)
     75  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_mday == day,
     76  1.7.8.1       snj 		    "parsedate(\"%s\",,) expected day %d got %d\n",
     77  1.7.8.1       snj 		    datestr, day, (int)tm.tm_mday);
     78  1.7.8.1       snj 	if (hour != ANY)
     79  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_hour == hour,
     80  1.7.8.1       snj 		    "parsedate(\"%s\",,) expected hour %d got %d\n",
     81  1.7.8.1       snj 		    datestr, hour, (int)tm.tm_hour);
     82  1.7.8.1       snj 	if (minute != ANY)
     83  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_min == minute,
     84  1.7.8.1       snj 		    "parsedate(\"%s\",,) expected minute %d got %d\n",
     85  1.7.8.1       snj 		    datestr, minute, (int)tm.tm_min);
     86  1.7.8.1       snj 	if (second != ANY)
     87  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_sec == second,
     88  1.7.8.1       snj 		    "parsedate(\"%s\",,) expected second %d got %d\n",
     89  1.7.8.1       snj 		    datestr, second, (int)tm.tm_sec);
     90  1.7.8.1       snj }
     91  1.7.8.1       snj 
     92      1.1     njoly ATF_TC(dates);
     93      1.1     njoly 
     94      1.1     njoly ATF_TC_HEAD(dates, tc)
     95      1.1     njoly {
     96      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test unambiguous dates"
     97      1.5    jruoho 	    " (PR lib/44255)");
     98      1.1     njoly }
     99      1.1     njoly 
    100      1.1     njoly ATF_TC_BODY(dates, tc)
    101      1.1     njoly {
    102      1.1     njoly 
    103  1.7.8.3       snj 	parsecheck("9/10/69", NULL, NULL, localtime_r,
    104  1.7.8.2       snj 		2069, 9, 10, 0, 0, 0); /* year < 70: add 2000 */
    105  1.7.8.3       snj 	parsecheck("9/10/70", NULL, NULL, localtime_r,
    106  1.7.8.2       snj 		1970, 9, 10, 0, 0, 0); /* 70 <= year < 100: add 1900 */
    107  1.7.8.3       snj 	parsecheck("69-09-10", NULL, NULL, localtime_r,
    108  1.7.8.3       snj 		69, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
    109  1.7.8.3       snj 	parsecheck("70-09-10", NULL, NULL, localtime_r,
    110  1.7.8.3       snj 		70, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
    111  1.7.8.1       snj 	parsecheck("2006-11-17", NULL, NULL, localtime_r,
    112  1.7.8.1       snj 		2006, 11, 17, 0, 0, 0);
    113  1.7.8.1       snj 	parsecheck("10/1/2000", NULL, NULL, localtime_r,
    114  1.7.8.2       snj 		2000, 10, 1, 0, 0, 0); /* month/day/year */
    115  1.7.8.1       snj 	parsecheck("20 Jun 1994", NULL, NULL, localtime_r,
    116  1.7.8.1       snj 		1994, 6, 20, 0, 0, 0);
    117  1.7.8.1       snj 	parsecheck("23jun2001", NULL, NULL, localtime_r,
    118  1.7.8.1       snj 		2001, 6, 23, 0, 0, 0);
    119  1.7.8.1       snj 	parsecheck("1-sep-06", NULL, NULL, localtime_r,
    120  1.7.8.1       snj 		2006, 9, 1, 0, 0, 0);
    121  1.7.8.1       snj 	parsecheck("1/11", NULL, NULL, localtime_r,
    122  1.7.8.2       snj 		ANY, 1, 11, 0, 0, 0); /* month/day */
    123  1.7.8.1       snj 	parsecheck("1500-01-02", NULL, NULL, localtime_r,
    124  1.7.8.1       snj 		1500, 1, 2, 0, 0, 0);
    125  1.7.8.1       snj 	parsecheck("9999-12-21", NULL, NULL, localtime_r,
    126  1.7.8.1       snj 		9999, 12, 21, 0, 0, 0);
    127      1.1     njoly }
    128      1.1     njoly 
    129      1.1     njoly ATF_TC(times);
    130      1.1     njoly 
    131      1.1     njoly ATF_TC_HEAD(times, tc)
    132      1.1     njoly {
    133      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test times"
    134      1.5    jruoho 	    " (PR lib/44255)");
    135      1.1     njoly }
    136      1.1     njoly 
    137      1.1     njoly ATF_TC_BODY(times, tc)
    138      1.1     njoly {
    139      1.1     njoly 
    140  1.7.8.1       snj 	parsecheck("10:01", NULL, NULL, localtime_r,
    141  1.7.8.1       snj 		ANY, ANY, ANY, 10, 1, 0);
    142  1.7.8.1       snj 	parsecheck("10:12pm", NULL, NULL, localtime_r,
    143  1.7.8.1       snj 		ANY, ANY, ANY, 22, 12, 0);
    144  1.7.8.1       snj 	parsecheck("12:11:01.000012", NULL, NULL, localtime_r,
    145  1.7.8.1       snj 		ANY, ANY, ANY, 12, 11, 1);
    146  1.7.8.1       snj 	parsecheck("12:21-0500", NULL, NULL, gmtime_r,
    147  1.7.8.1       snj 		ANY, ANY, ANY, 12+5, 21, 0);
    148      1.1     njoly }
    149      1.1     njoly 
    150  1.7.8.4       snj ATF_TC(dsttimes);
    151  1.7.8.4       snj 
    152  1.7.8.4       snj ATF_TC_HEAD(dsttimes, tc)
    153  1.7.8.4       snj {
    154  1.7.8.4       snj 	atf_tc_set_md_var(tc, "descr", "Test DST transition times"
    155  1.7.8.4       snj 	    " (PR lib/47916)");
    156  1.7.8.4       snj }
    157  1.7.8.4       snj 
    158  1.7.8.4       snj ATF_TC_BODY(dsttimes, tc)
    159  1.7.8.4       snj {
    160  1.7.8.4       snj 	struct tm tm;
    161  1.7.8.4       snj 	time_t t;
    162  1.7.8.4       snj 	int tzoff;
    163  1.7.8.4       snj 
    164  1.7.8.4       snj 	putenv(__UNCONST("TZ=EST"));
    165  1.7.8.4       snj 	tzset();
    166  1.7.8.4       snj 	parsecheck("12:0", NULL, NULL, localtime_r,
    167  1.7.8.4       snj 		ANY, ANY, ANY, 12, 0, 0);
    168  1.7.8.4       snj 
    169  1.7.8.4       snj 	putenv(__UNCONST("TZ=Japan"));
    170  1.7.8.4       snj 	tzset();
    171  1.7.8.4       snj 	parsecheck("12:0", NULL, NULL, localtime_r,
    172  1.7.8.4       snj 		ANY, ANY, ANY, 12, 0, 0);
    173  1.7.8.4       snj 
    174  1.7.8.4       snj 	/*
    175  1.7.8.4       snj 	 * When the effective local time is Tue Jul  9 13:21:53 BST 2013,
    176  1.7.8.4       snj 	 * check mktime("14:00")
    177  1.7.8.4       snj 	 */
    178  1.7.8.4       snj 	putenv(__UNCONST("TZ=Europe/London"));
    179  1.7.8.4       snj 	tzset();
    180  1.7.8.4       snj 	tm = (struct tm){
    181  1.7.8.4       snj 		.tm_year = 2013-1900, .tm_mon = 7-1, .tm_mday = 9,
    182  1.7.8.4       snj 		.tm_hour = 13, .tm_min = 21, .tm_sec = 53,
    183  1.7.8.4       snj 		.tm_isdst = 0 };
    184  1.7.8.4       snj 	t = mktime(&tm);
    185  1.7.8.4       snj 	ATF_CHECK(t != (time_t)-1);
    186  1.7.8.4       snj 	parsecheck("14:00", &t, NULL, localtime_r,
    187  1.7.8.4       snj 		2013, 7, 9, 14, 0, 0);
    188  1.7.8.4       snj 	tzoff = 0;
    189  1.7.8.4       snj 	parsecheck("14:00", &t, &tzoff, localtime_r,
    190  1.7.8.4       snj 		2013, 7, 9, 14, 0, 0);
    191  1.7.8.4       snj }
    192  1.7.8.4       snj 
    193      1.1     njoly ATF_TC(relative);
    194      1.1     njoly 
    195      1.1     njoly ATF_TC_HEAD(relative, tc)
    196      1.1     njoly {
    197      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test relative items"
    198      1.5    jruoho 	    " (PR lib/44255)");
    199      1.1     njoly }
    200      1.1     njoly 
    201      1.1     njoly ATF_TC_BODY(relative, tc)
    202      1.1     njoly {
    203      1.1     njoly 
    204      1.1     njoly 	ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1);
    205      1.1     njoly 	ATF_CHECK(parsedate("last friday", NULL, NULL) != -1);
    206      1.1     njoly 	ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1);
    207      1.1     njoly 	ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1);
    208      1.1     njoly 	ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1);
    209      1.1     njoly 	ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1);
    210      1.1     njoly }
    211      1.1     njoly 
    212      1.6       apb ATF_TC(atsecs);
    213      1.6       apb 
    214      1.6       apb ATF_TC_HEAD(atsecs, tc)
    215      1.6       apb {
    216      1.6       apb 	atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch");
    217      1.6       apb }
    218      1.6       apb 
    219      1.6       apb ATF_TC_BODY(atsecs, tc)
    220      1.6       apb {
    221      1.6       apb 	int tzoff;
    222      1.6       apb 
    223      1.6       apb 	/* "@0" -> (time_t)0, regardless of timezone */
    224      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    225      1.6       apb 	putenv(__UNCONST("TZ=Europe/Berlin"));
    226      1.6       apb 	tzset();
    227      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    228      1.6       apb 	putenv(__UNCONST("TZ=America/New_York"));
    229      1.6       apb 	tzset();
    230      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    231      1.6       apb 	tzoff = 0;
    232      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    233      1.6       apb 	tzoff = 3600;
    234      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    235      1.6       apb 	tzoff = -3600;
    236      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    237      1.6       apb 
    238      1.7       apb 	/* -1 or other negative numbers are not errors */
    239      1.6       apb 	errno = 0;
    240      1.6       apb 	ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0);
    241      1.7       apb 	ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0);
    242      1.7       apb 
    243      1.7       apb 	/* junk is an error */
    244      1.7       apb 	errno = 0;
    245      1.7       apb 	ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0);
    246      1.6       apb }
    247      1.6       apb 
    248      1.1     njoly ATF_TP_ADD_TCS(tp)
    249      1.1     njoly {
    250      1.1     njoly 	ATF_TP_ADD_TC(tp, dates);
    251      1.1     njoly 	ATF_TP_ADD_TC(tp, times);
    252  1.7.8.4       snj 	ATF_TP_ADD_TC(tp, dsttimes);
    253      1.1     njoly 	ATF_TP_ADD_TC(tp, relative);
    254      1.6       apb 	ATF_TP_ADD_TC(tp, atsecs);
    255      1.1     njoly 
    256      1.1     njoly 	return atf_no_error();
    257      1.1     njoly }
    258