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