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 "hdr/types/struct_tm.h"
10 #include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN
11 #include "src/errno/libc_errno.h"
12 #include "src/time/gmtime.h"
13 #include "src/time/time_constants.h"
14 #include "test/UnitTest/ErrnoSetterMatcher.h"
15 #include "test/UnitTest/Test.h"
16 #include "test/src/time/TmMatcher.h"
18 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails
;
19 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds
;
21 TEST(LlvmLibcGmTime
, OutOfRange
) {
22 if (sizeof(time_t) < sizeof(int64_t))
28 LIBC_NAMESPACE::time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR
);
29 struct tm
*tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
30 EXPECT_TRUE(tm_data
== nullptr);
31 ASSERT_ERRNO_EQ(EOVERFLOW
);
33 LIBC_NAMESPACE::libc_errno
= 0;
37 LIBC_NAMESPACE::time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR
) -
39 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
40 EXPECT_TRUE(tm_data
== nullptr);
41 ASSERT_ERRNO_EQ(EOVERFLOW
);
44 TEST(LlvmLibcGmTime
, InvalidSeconds
) {
46 struct tm
*tm_data
= nullptr;
47 // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59.
49 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
55 12 - 1, // tm_mon starts with 0 for Jan
56 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
61 // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00.
63 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
69 0, // tm_mon starts with 0 for Jan
70 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
77 TEST(LlvmLibcGmTime
, InvalidMinutes
) {
79 struct tm
*tm_data
= nullptr;
80 // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00.
81 seconds
= -LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN
;
82 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
88 11, // tm_mon starts with 0 for Jan
89 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
94 // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00.
95 seconds
= 60 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN
;
96 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
102 0, // tm_mon starts with 0 for Jan
103 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
110 TEST(LlvmLibcGmTime
, InvalidHours
) {
112 struct tm
*tm_data
= nullptr;
113 // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00.
114 seconds
= -LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR
;
115 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
121 11, // tm_mon starts with 0 for Jan
122 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
127 // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00.
128 seconds
= 24 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR
;
129 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
135 0, // tm_mon starts with 0 for Jan
136 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
143 TEST(LlvmLibcGmTime
, InvalidYear
) {
144 // -1 year from 1970-01-01 00:00:00 returns 1969-01-01 00:00:00.
145 time_t seconds
= -LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR
*
146 LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY
;
147 struct tm
*tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
153 0, // tm_mon starts with 0 for Jan
154 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
161 TEST(LlvmLibcGmTime
, InvalidMonths
) {
163 struct tm
*tm_data
= nullptr;
164 // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00.
165 seconds
= -31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY
;
166 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
172 12 - 1, // tm_mon starts with 0 for Jan
173 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
178 // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00.
179 seconds
= LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR
*
180 LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY
;
181 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
187 0, // tm_mon starts with 0 for Jan
188 1971 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
195 TEST(LlvmLibcGmTime
, InvalidDays
) {
197 struct tm
*tm_data
= nullptr;
198 // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00.
199 seconds
= -1 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY
;
200 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
206 11, // tm_mon starts with 0 for Jan
207 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
213 // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00.
214 seconds
= 31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY
;
215 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
221 0, // tm_mon starts with 0 for Jan
222 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
228 // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00.
229 seconds
= 59 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY
;
230 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
236 2, // tm_mon starts with 0 for Jan
237 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
243 // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00.
245 ((2 * LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR
) + 60) *
246 LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY
;
247 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
253 2, // tm_mon starts with 0 for Jan
254 1972 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
261 TEST(LlvmLibcGmTime
, EndOf32BitEpochYear
) {
262 // Test for maximum value of a signed 32-bit integer.
263 // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
264 time_t seconds
= 0x7FFFFFFF;
265 struct tm
*tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
271 0, // tm_mon starts with 0 for Jan
272 2038 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
279 TEST(LlvmLibcGmTime
, Max64BitYear
) {
280 if (sizeof(time_t) == 4)
282 // Mon Jan 1 12:50:50 2170 (200 years from 1970),
283 time_t seconds
= 6311479850;
284 struct tm
*tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
290 0, // tm_mon starts with 0 for Jan
291 2170 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year
297 // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
298 seconds
= 67767976202043050;
299 tm_data
= LIBC_NAMESPACE::gmtime(&seconds
);
305 0, // tm_mon starts with 0 for Jan
306 2147483647 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE
, // year