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