1 //===-- Unittests for gmtime_r --------------------------------------------===//
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/time/gmtime_r.h"
10 #include "src/time/time_utils.h"
11 #include "test/UnitTest/Test.h"
12 #include "test/src/time/TmMatcher.h"
14 using LIBC_NAMESPACE::time_utils::TimeConstants
;
16 // gmtime and gmtime_r share the same code and thus didn't repeat all the tests
17 // from gmtime. Added couple of validation tests.
18 TEST(LlvmLibcGmTimeR
, EndOf32BitEpochYear
) {
19 // Test for maximum value of a signed 32-bit integer.
20 // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
21 time_t seconds
= 0x7FFFFFFF;
23 struct tm
*tm_data_ptr
;
24 tm_data_ptr
= LIBC_NAMESPACE::gmtime_r(&seconds
, &tm_data
);
25 EXPECT_TM_EQ((tm
{7, // sec
29 0, // tm_mon starts with 0 for Jan
30 2038 - TimeConstants::TIME_YEAR_BASE
, // year
35 EXPECT_TM_EQ(*tm_data_ptr
, tm_data
);
38 TEST(LlvmLibcGmTimeR
, Max64BitYear
) {
39 if (sizeof(time_t) == 4)
41 // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
42 time_t seconds
= 67767976202043050;
44 struct tm
*tm_data_ptr
;
45 tm_data_ptr
= LIBC_NAMESPACE::gmtime_r(&seconds
, &tm_data
);
46 EXPECT_TM_EQ((tm
{50, // sec
50 0, // tm_mon starts with 0 for Jan
51 2147483647 - TimeConstants::TIME_YEAR_BASE
, // year
56 EXPECT_TM_EQ(*tm_data_ptr
, tm_data
);