1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26229592Smm__FBSDID("$FreeBSD$");
27228753Smm
28228753Smm#include <time.h>
29228753Smm
30228753Smm/*
31228753Smm * Verify that the getdate() function works.
32228753Smm */
33228753Smm
34228753Smmtime_t get_date(time_t, const char *);
35228753Smm
36228753SmmDEFINE_TEST(test_getdate)
37228753Smm{
38228753Smm	time_t now = time(NULL);
39228753Smm
40228753Smm	assertEqualInt(get_date(now, "Jan 1, 1970 UTC"), 0);
41228753Smm	assertEqualInt(get_date(now, "7:12:18-0530 4 May 1983"), 420900138);
42228753Smm	assertEqualInt(get_date(now, "2004/01/29 513 mest"), 1075345980);
43228753Smm	assertEqualInt(get_date(now, "99/02/17 7pm utc"), 919278000);
44228753Smm	assertEqualInt(get_date(now, "02/17/99 7:11am est"), 919253460);
45228753Smm	/* It's important that we handle ctime() format. */
46228753Smm	assertEqualInt(get_date(now, "Sun Feb 22 17:38:26 PST 2009"),
47228753Smm	    1235353106);
48228753Smm	/* Basic relative offsets. */
49228753Smm	/* If we use the actual current time as the reference, then
50228753Smm	 * these tests break around DST changes, so it's actually
51228753Smm	 * important to use a specific reference time here. */
52228753Smm	assertEqualInt(get_date(0, "tomorrow"), 24 * 60 * 60);
53228753Smm	assertEqualInt(get_date(0, "yesterday"), - 24 * 60 * 60);
54228753Smm	assertEqualInt(get_date(0, "now + 1 hour"), 60 * 60);
55228753Smm	assertEqualInt(get_date(0, "now + 1 hour + 1 minute"), 60 * 60 + 60);
56228753Smm	/* Repeat the above for a different start time. */
57228753Smm	now = 1231113600; /* Jan 5, 2009 00:00 UTC */
58228753Smm	assertEqualInt(get_date(0, "Jan 5, 2009 00:00 UTC"), now);
59228753Smm	assertEqualInt(get_date(now, "tomorrow"), now + 24 * 60 * 60);
60228753Smm	assertEqualInt(get_date(now, "yesterday"), now - 24 * 60 * 60);
61228753Smm	assertEqualInt(get_date(now, "now + 1 hour"), now + 60 * 60);
62228753Smm	assertEqualInt(get_date(now, "now + 1 hour + 1 minute"),
63228753Smm	    now + 60 * 60 + 60);
64228753Smm	assertEqualInt(get_date(now, "tomorrow 5:16am UTC"),
65228753Smm	    now + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
66228753Smm	assertEqualInt(get_date(now, "UTC 5:16am tomorrow"),
67228753Smm	    now + 24 * 60 * 60 + 5 * 60 * 60 + 16 * 60);
68228753Smm
69228753Smm	/* Jan 5, 2009 was a Monday. */
70228753Smm	assertEqualInt(get_date(now, "monday UTC"), now);
71228753Smm	assertEqualInt(get_date(now, "sunday UTC"), now + 6 * 24 * 60 * 60);
72228753Smm	assertEqualInt(get_date(now, "tuesday UTC"), now + 24 * 60 * 60);
73228753Smm	/* "next tuesday" is one week after "tuesday" */
74228753Smm	assertEqualInt(get_date(now, "UTC next tuesday"),
75228753Smm	    now + 8 * 24 * 60 * 60);
76228753Smm	/* "last tuesday" is one week before "tuesday" */
77228753Smm	assertEqualInt(get_date(now, "last tuesday UTC"),
78228753Smm	    now - 6 * 24 * 60 * 60);
79228753Smm	/* TODO: Lots more tests here. */
80228753Smm}
81