xref: /netbsd-src/tests/lib/libc/time/t_mktime.c (revision 9ddb6ab554e70fb9bbd90c3d96b812bc57755a14)
1 /* $NetBSD: t_mktime.c,v 1.4 2012/01/07 15:05:22 martin 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(mktime_negyear);
37 
38 ATF_TC_HEAD(mktime_negyear, tc)
39 {
40 
41 	atf_tc_set_md_var(tc, "descr", "Test mktime(3) with negative year");
42 }
43 
44 ATF_TC_BODY(mktime_negyear, tc)
45 {
46 	struct tm tms;
47 	time_t t;
48 
49 	(void)memset(&tms, 0, sizeof(tms));
50 	tms.tm_year = ~0;
51 
52 	errno = 0;
53 	t = mktime(&tms);
54 	ATF_REQUIRE_ERRNO(0, t != (time_t)-1);
55 }
56 
57 ATF_TC(timegm_epoch);
58 
59 ATF_TC_HEAD(timegm_epoch, tc)
60 {
61 
62 	atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch");
63 }
64 
65 ATF_TC_BODY(timegm_epoch, tc)
66 {
67 	struct tm tms;
68 	time_t t;
69 
70 	/* midnight on 1 Jan 1970 */
71 	(void)memset(&tms, 0, sizeof(tms));
72 	errno = 0;
73 	tms.tm_year = 1970 - 1900;
74 	tms.tm_mday = 1;
75 	t = timegm(&tms);
76 	ATF_REQUIRE_ERRNO(0, t == (time_t)0);
77 
78 	/* one second after midnight on 1 Jan 1970 */
79 	(void)memset(&tms, 0, sizeof(tms));
80 	errno = 0;
81 	tms.tm_year = 1970 - 1900;
82 	tms.tm_mday = 1;
83 	tms.tm_sec = 1;
84 	t = timegm(&tms);
85 	ATF_REQUIRE_ERRNO(0, t == (time_t)1);
86 
87 	/*
88 	 * 1969-12-31 23:59:59 = one second before the epoch.
89 	 * Result should be -1 with errno = 0.
90 	 */
91 	(void)memset(&tms, 0, sizeof(tms));
92 	errno = 0;
93 	tms.tm_year = 1969 - 1900;
94 	tms.tm_mon = 12 - 1;
95 	tms.tm_mday = 31;
96 	tms.tm_hour = 23;
97 	tms.tm_min = 59;
98 	tms.tm_sec = 59;
99 	t = timegm(&tms);
100 	ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
101 
102 	/*
103 	 * Another way of getting one second before the epoch:
104 	 * Set date to 1 Jan 1970, and time to -1 second.
105 	 */
106 	(void)memset(&tms, 0, sizeof(tms));
107 	errno = 0;
108 	tms.tm_year = 1970 - 1900;
109 	tms.tm_mday = 1;
110 	tms.tm_sec = -1;
111 	t = timegm(&tms);
112 	ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
113 
114 	/*
115 	 * Two seconds before the epoch.
116 	 */
117 	(void)memset(&tms, 0, sizeof(tms));
118 	errno = 0;
119 	tms.tm_year = 1970 - 1900;
120 	tms.tm_mday = 1;
121 	tms.tm_sec = -2;
122 	t = timegm(&tms);
123 	ATF_REQUIRE_ERRNO(0, t == (time_t)-2);
124 
125 }
126 
127 
128 ATF_TP_ADD_TCS(tp)
129 {
130 
131 	ATF_TP_ADD_TC(tp, mktime_negyear);
132 	ATF_TP_ADD_TC(tp, timegm_epoch);
133 
134 	return atf_no_error();
135 }
136