Home | History | Annotate | Line # | Download | only in libutil
t_parsedate.c revision 1.7.8.3
      1  1.7.8.3       snj /* $NetBSD: t_parsedate.c,v 1.7.8.3 2014/10/11 16:45:24 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.3       snj __RCSID("$NetBSD: t_parsedate.c,v 1.7.8.3 2014/10/11 16:45:24 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.1     njoly ATF_TC(relative);
    151      1.1     njoly 
    152      1.1     njoly ATF_TC_HEAD(relative, tc)
    153      1.1     njoly {
    154      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test relative items"
    155      1.5    jruoho 	    " (PR lib/44255)");
    156      1.1     njoly }
    157      1.1     njoly 
    158      1.1     njoly ATF_TC_BODY(relative, tc)
    159      1.1     njoly {
    160      1.1     njoly 
    161      1.1     njoly 	ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1);
    162      1.1     njoly 	ATF_CHECK(parsedate("last friday", NULL, NULL) != -1);
    163      1.1     njoly 	ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1);
    164      1.1     njoly 	ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1);
    165      1.1     njoly 	ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1);
    166      1.1     njoly 	ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1);
    167      1.1     njoly }
    168      1.1     njoly 
    169      1.6       apb ATF_TC(atsecs);
    170      1.6       apb 
    171      1.6       apb ATF_TC_HEAD(atsecs, tc)
    172      1.6       apb {
    173      1.6       apb 	atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch");
    174      1.6       apb }
    175      1.6       apb 
    176      1.6       apb ATF_TC_BODY(atsecs, tc)
    177      1.6       apb {
    178      1.6       apb 	int tzoff;
    179      1.6       apb 
    180      1.6       apb 	/* "@0" -> (time_t)0, regardless of timezone */
    181      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    182      1.6       apb 	putenv(__UNCONST("TZ=Europe/Berlin"));
    183      1.6       apb 	tzset();
    184      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    185      1.6       apb 	putenv(__UNCONST("TZ=America/New_York"));
    186      1.6       apb 	tzset();
    187      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    188      1.6       apb 	tzoff = 0;
    189      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    190      1.6       apb 	tzoff = 3600;
    191      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    192      1.6       apb 	tzoff = -3600;
    193      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    194      1.6       apb 
    195      1.7       apb 	/* -1 or other negative numbers are not errors */
    196      1.6       apb 	errno = 0;
    197      1.6       apb 	ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0);
    198      1.7       apb 	ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0);
    199      1.7       apb 
    200      1.7       apb 	/* junk is an error */
    201      1.7       apb 	errno = 0;
    202      1.7       apb 	ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0);
    203      1.6       apb }
    204      1.6       apb 
    205      1.1     njoly ATF_TP_ADD_TCS(tp)
    206      1.1     njoly {
    207      1.1     njoly 	ATF_TP_ADD_TC(tp, dates);
    208      1.1     njoly 	ATF_TP_ADD_TC(tp, times);
    209      1.1     njoly 	ATF_TP_ADD_TC(tp, relative);
    210      1.6       apb 	ATF_TP_ADD_TC(tp, atsecs);
    211      1.1     njoly 
    212      1.1     njoly 	return atf_no_error();
    213      1.1     njoly }
    214