test_archive_getdate.c revision 299529
1107178Snjl/*-
2107178Snjl * Copyright (c) 2003-2007 Tim Kientzle
3107178Snjl * All rights reserved.
4107178Snjl *
5107178Snjl * Redistribution and use in source and binary forms, with or without
6107178Snjl * modification, are permitted provided that the following conditions
7107178Snjl * are met:
8107178Snjl * 1. Redistributions of source code must retain the above copyright
9107178Snjl *    notice, this list of conditions and the following disclaimer.
10107178Snjl * 2. Redistributions in binary form must reproduce the above copyright
11107178Snjl *    notice, this list of conditions and the following disclaimer in the
12107178Snjl *    documentation and/or other materials provided with the distribution.
13107178Snjl *
14107178Snjl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15107178Snjl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16107178Snjl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17107178Snjl * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18107178Snjl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19107178Snjl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20107178Snjl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21107178Snjl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22107178Snjl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23107178Snjl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24107178Snjl */
25107178Snjl#include "test.h"
26107178Snjl__FBSDID("$FreeBSD$");
27107178Snjl
28107178Snjl#include <time.h>
29107178Snjl
30107178Snjl#define __LIBARCHIVE_BUILD 1
31107178Snjl#include "archive_getdate.h"
32107178Snjl
33107178Snjl/*
34107178Snjl * Verify that the getdate() function works.
35124307Snjl */
36162704Smjacob
37107178Snjl#define get_date __archive_get_date
38196955Ssbruno
39107178SnjlDEFINE_TEST(test_archive_getdate)
40107178Snjl{
41107178Snjl	time_t now = time(NULL);
42107178Snjl
43107178Snjl	assertEqualInt(get_date(now, "Jan 1, 1970 UTC"), 0);
44107178Snjl	assertEqualInt(get_date(now, "7:12:18-0530 4 May 1983"), 420900138);
45107178Snjl	assertEqualInt(get_date(now, "2004/01/29 513 mest"), 1075345980);
46107178Snjl	assertEqualInt(get_date(now, "99/02/17 7pm utc"), 919278000);
47107178Snjl	assertEqualInt(get_date(now, "02/17/99 7:11am est"), 919253460);
48107178Snjl	assertEqualInt(get_date(now, "now - 2 hours"),
49107178Snjl	    get_date(now, "2 hours ago"));
50107178Snjl	assertEqualInt(get_date(now, "2 hours ago"),
51107178Snjl	    get_date(now, "+2 hours ago"));
52107178Snjl	assertEqualInt(get_date(now, "now - 2 hours"),
53107178Snjl	    get_date(now, "-2 hours"));
54121184Ssimokawa	/* It's important that we handle ctime() format. */
55121184Ssimokawa	assertEqualInt(get_date(now, "Sun Feb 22 17:38:26 PST 2009"),
56121184Ssimokawa	    1235353106);
57121184Ssimokawa	/* Basic relative offsets. */
58121184Ssimokawa	/* If we use the actual current time as the reference, then
59107178Snjl	 * these tests break around DST changes, so it's actually
60107178Snjl	 * important to use a specific reference time here. */
61107178Snjl	assertEqualInt(get_date(0, "tomorrow"), 24 * 60 * 60);
62107178Snjl	assertEqualInt(get_date(0, "yesterday"), - 24 * 60 * 60);
63107178Snjl	assertEqualInt(get_date(0, "now + 1 hour"), 60 * 60);
64107178Snjl	assertEqualInt(get_date(0, "now + 1 hour + 1 minute"), 60 * 60 + 60);
65107178Snjl	/* Repeat the above for a different start time. */
66107178Snjl	now = 1231113600; /* Jan 5, 2009 00:00 UTC */
67107178Snjl	assertEqualInt(get_date(0, "Jan 5, 2009 00:00 UTC"), now);
68107178Snjl	assertEqualInt(get_date(now, "tomorrow"), now + 24 * 60 * 60);
69107178Snjl	assertEqualInt(get_date(now, "yesterday"), now - 24 * 60 * 60);
70107178Snjl	assertEqualInt(get_date(now, "now + 1 hour"), now + 60 * 60);
71107178Snjl	assertEqualInt(get_date(now, "now + 1 hour + 1 minute"),
72107178Snjl	    now + 60 * 60 + 60);
73107178Snjl	assertEqualInt(get_date(now, "tomorrow 5:16am UTC"),
74107178Snjl	    now + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
75107178Snjl	assertEqualInt(get_date(now, "UTC 5:16am tomorrow"),
76107178Snjl	    now + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
77107178Snjl
78107178Snjl	/* Jan 5, 2009 was a Monday. */
79107178Snjl	assertEqualInt(get_date(now, "monday UTC"), now);
80107178Snjl	assertEqualInt(get_date(now, "sunday UTC"), now + 6 * 24 * 60 * 60);
81107178Snjl	assertEqualInt(get_date(now, "tuesday UTC"), now + 24 * 60 * 60);
82107178Snjl	/* "next tuesday" is one week after "tuesday" */
83107178Snjl	assertEqualInt(get_date(now, "UTC next tuesday"),
84107178Snjl	    now + 8 * 24 * 60 * 60);
85107178Snjl	/* "last tuesday" is one week before "tuesday" */
86107178Snjl	assertEqualInt(get_date(now, "last tuesday UTC"),
87107178Snjl	    now - 6 * 24 * 60 * 60);
88107178Snjl	/* TODO: Lots more tests here. */
89107178Snjl}
90107178Snjl