| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //===-- Collection of utils for mktime and friends --------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC_TIME_TIME_UTILS_H | ||
| #define LLVM_LIBC_SRC_TIME_TIME_UTILS_H | ||
|
|
||
| #include "include/errno.h" | ||
|
|
||
| #include "src/errno/llvmlibc_errno.h" | ||
| #include "src/time/mktime.h" | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| namespace __llvm_libc { | ||
| namespace time_utils { | ||
|
|
||
| struct TimeConstants { | ||
| static constexpr int SecondsPerMin = 60; | ||
| static constexpr int SecondsPerHour = 3600; | ||
| static constexpr int SecondsPerDay = 86400; | ||
| static constexpr int DaysPerWeek = 7; | ||
| static constexpr int MonthsPerYear = 12; | ||
| static constexpr int DaysPerNonLeapYear = 365; | ||
| static constexpr int DaysPerLeapYear = 366; | ||
| static constexpr int TimeYearBase = 1900; | ||
| static constexpr int EpochYear = 1970; | ||
| static constexpr int EpochWeekDay = 4; | ||
| static constexpr int NumberOfSecondsInLeapYear = | ||
| (DaysPerNonLeapYear + 1) * SecondsPerDay; | ||
|
|
||
| /* 2000-03-01 (mod 400 year, immediately after feb29 */ | ||
| static constexpr int64_t SecondsUntil2000MarchFirst = | ||
| (946684800LL + SecondsPerDay * (31 + 29)); | ||
| static constexpr int WeekDayOf2000MarchFirst = 3; | ||
|
|
||
| static constexpr int DaysPer400Years = | ||
| (DaysPerNonLeapYear * 400 + (400 / 4) - 3); | ||
| static constexpr int DaysPer100Years = | ||
| (DaysPerNonLeapYear * 100 + (100 / 4) - 1); | ||
| static constexpr int DaysPer4Years = (DaysPerNonLeapYear * 4 + 1); | ||
|
|
||
| // The latest time that can be represented in this form is 03:14:07 UTC on | ||
| // Tuesday, 19 January 2038 (corresponding to 2,147,483,647 seconds since the | ||
| // start of the epoch). This means that systems using a 32-bit time_t type are | ||
| // susceptible to the Year 2038 problem. | ||
| static constexpr int EndOf32BitEpochYear = 2038; | ||
|
|
||
| static constexpr int NonLeapYearDaysInMonth[] = {31, 28, 31, 30, 31, 30, 30, | ||
| 31, 31, 30, 31, 30, 31}; | ||
|
|
||
| static constexpr time_t OutOfRangeReturnValue = -1; | ||
| }; | ||
|
|
||
| // POSIX.1-2017 requires this. | ||
| static inline time_t OutOfRange() { | ||
| llvmlibc_errno = EOVERFLOW; | ||
| return static_cast<time_t>(-1); | ||
| } | ||
|
|
||
| } // namespace time_utils | ||
| } // namespace __llvm_libc | ||
|
|
||
| #endif // LLVM_LIBC_SRC_TIME_TIME_UTILS_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ add_libc_unittest( | |
| libc_time_unittests | ||
| SRCS | ||
| mktime_test.cpp | ||
| HDRS | ||
| TmMatcher.h | ||
| DEPENDS | ||
| libc.src.time.mktime | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| //===---- TmMatchers.h ------------------------------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H | ||
| #define LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H | ||
|
|
||
| #include <time.h> | ||
|
|
||
| #include "utils/UnitTest/Test.h" | ||
|
|
||
| namespace __llvm_libc { | ||
| namespace tmmatcher { | ||
| namespace testing { | ||
|
|
||
| class StructTmMatcher : public __llvm_libc::testing::Matcher<::tm> { | ||
| ::tm expected; | ||
| ::tm actual; | ||
|
|
||
| public: | ||
| StructTmMatcher(::tm expectedValue) : expected(expectedValue) {} | ||
|
|
||
| bool match(::tm actualValue) { | ||
| actual = actualValue; | ||
| return (actual.tm_sec == expected.tm_sec || | ||
| actual.tm_min == expected.tm_min || | ||
| actual.tm_hour == expected.tm_hour || | ||
| actual.tm_mday == expected.tm_mday || | ||
| actual.tm_mon == expected.tm_mon || | ||
| actual.tm_year == expected.tm_year || | ||
| actual.tm_wday == expected.tm_wday || | ||
| actual.tm_yday == expected.tm_yday || | ||
| actual.tm_isdst == expected.tm_isdst); | ||
| } | ||
|
|
||
| void describeValue(const char *label, ::tm value, | ||
| __llvm_libc::testutils::StreamWrapper &stream) { | ||
| stream << label; | ||
| stream << " sec: " << value.tm_sec; | ||
| stream << " min: " << value.tm_min; | ||
| stream << " hour: " << value.tm_hour; | ||
| stream << " mday: " << value.tm_mday; | ||
| stream << " mon: " << value.tm_mon; | ||
| stream << " year: " << value.tm_year; | ||
| stream << " wday: " << value.tm_wday; | ||
| stream << " yday: " << value.tm_yday; | ||
| stream << " isdst: " << value.tm_isdst; | ||
| stream << '\n'; | ||
| } | ||
|
|
||
| void explainError(__llvm_libc::testutils::StreamWrapper &stream) override { | ||
| describeValue("Expected tm_struct value: ", expected, stream); | ||
| describeValue(" Actual tm_struct value: ", actual, stream); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace testing | ||
| } // namespace tmmatcher | ||
| } // namespace __llvm_libc | ||
|
|
||
| #define EXPECT_TM_EQ(expected, actual) \ | ||
| EXPECT_THAT((actual), \ | ||
| __llvm_libc::tmmatcher::testing::StructTmMatcher((expected))) | ||
|
|
||
| #endif // LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H |