xref: /minix3/tests/lib/libc/time/t_mktime.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_mktime.c,v 1.5 2012/03/18 07:33:58 jruoho Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include <atf-c.h>
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <err.h>
32*11be35a1SLionel Sambuc #include <errno.h>
33*11be35a1SLionel Sambuc #include <string.h>
34*11be35a1SLionel Sambuc #include <time.h>
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc ATF_TC(localtime_r_gmt);
ATF_TC_HEAD(localtime_r_gmt,tc)37*11be35a1SLionel Sambuc ATF_TC_HEAD(localtime_r_gmt, tc)
38*11be35a1SLionel Sambuc {
39*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test that localtime_r(3) "
40*11be35a1SLionel Sambuc 	    "returns localtime, not GMT (PR lib/28324)");
41*11be35a1SLionel Sambuc }
42*11be35a1SLionel Sambuc 
ATF_TC_BODY(localtime_r_gmt,tc)43*11be35a1SLionel Sambuc ATF_TC_BODY(localtime_r_gmt, tc)
44*11be35a1SLionel Sambuc {
45*11be35a1SLionel Sambuc 	struct tm *t;
46*11be35a1SLionel Sambuc 	struct tm tt;
47*11be35a1SLionel Sambuc 	time_t x;
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc 	x = time(NULL);
50*11be35a1SLionel Sambuc 	localtime_r(&x, &tt);
51*11be35a1SLionel Sambuc 	t = localtime(&x);
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 	if (t->tm_sec != tt.tm_sec || t->tm_min != tt.tm_min ||
54*11be35a1SLionel Sambuc 	    t->tm_hour != tt.tm_hour || t->tm_mday != tt.tm_mday)
55*11be35a1SLionel Sambuc 		atf_tc_fail("inconsistencies between "
56*11be35a1SLionel Sambuc 		    "localtime(3) and localtime_r(3)");
57*11be35a1SLionel Sambuc }
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc ATF_TC(mktime_negyear);
ATF_TC_HEAD(mktime_negyear,tc)60*11be35a1SLionel Sambuc ATF_TC_HEAD(mktime_negyear, tc)
61*11be35a1SLionel Sambuc {
62*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test mktime(3) with negative year");
63*11be35a1SLionel Sambuc }
64*11be35a1SLionel Sambuc 
ATF_TC_BODY(mktime_negyear,tc)65*11be35a1SLionel Sambuc ATF_TC_BODY(mktime_negyear, tc)
66*11be35a1SLionel Sambuc {
67*11be35a1SLionel Sambuc 	struct tm tms;
68*11be35a1SLionel Sambuc 	time_t t;
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc 	(void)memset(&tms, 0, sizeof(tms));
71*11be35a1SLionel Sambuc 	tms.tm_year = ~0;
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 	errno = 0;
74*11be35a1SLionel Sambuc 	t = mktime(&tms);
75*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(0, t != (time_t)-1);
76*11be35a1SLionel Sambuc }
77*11be35a1SLionel Sambuc 
78*11be35a1SLionel Sambuc ATF_TC(timegm_epoch);
ATF_TC_HEAD(timegm_epoch,tc)79*11be35a1SLionel Sambuc ATF_TC_HEAD(timegm_epoch, tc)
80*11be35a1SLionel Sambuc {
81*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch");
82*11be35a1SLionel Sambuc }
83*11be35a1SLionel Sambuc 
ATF_TC_BODY(timegm_epoch,tc)84*11be35a1SLionel Sambuc ATF_TC_BODY(timegm_epoch, tc)
85*11be35a1SLionel Sambuc {
86*11be35a1SLionel Sambuc 	struct tm tms;
87*11be35a1SLionel Sambuc 	time_t t;
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc 	/* midnight on 1 Jan 1970 */
90*11be35a1SLionel Sambuc 	(void)memset(&tms, 0, sizeof(tms));
91*11be35a1SLionel Sambuc 	errno = 0;
92*11be35a1SLionel Sambuc 	tms.tm_year = 1970 - 1900;
93*11be35a1SLionel Sambuc 	tms.tm_mday = 1;
94*11be35a1SLionel Sambuc 	t = timegm(&tms);
95*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(0, t == (time_t)0);
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc 	/* one second after midnight on 1 Jan 1970 */
98*11be35a1SLionel Sambuc 	(void)memset(&tms, 0, sizeof(tms));
99*11be35a1SLionel Sambuc 	errno = 0;
100*11be35a1SLionel Sambuc 	tms.tm_year = 1970 - 1900;
101*11be35a1SLionel Sambuc 	tms.tm_mday = 1;
102*11be35a1SLionel Sambuc 	tms.tm_sec = 1;
103*11be35a1SLionel Sambuc 	t = timegm(&tms);
104*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(0, t == (time_t)1);
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc 	/*
107*11be35a1SLionel Sambuc 	 * 1969-12-31 23:59:59 = one second before the epoch.
108*11be35a1SLionel Sambuc 	 * Result should be -1 with errno = 0.
109*11be35a1SLionel Sambuc 	 */
110*11be35a1SLionel Sambuc 	(void)memset(&tms, 0, sizeof(tms));
111*11be35a1SLionel Sambuc 	errno = 0;
112*11be35a1SLionel Sambuc 	tms.tm_year = 1969 - 1900;
113*11be35a1SLionel Sambuc 	tms.tm_mon = 12 - 1;
114*11be35a1SLionel Sambuc 	tms.tm_mday = 31;
115*11be35a1SLionel Sambuc 	tms.tm_hour = 23;
116*11be35a1SLionel Sambuc 	tms.tm_min = 59;
117*11be35a1SLionel Sambuc 	tms.tm_sec = 59;
118*11be35a1SLionel Sambuc 	t = timegm(&tms);
119*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc 	/*
122*11be35a1SLionel Sambuc 	 * Another way of getting one second before the epoch:
123*11be35a1SLionel Sambuc 	 * Set date to 1 Jan 1970, and time to -1 second.
124*11be35a1SLionel Sambuc 	 */
125*11be35a1SLionel Sambuc 	(void)memset(&tms, 0, sizeof(tms));
126*11be35a1SLionel Sambuc 	errno = 0;
127*11be35a1SLionel Sambuc 	tms.tm_year = 1970 - 1900;
128*11be35a1SLionel Sambuc 	tms.tm_mday = 1;
129*11be35a1SLionel Sambuc 	tms.tm_sec = -1;
130*11be35a1SLionel Sambuc 	t = timegm(&tms);
131*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
132*11be35a1SLionel Sambuc 
133*11be35a1SLionel Sambuc 	/*
134*11be35a1SLionel Sambuc 	 * Two seconds before the epoch.
135*11be35a1SLionel Sambuc 	 */
136*11be35a1SLionel Sambuc 	(void)memset(&tms, 0, sizeof(tms));
137*11be35a1SLionel Sambuc 	errno = 0;
138*11be35a1SLionel Sambuc 	tms.tm_year = 1970 - 1900;
139*11be35a1SLionel Sambuc 	tms.tm_mday = 1;
140*11be35a1SLionel Sambuc 	tms.tm_sec = -2;
141*11be35a1SLionel Sambuc 	t = timegm(&tms);
142*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(0, t == (time_t)-2);
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc }
145*11be35a1SLionel Sambuc 
146*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)147*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
148*11be35a1SLionel Sambuc {
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, localtime_r_gmt);
151*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, mktime_negyear);
152*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, timegm_epoch);
153*11be35a1SLionel Sambuc 
154*11be35a1SLionel Sambuc 	return atf_no_error();
155*11be35a1SLionel Sambuc }
156