1 //===-- Unittests for gmtime ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/errno/libc_errno.h"
10 #include "src/time/gmtime.h"
11 #include "src/time/time_utils.h"
12 #include "test/UnitTest/ErrnoSetterMatcher.h"
13 #include "test/UnitTest/Test.h"
14 #include "test/src/time/TmMatcher.h"
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
19 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
20 using LIBC_NAMESPACE::time_utils::TimeConstants
;
22 TEST(LlvmLibcGmTime
, OutOfRange
) {
23 if (sizeof(time_t) < sizeof(int64_t))
26 1 + INT_MAX
* static_cast<int64_t>(
27 TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR
);
28 struct tm
*tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
29 EXPECT_TRUE(tm_data
== nullptr);
30 EXPECT_EQ(libc_errno
, EOVERFLOW
);
33 seconds
= INT_MIN
* static_cast<int64_t>(
34 TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR
) -
36 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
37 EXPECT_TRUE(tm_data
== nullptr);
38 EXPECT_EQ(libc_errno
, EOVERFLOW
);
41 TEST(LlvmLibcGmTime
, InvalidSeconds
) {
43 struct tm
*tm_data
= nullptr;
44 // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59.
46 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
47 EXPECT_TM_EQ((tm
{59, // sec
51 12 - 1, // tm_mon starts with 0 for Jan
52 1969 - TimeConstants::TIME_YEAR_BASE
, // year
57 // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00.
59 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
60 EXPECT_TM_EQ((tm
{0, // sec
64 0, // tm_mon starts with 0 for Jan
65 1970 - TimeConstants::TIME_YEAR_BASE
, // year
72 TEST(LlvmLibcGmTime
, InvalidMinutes
) {
74 struct tm
*tm_data
= nullptr;
75 // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00.
76 seconds
= -TimeConstants::SECONDS_PER_MIN
;
77 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
78 EXPECT_TM_EQ((tm
{0, // sec
82 11, // tm_mon starts with 0 for Jan
83 1969 - TimeConstants::TIME_YEAR_BASE
, // year
88 // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00.
89 seconds
= 60 * TimeConstants::SECONDS_PER_MIN
;
90 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
91 EXPECT_TM_EQ((tm
{0, // sec
95 0, // tm_mon starts with 0 for Jan
96 1970 - TimeConstants::TIME_YEAR_BASE
, // year
103 TEST(LlvmLibcGmTime
, InvalidHours
) {
105 struct tm
*tm_data
= nullptr;
106 // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00.
107 seconds
= -TimeConstants::SECONDS_PER_HOUR
;
108 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
109 EXPECT_TM_EQ((tm
{0, // sec
113 11, // tm_mon starts with 0 for Jan
114 1969 - TimeConstants::TIME_YEAR_BASE
, // year
119 // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00.
120 seconds
= 24 * TimeConstants::SECONDS_PER_HOUR
;
121 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
122 EXPECT_TM_EQ((tm
{0, // sec
126 0, // tm_mon starts with 0 for Jan
127 1970 - TimeConstants::TIME_YEAR_BASE
, // year
134 TEST(LlvmLibcGmTime
, InvalidYear
) {
135 // -1 year from 1970-01-01 00:00:00 returns 1969-01-01 00:00:00.
137 -TimeConstants::DAYS_PER_NON_LEAP_YEAR
* TimeConstants::SECONDS_PER_DAY
;
138 struct tm
*tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
139 EXPECT_TM_EQ((tm
{0, // sec
143 0, // tm_mon starts with 0 for Jan
144 1969 - TimeConstants::TIME_YEAR_BASE
, // year
151 TEST(LlvmLibcGmTime
, InvalidMonths
) {
153 struct tm
*tm_data
= nullptr;
154 // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00.
155 seconds
= -31 * TimeConstants::SECONDS_PER_DAY
;
156 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
157 EXPECT_TM_EQ((tm
{0, // sec
161 12 - 1, // tm_mon starts with 0 for Jan
162 1969 - TimeConstants::TIME_YEAR_BASE
, // year
167 // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00.
169 TimeConstants::DAYS_PER_NON_LEAP_YEAR
* TimeConstants::SECONDS_PER_DAY
;
170 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
171 EXPECT_TM_EQ((tm
{0, // sec
175 0, // tm_mon starts with 0 for Jan
176 1971 - TimeConstants::TIME_YEAR_BASE
, // year
183 TEST(LlvmLibcGmTime
, InvalidDays
) {
185 struct tm
*tm_data
= nullptr;
186 // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00.
187 seconds
= -1 * TimeConstants::SECONDS_PER_DAY
;
188 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
189 EXPECT_TM_EQ((tm
{0, // sec
193 11, // tm_mon starts with 0 for Jan
194 1969 - TimeConstants::TIME_YEAR_BASE
, // year
200 // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00.
201 seconds
= 31 * TimeConstants::SECONDS_PER_DAY
;
202 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
203 EXPECT_TM_EQ((tm
{0, // sec
207 0, // tm_mon starts with 0 for Jan
208 1970 - TimeConstants::TIME_YEAR_BASE
, // year
214 // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00.
215 seconds
= 59 * TimeConstants::SECONDS_PER_DAY
;
216 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
217 EXPECT_TM_EQ((tm
{0, // sec
221 2, // tm_mon starts with 0 for Jan
222 1970 - TimeConstants::TIME_YEAR_BASE
, // year
228 // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00.
229 seconds
= ((2 * TimeConstants::DAYS_PER_NON_LEAP_YEAR
) + 60) *
230 TimeConstants::SECONDS_PER_DAY
;
231 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
232 EXPECT_TM_EQ((tm
{0, // sec
236 2, // tm_mon starts with 0 for Jan
237 1972 - TimeConstants::TIME_YEAR_BASE
, // year
244 TEST(LlvmLibcGmTime
, EndOf32BitEpochYear
) {
245 // Test for maximum value of a signed 32-bit integer.
246 // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
247 time_t seconds
= 0x7FFFFFFF;
248 struct tm
*tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
249 EXPECT_TM_EQ((tm
{7, // sec
253 0, // tm_mon starts with 0 for Jan
254 2038 - TimeConstants::TIME_YEAR_BASE
, // year
261 TEST(LlvmLibcGmTime
, Max64BitYear
) {
262 if (sizeof(time_t) == 4)
264 // Mon Jan 1 12:50:50 2170 (200 years from 1970),
265 time_t seconds
= 6311479850;
266 struct tm
*tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
267 EXPECT_TM_EQ((tm
{50, // sec
271 0, // tm_mon starts with 0 for Jan
272 2170 - TimeConstants::TIME_YEAR_BASE
, // year
278 // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
279 seconds
= 67767976202043050;
280 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
281 EXPECT_TM_EQ((tm
{50, // sec
285 0, // tm_mon starts with 0 for Jan
286 2147483647 - TimeConstants::TIME_YEAR_BASE
, // year