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