xref: /freebsd/contrib/netbsd-tests/lib/libc/time/t_mktime.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /* $NetBSD: t_mktime.c,v 1.6 2017/10/27 00:55:27 kre 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 = -1;
72 	tms.tm_mday = 1;
73 
74 	errno = 0;
75 	t = mktime(&tms);
76 	ATF_REQUIRE(t != (time_t)-1);
77 }
78 
79 ATF_TC(timegm_epoch);
80 ATF_TC_HEAD(timegm_epoch, tc)
81 {
82 	atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch");
83 }
84 
85 ATF_TC_BODY(timegm_epoch, tc)
86 {
87 	struct tm tms;
88 	time_t t;
89 
90 	/* midnight on 1 Jan 1970 */
91 	(void)memset(&tms, 0, sizeof(tms));
92 	errno = 0;
93 	tms.tm_year = 1970 - 1900;
94 	tms.tm_mday = 1;
95 	t = timegm(&tms);
96 	ATF_REQUIRE(t == (time_t)0);
97 
98 	/* one second after midnight on 1 Jan 1970 */
99 	(void)memset(&tms, 0, sizeof(tms));
100 	errno = 0;
101 	tms.tm_year = 1970 - 1900;
102 	tms.tm_mday = 1;
103 	tms.tm_sec = 1;
104 	t = timegm(&tms);
105 	ATF_REQUIRE(t == (time_t)1);
106 
107 	/*
108 	 * 1969-12-31 23:59:59 = one second before the epoch.
109 	 * Result should be -1 with errno = 0.
110 	 */
111 	(void)memset(&tms, 0, sizeof(tms));
112 	errno = 0;
113 	tms.tm_year = 1969 - 1900;
114 	tms.tm_mon = 12 - 1;
115 	tms.tm_mday = 31;
116 	tms.tm_hour = 23;
117 	tms.tm_min = 59;
118 	tms.tm_sec = 59;
119 	t = timegm(&tms);
120 	ATF_REQUIRE(t == (time_t)-1);
121 	/* ATF_REQUIRE(errno == 0); does not work, errno is kept clear */
122 
123 	/*
124 	 * Another way of getting one second before the epoch:
125 	 * Set date to 1 Jan 1970, and time to -1 second.
126 	 */
127 	(void)memset(&tms, 0, sizeof(tms));
128 	errno = 0;
129 	tms.tm_year = 1970 - 1900;
130 	tms.tm_mday = 1;
131 	tms.tm_sec = -1;
132 	t = timegm(&tms);
133 	ATF_REQUIRE(t == (time_t)-1);
134 
135 	/*
136 	 * Two seconds before the epoch.
137 	 */
138 	(void)memset(&tms, 0, sizeof(tms));
139 	errno = 0;
140 	tms.tm_year = 1970 - 1900;
141 	tms.tm_mday = 1;
142 	tms.tm_sec = -2;
143 	t = timegm(&tms);
144 	ATF_REQUIRE(t == (time_t)-2);
145 
146 }
147 
148 
149 ATF_TP_ADD_TCS(tp)
150 {
151 
152 	ATF_TP_ADD_TC(tp, localtime_r_gmt);
153 	ATF_TP_ADD_TC(tp, mktime_negyear);
154 	ATF_TP_ADD_TC(tp, timegm_epoch);
155 
156 	return atf_no_error();
157 }
158