etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / time / t_mktime.c
blob8092361dbc3624cc924ca005b0c3f032646265e6
1 /* $NetBSD: t_mktime.c,v 1.5 2012/03/18 07:33:58 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
29 #include <atf-c.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <time.h>
36 ATF_TC(localtime_r_gmt);
37 ATF_TC_HEAD(localtime_r_gmt, tc)
39 atf_tc_set_md_var(tc, "descr", "Test that localtime_r(3) "
40 "returns localtime, not GMT (PR lib/28324)");
43 ATF_TC_BODY(localtime_r_gmt, tc)
45 struct tm *t;
46 struct tm tt;
47 time_t x;
49 x = time(NULL);
50 localtime_r(&x, &tt);
51 t = localtime(&x);
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)");
59 ATF_TC(mktime_negyear);
60 ATF_TC_HEAD(mktime_negyear, tc)
62 atf_tc_set_md_var(tc, "descr", "Test mktime(3) with negative year");
65 ATF_TC_BODY(mktime_negyear, tc)
67 struct tm tms;
68 time_t t;
70 (void)memset(&tms, 0, sizeof(tms));
71 tms.tm_year = ~0;
73 errno = 0;
74 t = mktime(&tms);
75 ATF_REQUIRE_ERRNO(0, t != (time_t)-1);
78 ATF_TC(timegm_epoch);
79 ATF_TC_HEAD(timegm_epoch, tc)
81 atf_tc_set_md_var(tc, "descr", "Test timegm(3) close to the epoch");
84 ATF_TC_BODY(timegm_epoch, tc)
86 struct tm tms;
87 time_t t;
89 /* midnight on 1 Jan 1970 */
90 (void)memset(&tms, 0, sizeof(tms));
91 errno = 0;
92 tms.tm_year = 1970 - 1900;
93 tms.tm_mday = 1;
94 t = timegm(&tms);
95 ATF_REQUIRE_ERRNO(0, t == (time_t)0);
97 /* one second after midnight on 1 Jan 1970 */
98 (void)memset(&tms, 0, sizeof(tms));
99 errno = 0;
100 tms.tm_year = 1970 - 1900;
101 tms.tm_mday = 1;
102 tms.tm_sec = 1;
103 t = timegm(&tms);
104 ATF_REQUIRE_ERRNO(0, t == (time_t)1);
107 * 1969-12-31 23:59:59 = one second before the epoch.
108 * Result should be -1 with errno = 0.
110 (void)memset(&tms, 0, sizeof(tms));
111 errno = 0;
112 tms.tm_year = 1969 - 1900;
113 tms.tm_mon = 12 - 1;
114 tms.tm_mday = 31;
115 tms.tm_hour = 23;
116 tms.tm_min = 59;
117 tms.tm_sec = 59;
118 t = timegm(&tms);
119 ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
122 * Another way of getting one second before the epoch:
123 * Set date to 1 Jan 1970, and time to -1 second.
125 (void)memset(&tms, 0, sizeof(tms));
126 errno = 0;
127 tms.tm_year = 1970 - 1900;
128 tms.tm_mday = 1;
129 tms.tm_sec = -1;
130 t = timegm(&tms);
131 ATF_REQUIRE_ERRNO(0, t == (time_t)-1);
134 * Two seconds before the epoch.
136 (void)memset(&tms, 0, sizeof(tms));
137 errno = 0;
138 tms.tm_year = 1970 - 1900;
139 tms.tm_mday = 1;
140 tms.tm_sec = -2;
141 t = timegm(&tms);
142 ATF_REQUIRE_ERRNO(0, t == (time_t)-2);
147 ATF_TP_ADD_TCS(tp)
150 ATF_TP_ADD_TC(tp, localtime_r_gmt);
151 ATF_TP_ADD_TC(tp, mktime_negyear);
152 ATF_TP_ADD_TC(tp, timegm_epoch);
154 return atf_no_error();