Home | History | Annotate | Line # | Download | only in libutil
t_parsedate.c revision 1.7.8.1
      1  1.7.8.1       snj /* $NetBSD: t_parsedate.c,v 1.7.8.1 2014/10/11 16:42:45 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.1       snj __RCSID("$NetBSD: t_parsedate.c,v 1.7.8.1 2014/10/11 16:42:45 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.1       snj 	parsecheck("69-09-10", NULL, NULL, localtime_r,
    104  1.7.8.1       snj 		2069, 9, 10, 0, 0, 0);
    105  1.7.8.1       snj 	parsecheck("2006-11-17", NULL, NULL, localtime_r,
    106  1.7.8.1       snj 		2006, 11, 17, 0, 0, 0);
    107  1.7.8.1       snj 	parsecheck("10/1/2000", NULL, NULL, localtime_r,
    108  1.7.8.1       snj 		2000, 10, 1, 0, 0, 0);
    109  1.7.8.1       snj 	parsecheck("20 Jun 1994", NULL, NULL, localtime_r,
    110  1.7.8.1       snj 		1994, 6, 20, 0, 0, 0);
    111  1.7.8.1       snj 	parsecheck("23jun2001", NULL, NULL, localtime_r,
    112  1.7.8.1       snj 		2001, 6, 23, 0, 0, 0);
    113  1.7.8.1       snj 	parsecheck("1-sep-06", NULL, NULL, localtime_r,
    114  1.7.8.1       snj 		2006, 9, 1, 0, 0, 0);
    115  1.7.8.1       snj 	parsecheck("1/11", NULL, NULL, localtime_r,
    116  1.7.8.1       snj 		ANY, 1, 11, 0, 0, 0);
    117  1.7.8.1       snj 	parsecheck("1500-01-02", NULL, NULL, localtime_r,
    118  1.7.8.1       snj 		1500, 1, 2, 0, 0, 0);
    119  1.7.8.1       snj 	parsecheck("9999-12-21", NULL, NULL, localtime_r,
    120  1.7.8.1       snj 		9999, 12, 21, 0, 0, 0);
    121      1.1     njoly }
    122      1.1     njoly 
    123      1.1     njoly ATF_TC(times);
    124      1.1     njoly 
    125      1.1     njoly ATF_TC_HEAD(times, tc)
    126      1.1     njoly {
    127      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test times"
    128      1.5    jruoho 	    " (PR lib/44255)");
    129      1.1     njoly }
    130      1.1     njoly 
    131      1.1     njoly ATF_TC_BODY(times, tc)
    132      1.1     njoly {
    133      1.1     njoly 
    134  1.7.8.1       snj 	parsecheck("10:01", NULL, NULL, localtime_r,
    135  1.7.8.1       snj 		ANY, ANY, ANY, 10, 1, 0);
    136  1.7.8.1       snj 	parsecheck("10:12pm", NULL, NULL, localtime_r,
    137  1.7.8.1       snj 		ANY, ANY, ANY, 22, 12, 0);
    138  1.7.8.1       snj 	parsecheck("12:11:01.000012", NULL, NULL, localtime_r,
    139  1.7.8.1       snj 		ANY, ANY, ANY, 12, 11, 1);
    140  1.7.8.1       snj 	parsecheck("12:21-0500", NULL, NULL, gmtime_r,
    141  1.7.8.1       snj 		ANY, ANY, ANY, 12+5, 21, 0);
    142      1.1     njoly }
    143      1.1     njoly 
    144      1.1     njoly ATF_TC(relative);
    145      1.1     njoly 
    146      1.1     njoly ATF_TC_HEAD(relative, tc)
    147      1.1     njoly {
    148      1.4  christos 	atf_tc_set_md_var(tc, "descr", "Test relative items"
    149      1.5    jruoho 	    " (PR lib/44255)");
    150      1.1     njoly }
    151      1.1     njoly 
    152      1.1     njoly ATF_TC_BODY(relative, tc)
    153      1.1     njoly {
    154      1.1     njoly 
    155      1.1     njoly 	ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1);
    156      1.1     njoly 	ATF_CHECK(parsedate("last friday", NULL, NULL) != -1);
    157      1.1     njoly 	ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1);
    158      1.1     njoly 	ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1);
    159      1.1     njoly 	ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1);
    160      1.1     njoly 	ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1);
    161      1.1     njoly }
    162      1.1     njoly 
    163      1.6       apb ATF_TC(atsecs);
    164      1.6       apb 
    165      1.6       apb ATF_TC_HEAD(atsecs, tc)
    166      1.6       apb {
    167      1.6       apb 	atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch");
    168      1.6       apb }
    169      1.6       apb 
    170      1.6       apb ATF_TC_BODY(atsecs, tc)
    171      1.6       apb {
    172      1.6       apb 	int tzoff;
    173      1.6       apb 
    174      1.6       apb 	/* "@0" -> (time_t)0, regardless of timezone */
    175      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    176      1.6       apb 	putenv(__UNCONST("TZ=Europe/Berlin"));
    177      1.6       apb 	tzset();
    178      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    179      1.6       apb 	putenv(__UNCONST("TZ=America/New_York"));
    180      1.6       apb 	tzset();
    181      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
    182      1.6       apb 	tzoff = 0;
    183      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    184      1.6       apb 	tzoff = 3600;
    185      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    186      1.6       apb 	tzoff = -3600;
    187      1.6       apb 	ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
    188      1.6       apb 
    189      1.7       apb 	/* -1 or other negative numbers are not errors */
    190      1.6       apb 	errno = 0;
    191      1.6       apb 	ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0);
    192      1.7       apb 	ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0);
    193      1.7       apb 
    194      1.7       apb 	/* junk is an error */
    195      1.7       apb 	errno = 0;
    196      1.7       apb 	ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0);
    197      1.6       apb }
    198      1.6       apb 
    199      1.1     njoly ATF_TP_ADD_TCS(tp)
    200      1.1     njoly {
    201      1.1     njoly 	ATF_TP_ADD_TC(tp, dates);
    202      1.1     njoly 	ATF_TP_ADD_TC(tp, times);
    203      1.1     njoly 	ATF_TP_ADD_TC(tp, relative);
    204      1.6       apb 	ATF_TP_ADD_TC(tp, atsecs);
    205      1.1     njoly 
    206      1.1     njoly 	return atf_no_error();
    207      1.1     njoly }
    208