Home | History | Annotate | Line # | Download | only in libutil
t_parsedate.c revision 1.7.8.6
      1  1.7.8.6       snj /* $NetBSD: t_parsedate.c,v 1.7.8.6 2014/10/11 16:55:32 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.6       snj __RCSID("$NetBSD: t_parsedate.c,v 1.7.8.6 2014/10/11 16:55:32 snj Exp $");
     33      1.1     njoly 
     34      1.1     njoly #include <atf-c.h>
     35      1.6       apb #include <errno.h>
     36  1.7.8.5       snj #include <stdio.h>
     37      1.6       apb #include <stdlib.h>
     38      1.6       apb #include <time.h>
     39      1.1     njoly #include <util.h>
     40      1.1     njoly 
     41  1.7.8.1       snj /*
     42  1.7.8.1       snj  * ANY is used as a placeholder for values that do not need to be
     43  1.7.8.1       snj  * checked.  The actual value is arbitrary.  We don't use -1
     44  1.7.8.1       snj  * because some tests might want to use -1 as a literal value.
     45  1.7.8.1       snj  */
     46  1.7.8.1       snj #define ANY -30215
     47  1.7.8.1       snj 
     48  1.7.8.1       snj /* parsecheck --
     49  1.7.8.1       snj  * call parsedate(), then call time_to_tm() on the result,
     50  1.7.8.1       snj  * and check that year/month/day/hour/minute/second are as expected.
     51  1.7.8.1       snj  *
     52  1.7.8.1       snj  * time_to_tm should usually be localtime_r or gmtime_r.
     53  1.7.8.1       snj  *
     54  1.7.8.1       snj  * Don't check values specified as ANY.
     55  1.7.8.1       snj  */
     56  1.7.8.1       snj static void
     57  1.7.8.1       snj parsecheck(const char *datestr, const time_t *reftime, const int *zoff,
     58  1.7.8.1       snj 	struct tm * time_to_tm(const time_t *, struct tm *),
     59  1.7.8.1       snj 	int year, int month, int day, int hour, int minute, int second)
     60  1.7.8.1       snj {
     61  1.7.8.1       snj 	time_t t;
     62  1.7.8.1       snj 	struct tm tm;
     63  1.7.8.5       snj 	char argstr[128];
     64  1.7.8.5       snj 
     65  1.7.8.5       snj 	/*
     66  1.7.8.5       snj 	 * printable version of the args.
     67  1.7.8.5       snj 	 *
     68  1.7.8.5       snj 	 * Note that printf("%.*d", 0, 0)) prints nothing at all,
     69  1.7.8.5       snj 	 * while printf("%.*d", 1, val) prints the value as usual.
     70  1.7.8.5       snj 	 */
     71  1.7.8.5       snj 	snprintf(argstr, sizeof(argstr), "%s%s%s, %s%.*jd, %s%.*d",
     72  1.7.8.5       snj 		/* NULL or \"<datestr>\" */
     73  1.7.8.5       snj 		(datestr ? "\"" : ""),
     74  1.7.8.5       snj 		(datestr ? datestr : "NULL"),
     75  1.7.8.5       snj 		(datestr ? "\"" : ""),
     76  1.7.8.5       snj 		/* NULL or *reftime */
     77  1.7.8.5       snj 		(reftime ? "" : "NULL"),
     78  1.7.8.5       snj 		(reftime ? 1 : 0),
     79  1.7.8.5       snj 		(reftime ? (intmax_t)*reftime : (intmax_t)0),
     80  1.7.8.5       snj 		/* NULL or *zoff */
     81  1.7.8.5       snj 		(zoff ? "" : "NULL"),
     82  1.7.8.5       snj 		(zoff ? 1 : 0),
     83  1.7.8.5       snj 		(zoff ? *zoff : 0));
     84  1.7.8.1       snj 
     85  1.7.8.1       snj 	ATF_CHECK_MSG((t = parsedate(datestr, reftime, zoff)) != -1,
     86  1.7.8.5       snj 	    "parsedate(%s) returned -1\n", argstr);
     87  1.7.8.1       snj 	ATF_CHECK(time_to_tm(&t, &tm) != NULL);
     88  1.7.8.1       snj 	if (year != ANY)
     89  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_year + 1900 == year,
     90  1.7.8.5       snj 		    "parsedate(%s) expected year %d got %d (+1900)\n",
     91  1.7.8.5       snj 		    argstr, year, (int)tm.tm_year);
     92  1.7.8.1       snj 	if (month != ANY)
     93  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_mon + 1 == month,
     94  1.7.8.5       snj 		    "parsedate(%s) expected month %d got %d (+1)\n",
     95  1.7.8.5       snj 		    argstr, month, (int)tm.tm_mon);
     96  1.7.8.1       snj 	if (day != ANY)
     97  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_mday == day,
     98  1.7.8.5       snj 		    "parsedate(%s) expected day %d got %d\n",
     99  1.7.8.5       snj 		    argstr, day, (int)tm.tm_mday);
    100  1.7.8.1       snj 	if (hour != ANY)
    101  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_hour == hour,
    102  1.7.8.5       snj 		    "parsedate(%s) expected hour %d got %d\n",
    103  1.7.8.5       snj 		    argstr, hour, (int)tm.tm_hour);
    104  1.7.8.1       snj 	if (minute != ANY)
    105  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_min == minute,
    106  1.7.8.5       snj 		    "parsedate(%s) expected minute %d got %d\n",
    107  1.7.8.5       snj 		    argstr, minute, (int)tm.tm_min);
    108  1.7.8.1       snj 	if (second != ANY)
    109  1.7.8.1       snj 		ATF_CHECK_MSG(tm.tm_sec == second,
    110  1.7.8.5       snj 		    "parsedate(%s) expected second %d got %d\n",
    111  1.7.8.5       snj 		    argstr, second, (int)tm.tm_sec);
    112  1.7.8.1       snj }
    113  1.7.8.1       snj 
    114      1.1     njoly ATF_TC(dates);
    115      1.1     njoly 
    116      1.1     njoly ATF_TC_HEAD(dates, tc)
    117      1.1     njoly {
    118      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test unambiguous dates"
    119      1.5    jruoho 	    " (PR lib/44255)");
    120      1.1     njoly }
    121      1.1     njoly 
    122      1.1     njoly ATF_TC_BODY(dates, tc)
    123      1.1     njoly {
    124      1.1     njoly 
    125  1.7.8.3       snj 	parsecheck("9/10/69", NULL, NULL, localtime_r,
    126  1.7.8.2       snj 		2069, 9, 10, 0, 0, 0); /* year < 70: add 2000 */
    127  1.7.8.3       snj 	parsecheck("9/10/70", NULL, NULL, localtime_r,
    128  1.7.8.2       snj 		1970, 9, 10, 0, 0, 0); /* 70 <= year < 100: add 1900 */
    129  1.7.8.3       snj 	parsecheck("69-09-10", NULL, NULL, localtime_r,
    130  1.7.8.3       snj 		69, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
    131  1.7.8.3       snj 	parsecheck("70-09-10", NULL, NULL, localtime_r,
    132  1.7.8.3       snj 		70, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
    133  1.7.8.1       snj 	parsecheck("2006-11-17", NULL, NULL, localtime_r,
    134  1.7.8.1       snj 		2006, 11, 17, 0, 0, 0);
    135  1.7.8.1       snj 	parsecheck("10/1/2000", NULL, NULL, localtime_r,
    136  1.7.8.2       snj 		2000, 10, 1, 0, 0, 0); /* month/day/year */
    137  1.7.8.1       snj 	parsecheck("20 Jun 1994", NULL, NULL, localtime_r,
    138  1.7.8.1       snj 		1994, 6, 20, 0, 0, 0);
    139  1.7.8.1       snj 	parsecheck("23jun2001", NULL, NULL, localtime_r,
    140  1.7.8.1       snj 		2001, 6, 23, 0, 0, 0);
    141  1.7.8.1       snj 	parsecheck("1-sep-06", NULL, NULL, localtime_r,
    142  1.7.8.1       snj 		2006, 9, 1, 0, 0, 0);
    143  1.7.8.1       snj 	parsecheck("1/11", NULL, NULL, localtime_r,
    144  1.7.8.2       snj 		ANY, 1, 11, 0, 0, 0); /* month/day */
    145  1.7.8.1       snj 	parsecheck("1500-01-02", NULL, NULL, localtime_r,
    146  1.7.8.1       snj 		1500, 1, 2, 0, 0, 0);
    147  1.7.8.1       snj 	parsecheck("9999-12-21", NULL, NULL, localtime_r,
    148  1.7.8.1       snj 		9999, 12, 21, 0, 0, 0);
    149      1.1     njoly }
    150      1.1     njoly 
    151      1.1     njoly ATF_TC(times);
    152      1.1     njoly 
    153      1.1     njoly ATF_TC_HEAD(times, tc)
    154      1.1     njoly {
    155      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test times"
    156      1.5    jruoho 	    " (PR lib/44255)");
    157      1.1     njoly }
    158      1.1     njoly 
    159      1.1     njoly ATF_TC_BODY(times, tc)
    160      1.1     njoly {
    161      1.1     njoly 
    162  1.7.8.1       snj 	parsecheck("10:01", NULL, NULL, localtime_r,
    163  1.7.8.1       snj 		ANY, ANY, ANY, 10, 1, 0);
    164  1.7.8.1       snj 	parsecheck("10:12pm", NULL, NULL, localtime_r,
    165  1.7.8.1       snj 		ANY, ANY, ANY, 22, 12, 0);
    166  1.7.8.1       snj 	parsecheck("12:11:01.000012", NULL, NULL, localtime_r,
    167  1.7.8.1       snj 		ANY, ANY, ANY, 12, 11, 1);
    168  1.7.8.1       snj 	parsecheck("12:21-0500", NULL, NULL, gmtime_r,
    169  1.7.8.1       snj 		ANY, ANY, ANY, 12+5, 21, 0);
    170      1.1     njoly }
    171      1.1     njoly 
    172  1.7.8.4       snj ATF_TC(dsttimes);
    173  1.7.8.4       snj 
    174  1.7.8.4       snj ATF_TC_HEAD(dsttimes, tc)
    175  1.7.8.4       snj {
    176  1.7.8.4       snj 	atf_tc_set_md_var(tc, "descr", "Test DST transition times"
    177  1.7.8.4       snj 	    " (PR lib/47916)");
    178  1.7.8.4       snj }
    179  1.7.8.4       snj 
    180  1.7.8.4       snj ATF_TC_BODY(dsttimes, tc)
    181  1.7.8.4       snj {
    182  1.7.8.4       snj 	struct tm tm;
    183  1.7.8.4       snj 	time_t t;
    184  1.7.8.4       snj 	int tzoff;
    185  1.7.8.4       snj 
    186  1.7.8.4       snj 	putenv(__UNCONST("TZ=EST"));
    187  1.7.8.4       snj 	tzset();
    188  1.7.8.4       snj 	parsecheck("12:0", NULL, NULL, localtime_r,
    189  1.7.8.4       snj 		ANY, ANY, ANY, 12, 0, 0);
    190  1.7.8.4       snj 
    191  1.7.8.4       snj 	putenv(__UNCONST("TZ=Japan"));
    192  1.7.8.4       snj 	tzset();
    193  1.7.8.4       snj 	parsecheck("12:0", NULL, NULL, localtime_r,
    194  1.7.8.4       snj 		ANY, ANY, ANY, 12, 0, 0);
    195  1.7.8.4       snj 
    196  1.7.8.4       snj 	/*
    197  1.7.8.4       snj 	 * When the effective local time is Tue Jul  9 13:21:53 BST 2013,
    198  1.7.8.4       snj 	 * check mktime("14:00")
    199  1.7.8.4       snj 	 */
    200  1.7.8.4       snj 	putenv(__UNCONST("TZ=Europe/London"));
    201  1.7.8.4       snj 	tzset();
    202  1.7.8.4       snj 	tm = (struct tm){
    203  1.7.8.4       snj 		.tm_year = 2013-1900, .tm_mon = 7-1, .tm_mday = 9,
    204  1.7.8.4       snj 		.tm_hour = 13, .tm_min = 21, .tm_sec = 53,
    205  1.7.8.4       snj 		.tm_isdst = 0 };
    206  1.7.8.4       snj 	t = mktime(&tm);
    207  1.7.8.4       snj 	ATF_CHECK(t != (time_t)-1);
    208  1.7.8.4       snj 	parsecheck("14:00", &t, NULL, localtime_r,
    209  1.7.8.4       snj 		2013, 7, 9, 14, 0, 0);
    210  1.7.8.6       snj 	tzoff = -60; /* British Summer Time */
    211  1.7.8.4       snj 	parsecheck("14:00", &t, &tzoff, localtime_r,
    212  1.7.8.4       snj 		2013, 7, 9, 14, 0, 0);
    213  1.7.8.4       snj }
    214  1.7.8.4       snj 
    215      1.1     njoly ATF_TC(relative);
    216      1.1     njoly 
    217      1.1     njoly ATF_TC_HEAD(relative, tc)
    218      1.1     njoly {
    219      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test relative items"
    220      1.5    jruoho 	    " (PR lib/44255)");
    221      1.1     njoly }
    222      1.1     njoly 
    223      1.1     njoly ATF_TC_BODY(relative, tc)
    224      1.1     njoly {
    225      1.1     njoly 
    226      1.1     njoly 	ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1);
    227      1.1     njoly 	ATF_CHECK(parsedate("last friday", NULL, NULL) != -1);
    228      1.1     njoly 	ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1);
    229      1.1     njoly 	ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1);
    230      1.1     njoly 	ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1);
    231      1.1     njoly 	ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1);
    232      1.1     njoly }
    233      1.1     njoly 
    234      1.6       apb ATF_TC(atsecs);
    235      1.6       apb 
    236      1.6       apb ATF_TC_HEAD(atsecs, tc)
    237      1.6       apb {
    238      1.6       apb 	atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch");
    239      1.6       apb }
    240      1.6       apb 
    241      1.6       apb ATF_TC_BODY(atsecs, tc)
    242      1.6       apb {
    243      1.6       apb 	int tzoff;
    244      1.6       apb 
    245      1.6       apb 	/* "@0" -> (time_t)0, regardless of timezone */
    246      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    247      1.6       apb 	putenv(__UNCONST("TZ=Europe/Berlin"));
    248      1.6       apb 	tzset();
    249      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    250      1.6       apb 	putenv(__UNCONST("TZ=America/New_York"));
    251      1.6       apb 	tzset();
    252      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    253      1.6       apb 	tzoff = 0;
    254      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    255      1.6       apb 	tzoff = 3600;
    256      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    257      1.6       apb 	tzoff = -3600;
    258      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    259      1.6       apb 
    260      1.7       apb 	/* -1 or other negative numbers are not errors */
    261      1.6       apb 	errno = 0;
    262      1.6       apb 	ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0);
    263      1.7       apb 	ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0);
    264      1.7       apb 
    265      1.7       apb 	/* junk is an error */
    266      1.7       apb 	errno = 0;
    267      1.7       apb 	ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0);
    268      1.6       apb }
    269      1.6       apb 
    270      1.1     njoly ATF_TP_ADD_TCS(tp)
    271      1.1     njoly {
    272      1.1     njoly 	ATF_TP_ADD_TC(tp, dates);
    273      1.1     njoly 	ATF_TP_ADD_TC(tp, times);
    274  1.7.8.4       snj 	ATF_TP_ADD_TC(tp, dsttimes);
    275      1.1     njoly 	ATF_TP_ADD_TC(tp, relative);
    276      1.6       apb 	ATF_TP_ADD_TC(tp, atsecs);
    277      1.1     njoly 
    278      1.1     njoly 	return atf_no_error();
    279      1.1     njoly }
    280