xref: /freebsd/contrib/libarchive/libarchive/test/test_archive_parse_date.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 
27 #include <time.h>
28 
29 /*
30  * Verify that the archive_parse_date() function works.
31  */
32 
33 #define get_date archive_parse_date
34 
DEFINE_TEST(test_archive_parse_date)35 DEFINE_TEST(test_archive_parse_date)
36 {
37 	time_t now = time(NULL);
38 
39 	assertEqualInt(get_date(now, "Jan 1, 1970 UTC"), 0);
40 	assertEqualInt(get_date(now, "7:12:18-0530 4 May 1983"), 420900138);
41 	assertEqualInt(get_date(now, "2004/01/29 513 mest"), 1075345980);
42 	assertEqualInt(get_date(now, "99/02/17 7pm utc"), 919278000);
43 	assertEqualInt(get_date(now, "02/17/99 7:11am est"), 919253460);
44 	assertEqualInt(get_date(now, "now - 2 hours"),
45 	    get_date(now, "2 hours ago"));
46 	assertEqualInt(get_date(now, "2 hours ago"),
47 	    get_date(now, "+2 hours ago"));
48 	assertEqualInt(get_date(now, "now - 2 hours"),
49 	    get_date(now, "-2 hours"));
50 	/* It's important that we handle ctime() format. */
51 	assertEqualInt(get_date(now, "Sun Feb 22 17:38:26 PST 2009"),
52 	    1235353106);
53 	/* Basic relative offsets. */
54 	/* If we use the actual current time as the reference, then
55 	 * these tests break around DST changes, so it's actually
56 	 * important to use a specific reference time here. */
57 	assertEqualInt(get_date(0, "tomorrow"), 24 * 60 * 60);
58 	assertEqualInt(get_date(0, "yesterday"), - 24 * 60 * 60);
59 	assertEqualInt(get_date(0, "now + 1 hour"), 60 * 60);
60 	assertEqualInt(get_date(0, "now + 1 hour + 1 minute"), 60 * 60 + 60);
61 	/* Repeat the above for a different start time. */
62 	now = 1231113600; /* Jan 5, 2009 00:00 UTC */
63 	assertEqualInt(get_date(0, "Jan 5, 2009 00:00 UTC"), now);
64 	assertEqualInt(get_date(now, "tomorrow"), now + 24 * 60 * 60);
65 	assertEqualInt(get_date(now, "yesterday"), now - 24 * 60 * 60);
66 	assertEqualInt(get_date(now, "now + 1 hour"), now + 60 * 60);
67 	assertEqualInt(get_date(now, "now + 1 hour + 1 minute"),
68 	    now + 60 * 60 + 60);
69 	assertEqualInt(get_date(now, "tomorrow 5:16am UTC"),
70 	    now + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
71 	assertEqualInt(get_date(now, "UTC 5:16am tomorrow"),
72 	    now + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
73 
74 	/* Jan 5, 2009 was a Monday. */
75 	assertEqualInt(get_date(now, "monday UTC"), now);
76 	assertEqualInt(get_date(now, "sunday UTC"), now + 6 * 24 * 60 * 60);
77 	assertEqualInt(get_date(now, "tuesday UTC"), now + 24 * 60 * 60);
78 	/* "next tuesday" is one week after "tuesday" */
79 	assertEqualInt(get_date(now, "UTC next tuesday"),
80 	    now + 8 * 24 * 60 * 60);
81 	/* "last tuesday" is one week before "tuesday" */
82 	assertEqualInt(get_date(now, "last tuesday UTC"),
83 	    now - 6 * 24 * 60 * 60);
84 
85 	/* Unix epoch timestamps */
86 	assertEqualInt(get_date(now, "@0"), 0);
87 	assertEqualInt(get_date(now, "@100"), 100);
88 	assertEqualInt(get_date(now, "@+100"), 100);
89 
90 	assertEqualInt(get_date(now, "@"), -1);
91 	assertEqualInt(get_date(now, "@-"), -1);
92 	assertEqualInt(get_date(now, "@+"), -1);
93 	assertEqualInt(get_date(now, "@tenth"), -1);
94 	assertEqualInt(get_date(now, "@100 tomorrow"), -1);
95 
96 	/* TODO: Lots more tests here. */
97 }
98