1238825Smm/*-
2238825Smm * Copyright (c) 2003-2007 Tim Kientzle
3238825Smm * All rights reserved.
4238825Smm *
5238825Smm * Redistribution and use in source and binary forms, with or without
6238825Smm * modification, are permitted provided that the following conditions
7238825Smm * are met:
8238825Smm * 1. Redistributions of source code must retain the above copyright
9238825Smm *    notice, this list of conditions and the following disclaimer.
10238825Smm * 2. Redistributions in binary form must reproduce the above copyright
11238825Smm *    notice, this list of conditions and the following disclaimer in the
12238825Smm *    documentation and/or other materials provided with the distribution.
13238825Smm *
14238825Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15238825Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16238825Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17238825Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18238825Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19238825Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20238825Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21238825Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22238825Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23238825Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24238825Smm */
25238825Smm#include "test.h"
26238825Smm__FBSDID("$FreeBSD$");
27238825Smm
28238825Smm#include <time.h>
29238825Smm
30238825Smm/*
31238825Smm * Verify that the getdate() function works.
32238825Smm */
33238825Smm
34238825Smmtime_t __archive_get_date(time_t, const char *);
35238825Smm#define get_date __archive_get_date
36238825Smm
37238825SmmDEFINE_TEST(test_archive_getdate)
38238825Smm{
39238825Smm	time_t now = time(NULL);
40238825Smm
41238825Smm	assertEqualInt(get_date(now, "Jan 1, 1970 UTC"), 0);
42238825Smm	assertEqualInt(get_date(now, "7:12:18-0530 4 May 1983"), 420900138);
43238825Smm	assertEqualInt(get_date(now, "2004/01/29 513 mest"), 1075345980);
44238825Smm	assertEqualInt(get_date(now, "99/02/17 7pm utc"), 919278000);
45238825Smm	assertEqualInt(get_date(now, "02/17/99 7:11am est"), 919253460);
46246229Skientzle	assertEqualInt(get_date(now, "now - 2 hours"),
47246229Skientzle	    get_date(now, "2 hours ago"));
48238825Smm	/* It's important that we handle ctime() format. */
49238825Smm	assertEqualInt(get_date(now, "Sun Feb 22 17:38:26 PST 2009"),
50238825Smm	    1235353106);
51238825Smm	/* Basic relative offsets. */
52238825Smm	/* If we use the actual current time as the reference, then
53238825Smm	 * these tests break around DST changes, so it's actually
54238825Smm	 * important to use a specific reference time here. */
55238825Smm	assertEqualInt(get_date(0, "tomorrow"), 24 * 60 * 60);
56238825Smm	assertEqualInt(get_date(0, "yesterday"), - 24 * 60 * 60);
57238825Smm	assertEqualInt(get_date(0, "now + 1 hour"), 60 * 60);
58238825Smm	assertEqualInt(get_date(0, "now + 1 hour + 1 minute"), 60 * 60 + 60);
59238825Smm	/* Repeat the above for a different start time. */
60238825Smm	now = 1231113600; /* Jan 5, 2009 00:00 UTC */
61238825Smm	assertEqualInt(get_date(0, "Jan 5, 2009 00:00 UTC"), now);
62238825Smm	assertEqualInt(get_date(now, "tomorrow"), now + 24 * 60 * 60);
63238825Smm	assertEqualInt(get_date(now, "yesterday"), now - 24 * 60 * 60);
64238825Smm	assertEqualInt(get_date(now, "now + 1 hour"), now + 60 * 60);
65238825Smm	assertEqualInt(get_date(now, "now + 1 hour + 1 minute"),
66238825Smm	    now + 60 * 60 + 60);
67238825Smm	assertEqualInt(get_date(now, "tomorrow 5:16am UTC"),
68238825Smm	    now + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
69238825Smm	assertEqualInt(get_date(now, "UTC 5:16am tomorrow"),
70238825Smm	    now + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
71238825Smm
72238825Smm	/* Jan 5, 2009 was a Monday. */
73238825Smm	assertEqualInt(get_date(now, "monday UTC"), now);
74238825Smm	assertEqualInt(get_date(now, "sunday UTC"), now + 6 * 24 * 60 * 60);
75238825Smm	assertEqualInt(get_date(now, "tuesday UTC"), now + 24 * 60 * 60);
76238825Smm	/* "next tuesday" is one week after "tuesday" */
77238825Smm	assertEqualInt(get_date(now, "UTC next tuesday"),
78238825Smm	    now + 8 * 24 * 60 * 60);
79238825Smm	/* "last tuesday" is one week before "tuesday" */
80238825Smm	assertEqualInt(get_date(now, "last tuesday UTC"),
81238825Smm	    now - 6 * 24 * 60 * 60);
82246229Skientzle
83238825Smm	/* TODO: Lots more tests here. */
84238825Smm}
85