xref: /llvm-project/libc/test/src/time/gmtime_r_test.cpp (revision f9c2377fb68e5051b3061186c507f7b87db2a8b2)
1 //===-- Unittests for gmtime_r --------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/time/gmtime_r.h"
10 #include "src/time/time_constants.h"
11 #include "test/UnitTest/Test.h"
12 #include "test/src/time/TmMatcher.h"
13 
14 // gmtime and gmtime_r share the same code and thus didn't repeat all the tests
15 // from gmtime. Added couple of validation tests.
16 TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) {
17   // Test for maximum value of a signed 32-bit integer.
18   // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC.
19   time_t seconds = 0x7FFFFFFF;
20   struct tm tm_data;
21   struct tm *tm_data_ptr;
22   tm_data_ptr = LIBC_NAMESPACE::gmtime_r(&seconds, &tm_data);
23   EXPECT_TM_EQ(
24       (tm{7,  // sec
25           14, // min
26           3,  // hr
27           19, // day
28           0,  // tm_mon starts with 0 for Jan
29           2038 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year
30           2,                                                     // wday
31           7,                                                     // yday
32           0}),
33       *tm_data_ptr);
34   EXPECT_TM_EQ(*tm_data_ptr, tm_data);
35 }
36 
37 TEST(LlvmLibcGmTimeR, Max64BitYear) {
38   if (sizeof(time_t) == 4)
39     return;
40   // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year.
41   time_t seconds = 67767976202043050;
42   struct tm tm_data;
43   struct tm *tm_data_ptr;
44   tm_data_ptr = LIBC_NAMESPACE::gmtime_r(&seconds, &tm_data);
45   EXPECT_TM_EQ(
46       (tm{50, // sec
47           50, // min
48           12, // hr
49           1,  // day
50           0,  // tm_mon starts with 0 for Jan
51           2147483647 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year
52           2,                                                           // wday
53           50,                                                          // yday
54           0}),
55       *tm_data_ptr);
56   EXPECT_TM_EQ(*tm_data_ptr, tm_data);
57 }
58