From f9c2377fb68e5051b3061186c507f7b87db2a8b2 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Wed, 8 Jan 2025 12:28:50 -0800 Subject: [PATCH] [libc][NFC] Cleanup time.h (#122027) While working on strftime I noticed some constants were being defined in unexpected places. One thing led to another, and I ended up doing a major cleanup of the time functions. What's included: All uses of in /src and /test removed (except for LibcTest.cpp) The various time constants have been moved to time_constants.h, and the `time_constants` namespace. struct tm gets its own type indirection header now. --- libc/hdr/types/CMakeLists.txt | 8 + libc/hdr/types/struct_tm.h | 21 + libc/src/pthread/pthread_condattr_init.cpp | 4 +- .../src/pthread/pthread_condattr_setclock.cpp | 6 +- libc/src/time/CMakeLists.txt | 30 ++ libc/src/time/asctime.cpp | 8 +- libc/src/time/asctime.h | 2 +- libc/src/time/asctime_r.cpp | 6 +- libc/src/time/asctime_r.h | 2 +- libc/src/time/ctime.cpp | 11 +- libc/src/time/ctime_r.cpp | 9 +- libc/src/time/difftime.h | 2 +- libc/src/time/gmtime.h | 3 +- libc/src/time/gmtime_r.h | 3 +- libc/src/time/mktime.cpp | 34 +- libc/src/time/mktime.h | 3 +- libc/src/time/time.cpp | 3 +- libc/src/time/time_constants.h | 100 ++++ libc/src/time/time_utils.cpp | 53 ++- libc/src/time/time_utils.h | 93 +--- libc/test/src/time/CMakeLists.txt | 25 +- libc/test/src/time/TmHelper.h | 9 +- libc/test/src/time/TmMatcher.h | 3 +- libc/test/src/time/asctime_r_test.cpp | 8 +- libc/test/src/time/clock_gettime_test.cpp | 5 +- libc/test/src/time/clock_test.cpp | 3 +- libc/test/src/time/ctime_r_test.cpp | 12 +- libc/test/src/time/difftime_test.cpp | 7 +- libc/test/src/time/gettimeofday_test.cpp | 3 +- libc/test/src/time/gmtime_r_test.cpp | 46 +- libc/test/src/time/gmtime_test.cpp | 383 +++++++-------- libc/test/src/time/mktime_test.cpp | 438 +++++++++++------- libc/test/src/time/nanosleep_test.cpp | 3 +- 33 files changed, 790 insertions(+), 556 deletions(-) create mode 100644 libc/hdr/types/struct_tm.h create mode 100644 libc/src/time/time_constants.h diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt index 5156b58ee11af7..1674de14201524 100644 --- a/libc/hdr/types/CMakeLists.txt +++ b/libc/hdr/types/CMakeLists.txt @@ -85,6 +85,14 @@ add_proxy_header_library( libc.include.llvm-libc-types.struct_timespec ) +add_proxy_header_library( + struct_tm + HDRS + struct_tm.h + FULL_BUILD_DEPENDS + libc.include.llvm-libc-types.struct_tm +) + add_proxy_header_library( size_t HDRS diff --git a/libc/hdr/types/struct_tm.h b/libc/hdr/types/struct_tm.h new file mode 100644 index 00000000000000..96c23e2ce054a2 --- /dev/null +++ b/libc/hdr/types/struct_tm.h @@ -0,0 +1,21 @@ +//===-- Proxy for struct tm ----------------------------------------===// +// +// 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_HDR_TYPES_STRUCT_TM_H +#define LLVM_LIBC_HDR_TYPES_STRUCT_TM_H + +#ifdef LIBC_FULL_BUILD + +#include "include/llvm-libc-types/struct_tm.h" + +#else + +#include + +#endif // LIBC_FULL_BUILD + +#endif // LLVM_LIBC_HDR_TYPES_STRUCT_TM_H diff --git a/libc/src/pthread/pthread_condattr_init.cpp b/libc/src/pthread/pthread_condattr_init.cpp index 12005b8a9d30fe..b360804bb7bba3 100644 --- a/libc/src/pthread/pthread_condattr_init.cpp +++ b/libc/src/pthread/pthread_condattr_init.cpp @@ -11,8 +11,8 @@ #include "src/__support/common.h" #include "src/__support/macros/config.h" -#include // pthread_condattr_t, PTHREAD_PROCESS_PRIVATE -#include // CLOCK_REALTIME +#include "hdr/time_macros.h" // CLOCK_REALTIME +#include // pthread_condattr_t, PTHREAD_PROCESS_PRIVATE namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/pthread/pthread_condattr_setclock.cpp b/libc/src/pthread/pthread_condattr_setclock.cpp index 37fbd6b27143dd..5e825d5ecea698 100644 --- a/libc/src/pthread/pthread_condattr_setclock.cpp +++ b/libc/src/pthread/pthread_condattr_setclock.cpp @@ -12,9 +12,9 @@ #include "src/__support/macros/config.h" #include "src/errno/libc_errno.h" -#include // pthread_condattr_t -#include // clockid_t -#include // CLOCK_MONOTONIC, CLOCK_REALTIME +#include "hdr/time_macros.h" // CLOCK_MONOTONIC, CLOCK_REALTIME +#include // pthread_condattr_t +#include // clockid_t namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt index ae835dcc742742..ef9bfe57bc4ec2 100644 --- a/libc/src/time/CMakeLists.txt +++ b/libc/src/time/CMakeLists.txt @@ -2,6 +2,17 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) endif() +add_header_library( + time_constants + HDRS + time_constants.h + DEPENDS + libc.include.time + libc.src.__support.CPP.array + libc.src.__support.CPP.string_view + libc.hdr.types.time_t +) + add_object_library( time_utils SRCS @@ -12,6 +23,10 @@ add_object_library( libc.include.time libc.src.__support.CPP.limits libc.src.errno.errno + .time_constants + libc.hdr.types.time_t + libc.hdr.types.size_t + libc.hdr.types.struct_tm ) add_entrypoint_object( @@ -22,7 +37,9 @@ add_entrypoint_object( asctime.h DEPENDS .time_utils + .time_constants libc.include.time + libc.hdr.types.struct_tm ) add_entrypoint_object( @@ -33,7 +50,9 @@ add_entrypoint_object( asctime_r.h DEPENDS .time_utils + .time_constants libc.include.time + libc.hdr.types.struct_tm ) add_entrypoint_object( @@ -44,6 +63,7 @@ add_entrypoint_object( ctime.h DEPENDS .time_utils + .time_constants libc.hdr.types.time_t libc.include.time ) @@ -56,6 +76,7 @@ add_entrypoint_object( ctime_r.h DEPENDS .time_utils + .time_constants libc.hdr.types.time_t libc.include.time ) @@ -68,6 +89,7 @@ add_entrypoint_object( difftime.h DEPENDS libc.include.time + libc.hdr.types.time_t ) add_entrypoint_object( @@ -79,6 +101,8 @@ add_entrypoint_object( DEPENDS .time_utils libc.include.time + libc.hdr.types.time_t + libc.hdr.types.struct_tm ) add_entrypoint_object( @@ -90,6 +114,8 @@ add_entrypoint_object( DEPENDS .time_utils libc.include.time + libc.hdr.types.time_t + libc.hdr.types.struct_tm ) add_entrypoint_object( @@ -100,8 +126,11 @@ add_entrypoint_object( mktime.h DEPENDS .time_utils + .time_constants libc.include.time libc.src.errno.errno + libc.hdr.types.time_t + libc.hdr.types.struct_tm ) add_entrypoint_object( @@ -115,6 +144,7 @@ add_entrypoint_object( libc.hdr.types.time_t libc.src.__support.time.clock_gettime libc.src.errno.errno + libc.hdr.types.struct_tm ) add_entrypoint_object( diff --git a/libc/src/time/asctime.cpp b/libc/src/time/asctime.cpp index d6fbe7316ced0d..2b00c4136f9065 100644 --- a/libc/src/time/asctime.cpp +++ b/libc/src/time/asctime.cpp @@ -9,15 +9,15 @@ #include "src/time/asctime.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" +#include "src/time/time_constants.h" #include "src/time/time_utils.h" namespace LIBC_NAMESPACE_DECL { -using LIBC_NAMESPACE::time_utils::TimeConstants; - LLVM_LIBC_FUNCTION(char *, asctime, (const struct tm *timeptr)) { - static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; - return time_utils::asctime(timeptr, buffer, TimeConstants::ASCTIME_MAX_BYTES); + static char buffer[time_constants::ASCTIME_BUFFER_SIZE]; + return time_utils::asctime(timeptr, buffer, + time_constants::ASCTIME_MAX_BYTES); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/time/asctime.h b/libc/src/time/asctime.h index 623e6dff60c334..37325e75b829dc 100644 --- a/libc/src/time/asctime.h +++ b/libc/src/time/asctime.h @@ -9,8 +9,8 @@ #ifndef LLVM_LIBC_SRC_TIME_ASCTIME_H #define LLVM_LIBC_SRC_TIME_ASCTIME_H +#include "hdr/types/struct_tm.h" #include "src/__support/macros/config.h" -#include namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/time/asctime_r.cpp b/libc/src/time/asctime_r.cpp index caa22f1cd77833..bf53bfdf2f8c2f 100644 --- a/libc/src/time/asctime_r.cpp +++ b/libc/src/time/asctime_r.cpp @@ -9,15 +9,15 @@ #include "src/time/asctime_r.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" +#include "src/time/time_constants.h" #include "src/time/time_utils.h" namespace LIBC_NAMESPACE_DECL { -using LIBC_NAMESPACE::time_utils::TimeConstants; - LLVM_LIBC_FUNCTION(char *, asctime_r, (const struct tm *timeptr, char *buffer)) { - return time_utils::asctime(timeptr, buffer, TimeConstants::ASCTIME_MAX_BYTES); + return time_utils::asctime(timeptr, buffer, + time_constants::ASCTIME_MAX_BYTES); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/time/asctime_r.h b/libc/src/time/asctime_r.h index 328b7dff19c2e9..65a6b84ca38f60 100644 --- a/libc/src/time/asctime_r.h +++ b/libc/src/time/asctime_r.h @@ -9,8 +9,8 @@ #ifndef LLVM_LIBC_SRC_TIME_ASCTIME_R_H #define LLVM_LIBC_SRC_TIME_ASCTIME_R_H +#include "hdr/types/struct_tm.h" #include "src/__support/macros/config.h" -#include namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/time/ctime.cpp b/libc/src/time/ctime.cpp index 8adae9be73809a..ac0ffe5b32ae56 100644 --- a/libc/src/time/ctime.cpp +++ b/libc/src/time/ctime.cpp @@ -6,23 +6,22 @@ // //===----------------------------------------------------------------------===// -#include "ctime.h" +#include "src/time/ctime.h" #include "src/__support/CPP/limits.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" -#include "time_utils.h" +#include "src/time/time_constants.h" +#include "src/time/time_utils.h" namespace LIBC_NAMESPACE_DECL { -using LIBC_NAMESPACE::time_utils::TimeConstants; - LLVM_LIBC_FUNCTION(char *, ctime, (const time_t *t_ptr)) { if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits::max()) { return nullptr; } - static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + static char buffer[time_constants::ASCTIME_BUFFER_SIZE]; return time_utils::asctime(time_utils::localtime(t_ptr), buffer, - TimeConstants::ASCTIME_MAX_BYTES); + time_constants::ASCTIME_MAX_BYTES); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/time/ctime_r.cpp b/libc/src/time/ctime_r.cpp index 63d93c4085f38c..7224f7742f1394 100644 --- a/libc/src/time/ctime_r.cpp +++ b/libc/src/time/ctime_r.cpp @@ -6,16 +6,15 @@ // //===----------------------------------------------------------------------===// -#include "ctime_r.h" +#include "src/time/ctime_r.h" #include "src/__support/CPP/limits.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" -#include "time_utils.h" +#include "src/time/time_constants.h" +#include "src/time/time_utils.h" namespace LIBC_NAMESPACE_DECL { -using LIBC_NAMESPACE::time_utils::TimeConstants; - LLVM_LIBC_FUNCTION(char *, ctime_r, (const time_t *t_ptr, char *buffer)) { if (t_ptr == nullptr || buffer == nullptr || *t_ptr > cpp::numeric_limits::max()) { @@ -23,7 +22,7 @@ LLVM_LIBC_FUNCTION(char *, ctime_r, (const time_t *t_ptr, char *buffer)) { } return time_utils::asctime(time_utils::localtime(t_ptr), buffer, - TimeConstants::ASCTIME_MAX_BYTES); + time_constants::ASCTIME_MAX_BYTES); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/time/difftime.h b/libc/src/time/difftime.h index d5cd593cc53350..12de5678864c70 100644 --- a/libc/src/time/difftime.h +++ b/libc/src/time/difftime.h @@ -9,8 +9,8 @@ #ifndef LLVM_LIBC_SRC_TIME_DIFFTIME_H #define LLVM_LIBC_SRC_TIME_DIFFTIME_H +#include "hdr/types/time_t.h" #include "src/__support/macros/config.h" -#include namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/time/gmtime.h b/libc/src/time/gmtime.h index 3de3cebbfde2ca..ac7f1be7bbce89 100644 --- a/libc/src/time/gmtime.h +++ b/libc/src/time/gmtime.h @@ -9,8 +9,9 @@ #ifndef LLVM_LIBC_SRC_TIME_GMTIME_H #define LLVM_LIBC_SRC_TIME_GMTIME_H +#include "hdr/types/struct_tm.h" +#include "hdr/types/time_t.h" #include "src/__support/macros/config.h" -#include namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/time/gmtime_r.h b/libc/src/time/gmtime_r.h index b4f387ef443bca..4c88b22faf4cff 100644 --- a/libc/src/time/gmtime_r.h +++ b/libc/src/time/gmtime_r.h @@ -9,8 +9,9 @@ #ifndef LLVM_LIBC_SRC_TIME_GMTIME_R_H #define LLVM_LIBC_SRC_TIME_GMTIME_R_H +#include "hdr/types/struct_tm.h" +#include "hdr/types/time_t.h" #include "src/__support/macros/config.h" -#include namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/time/mktime.cpp b/libc/src/time/mktime.cpp index b5d1da5fa8fba1..3874cad02facbd 100644 --- a/libc/src/time/mktime.cpp +++ b/libc/src/time/mktime.cpp @@ -9,15 +9,11 @@ #include "src/time/mktime.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" +#include "src/time/time_constants.h" #include "src/time/time_utils.h" namespace LIBC_NAMESPACE_DECL { -using LIBC_NAMESPACE::time_utils::TimeConstants; - -static constexpr int NON_LEAP_YEAR_DAYS_IN_MONTH[] = {31, 28, 31, 30, 31, 30, - 31, 31, 30, 31, 30, 31}; - // Returns number of years from (1, year). static constexpr int64_t get_num_of_leap_years_before(int64_t year) { return (year / 4) - (year / 100) + (year / 400); @@ -31,12 +27,12 @@ static constexpr bool is_leap_year(const int64_t year) { LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) { // Unlike most C Library functions, mktime doesn't just die on bad input. // TODO(rtenneti); Handle leap seconds. - int64_t tm_year_from_base = tm_out->tm_year + TimeConstants::TIME_YEAR_BASE; + int64_t tm_year_from_base = tm_out->tm_year + time_constants::TIME_YEAR_BASE; // 32-bit end-of-the-world is 03:14:07 UTC on 19 January 2038. if (sizeof(time_t) == 4 && - tm_year_from_base >= TimeConstants::END_OF32_BIT_EPOCH_YEAR) { - if (tm_year_from_base > TimeConstants::END_OF32_BIT_EPOCH_YEAR) + tm_year_from_base >= time_constants::END_OF32_BIT_EPOCH_YEAR) { + if (tm_year_from_base > time_constants::END_OF32_BIT_EPOCH_YEAR) return time_utils::out_of_range(); if (tm_out->tm_mon > 0) return time_utils::out_of_range(); @@ -64,7 +60,7 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) { // Calculate number of months and years from tm_mon. int64_t month = tm_out->tm_mon; - if (month < 0 || month >= TimeConstants::MONTHS_PER_YEAR - 1) { + if (month < 0 || month >= time_constants::MONTHS_PER_YEAR - 1) { int64_t years = month / 12; month %= 12; if (month < 0) { @@ -78,23 +74,23 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) { // Calculate total number of days based on the month and the day (tm_mday). int64_t total_days = tm_out->tm_mday - 1; for (int64_t i = 0; i < month; ++i) - total_days += NON_LEAP_YEAR_DAYS_IN_MONTH[i]; + total_days += time_constants::NON_LEAP_YEAR_DAYS_IN_MONTH[i]; // Add one day if it is a leap year and the month is after February. if (tm_year_is_leap && month > 1) total_days++; // Calculate total numbers of days based on the year. - total_days += (tm_year_from_base - TimeConstants::EPOCH_YEAR) * - TimeConstants::DAYS_PER_NON_LEAP_YEAR; - if (tm_year_from_base >= TimeConstants::EPOCH_YEAR) { + total_days += (tm_year_from_base - time_constants::EPOCH_YEAR) * + time_constants::DAYS_PER_NON_LEAP_YEAR; + if (tm_year_from_base >= time_constants::EPOCH_YEAR) { total_days += get_num_of_leap_years_before(tm_year_from_base - 1) - - get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR); + get_num_of_leap_years_before(time_constants::EPOCH_YEAR); } else if (tm_year_from_base >= 1) { - total_days -= get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR) - + total_days -= get_num_of_leap_years_before(time_constants::EPOCH_YEAR) - get_num_of_leap_years_before(tm_year_from_base - 1); } else { // Calculate number of leap years until 0th year. - total_days -= get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR) - + total_days -= get_num_of_leap_years_before(time_constants::EPOCH_YEAR) - get_num_of_leap_years_before(0); if (tm_year_from_base <= 0) { total_days -= 1; // Subtract 1 for 0th year. @@ -109,9 +105,9 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) { // TODO: https://github.com/llvm/llvm-project/issues/121962 // Need to handle timezone and update of tm_isdst. int64_t seconds = tm_out->tm_sec + - tm_out->tm_min * TimeConstants::SECONDS_PER_MIN + - tm_out->tm_hour * TimeConstants::SECONDS_PER_HOUR + - total_days * TimeConstants::SECONDS_PER_DAY; + tm_out->tm_min * time_constants::SECONDS_PER_MIN + + tm_out->tm_hour * time_constants::SECONDS_PER_HOUR + + total_days * time_constants::SECONDS_PER_DAY; // Update the tm structure's year, month, day, etc. from seconds. if (time_utils::update_from_seconds(seconds, tm_out) < 0) diff --git a/libc/src/time/mktime.h b/libc/src/time/mktime.h index 2b4c67996555e8..985c6293f9d512 100644 --- a/libc/src/time/mktime.h +++ b/libc/src/time/mktime.h @@ -9,8 +9,9 @@ #ifndef LLVM_LIBC_SRC_TIME_MKTIME_H #define LLVM_LIBC_SRC_TIME_MKTIME_H +#include "hdr/types/struct_tm.h" +#include "hdr/types/time_t.h" #include "src/__support/macros/config.h" -#include namespace LIBC_NAMESPACE_DECL { diff --git a/libc/src/time/time.cpp b/libc/src/time/time.cpp index 4a0b614a68ef5f..860909af7488c7 100644 --- a/libc/src/time/time.cpp +++ b/libc/src/time/time.cpp @@ -6,12 +6,13 @@ // //===----------------------------------------------------------------------===// +#include "src/time/time_func.h" + #include "hdr/time_macros.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" #include "src/__support/time/clock_gettime.h" #include "src/errno/libc_errno.h" -#include "src/time/time_func.h" namespace LIBC_NAMESPACE_DECL { // avoid inconsitent clang-format behavior diff --git a/libc/src/time/time_constants.h b/libc/src/time/time_constants.h new file mode 100644 index 00000000000000..3e25f741745ab5 --- /dev/null +++ b/libc/src/time/time_constants.h @@ -0,0 +1,100 @@ +//===-- Collection of constants for time functions --------------*- 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_CONSTANTS_H +#define LLVM_LIBC_SRC_TIME_TIME_CONSTANTS_H + +#include "hdr/types/time_t.h" +#include "src/__support/CPP/array.h" +#include "src/__support/CPP/string_view.h" +#include + +namespace LIBC_NAMESPACE_DECL { +namespace time_constants { + +enum Month : int { + JANUARY, + FEBRUARY, + MARCH, + APRIL, + MAY, + JUNE, + JULY, + AUGUST, + SEPTEMBER, + OCTOBER, + NOVEMBER, + DECEMBER +}; + +constexpr int SECONDS_PER_MIN = 60; +constexpr int MINUTES_PER_HOUR = 60; +constexpr int HOURS_PER_DAY = 24; +constexpr int DAYS_PER_WEEK = 7; +constexpr int MONTHS_PER_YEAR = 12; +constexpr int DAYS_PER_NON_LEAP_YEAR = 365; +constexpr int DAYS_PER_LEAP_YEAR = 366; + +constexpr int SECONDS_PER_HOUR = SECONDS_PER_MIN * MINUTES_PER_HOUR; +constexpr int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; +constexpr int NUMBER_OF_SECONDS_IN_LEAP_YEAR = + DAYS_PER_LEAP_YEAR * SECONDS_PER_DAY; + +constexpr int TIME_YEAR_BASE = 1900; +constexpr int EPOCH_YEAR = 1970; +constexpr int EPOCH_WEEK_DAY = 4; + +// For asctime the behavior is undefined if struct tm's tm_wday or tm_mon are +// not within the normal ranges as defined in , or if struct tm's +// tm_year exceeds {INT_MAX}-1990, or if the below asctime_internal algorithm +// would attempt to generate more than 26 bytes of output (including the +// terminating null). +constexpr int ASCTIME_BUFFER_SIZE = 256; +constexpr int ASCTIME_MAX_BYTES = 26; + +/* 2000-03-01 (mod 400 year, immediately after feb29 */ +constexpr int64_t SECONDS_UNTIL2000_MARCH_FIRST = + (946684800LL + SECONDS_PER_DAY * (31 + 29)); +constexpr int WEEK_DAY_OF2000_MARCH_FIRST = 3; + +constexpr int DAYS_PER400_YEARS = + (DAYS_PER_NON_LEAP_YEAR * 400) + (400 / 4) - 3; +constexpr int DAYS_PER100_YEARS = + (DAYS_PER_NON_LEAP_YEAR * 100) + (100 / 4) - 1; +constexpr int DAYS_PER4_YEARS = (DAYS_PER_NON_LEAP_YEAR * 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. +constexpr int END_OF32_BIT_EPOCH_YEAR = 2038; + +constexpr time_t OUT_OF_RANGE_RETURN_VALUE = -1; + +constexpr cpp::array WEEK_DAY_NAMES = { + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + +constexpr cpp::array WEEK_DAY_FULL_NAMES = { + "Sunday", "Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday"}; + +constexpr cpp::array MONTH_NAMES = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + +constexpr cpp::array MONTH_FULL_NAMES = { + "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December"}; + +constexpr int NON_LEAP_YEAR_DAYS_IN_MONTH[] = {31, 28, 31, 30, 31, 30, + 31, 31, 30, 31, 30, 31}; + +} // namespace time_constants +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_TIME_TIME_CONSTANTS_H diff --git a/libc/src/time/time_utils.cpp b/libc/src/time/time_utils.cpp index 509cad8146df87..abc93b8cb961ed 100644 --- a/libc/src/time/time_utils.cpp +++ b/libc/src/time/time_utils.cpp @@ -10,12 +10,11 @@ #include "src/__support/CPP/limits.h" // INT_MIN, INT_MAX #include "src/__support/common.h" #include "src/__support/macros/config.h" +#include "src/time/time_constants.h" namespace LIBC_NAMESPACE_DECL { namespace time_utils { -using LIBC_NAMESPACE::time_utils::TimeConstants; - static int64_t computeRemainingYears(int64_t daysPerYears, int64_t quotientYears, int64_t *remainingDays) { @@ -52,36 +51,36 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) { (sizeof(time_t) == 4) ? INT_MIN : INT_MIN * static_cast( - TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); + time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); constexpr time_t time_max = (sizeof(time_t) == 4) ? INT_MAX : INT_MAX * static_cast( - TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); + time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); time_t ts = static_cast(total_seconds); if (ts < time_min || ts > time_max) return time_utils::out_of_range(); int64_t seconds = - total_seconds - TimeConstants::SECONDS_UNTIL2000_MARCH_FIRST; - int64_t days = seconds / TimeConstants::SECONDS_PER_DAY; - int64_t remainingSeconds = seconds % TimeConstants::SECONDS_PER_DAY; + total_seconds - time_constants::SECONDS_UNTIL2000_MARCH_FIRST; + int64_t days = seconds / time_constants::SECONDS_PER_DAY; + int64_t remainingSeconds = seconds % time_constants::SECONDS_PER_DAY; if (remainingSeconds < 0) { - remainingSeconds += TimeConstants::SECONDS_PER_DAY; + remainingSeconds += time_constants::SECONDS_PER_DAY; days--; } - int64_t wday = (TimeConstants::WEEK_DAY_OF2000_MARCH_FIRST + days) % - TimeConstants::DAYS_PER_WEEK; + int64_t wday = (time_constants::WEEK_DAY_OF2000_MARCH_FIRST + days) % + time_constants::DAYS_PER_WEEK; if (wday < 0) - wday += TimeConstants::DAYS_PER_WEEK; + wday += time_constants::DAYS_PER_WEEK; // Compute the number of 400 year cycles. - int64_t numOfFourHundredYearCycles = days / TimeConstants::DAYS_PER400_YEARS; - int64_t remainingDays = days % TimeConstants::DAYS_PER400_YEARS; + int64_t numOfFourHundredYearCycles = days / time_constants::DAYS_PER400_YEARS; + int64_t remainingDays = days % time_constants::DAYS_PER400_YEARS; if (remainingDays < 0) { - remainingDays += TimeConstants::DAYS_PER400_YEARS; + remainingDays += time_constants::DAYS_PER400_YEARS; numOfFourHundredYearCycles--; } @@ -89,17 +88,17 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) { // "four hundred year cycles" will be 4 hundred year cycles or less in 400 // years. int64_t numOfHundredYearCycles = computeRemainingYears( - TimeConstants::DAYS_PER100_YEARS, 4, &remainingDays); + time_constants::DAYS_PER100_YEARS, 4, &remainingDays); // The remaining number of years after computing the number of // "hundred year cycles" will be 25 four year cycles or less in 100 years. - int64_t numOfFourYearCycles = - computeRemainingYears(TimeConstants::DAYS_PER4_YEARS, 25, &remainingDays); + int64_t numOfFourYearCycles = computeRemainingYears( + time_constants::DAYS_PER4_YEARS, 25, &remainingDays); // The remaining number of years after computing the number of // "four year cycles" will be 4 one year cycles or less in 4 years. int64_t remainingYears = computeRemainingYears( - TimeConstants::DAYS_PER_NON_LEAP_YEAR, 4, &remainingDays); + time_constants::DAYS_PER_NON_LEAP_YEAR, 4, &remainingDays); // Calculate number of years from year 2000. int64_t years = remainingYears + 4 * numOfFourYearCycles + @@ -112,8 +111,8 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) { // We add 31 and 28 for the number of days in January and February, since our // starting point was March 1st. int64_t yday = remainingDays + 31 + 28 + leapDay; - if (yday >= TimeConstants::DAYS_PER_NON_LEAP_YEAR + leapDay) - yday -= TimeConstants::DAYS_PER_NON_LEAP_YEAR + leapDay; + if (yday >= time_constants::DAYS_PER_NON_LEAP_YEAR + leapDay) + yday -= time_constants::DAYS_PER_NON_LEAP_YEAR + leapDay; int64_t months = 0; while (daysInMonth[months] <= remainingDays) { @@ -121,8 +120,8 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) { months++; } - if (months >= TimeConstants::MONTHS_PER_YEAR - 2) { - months -= TimeConstants::MONTHS_PER_YEAR; + if (months >= time_constants::MONTHS_PER_YEAR - 2) { + months -= time_constants::MONTHS_PER_YEAR; years++; } @@ -131,19 +130,19 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) { // All the data (years, month and remaining days) was calculated from // March, 2000. Thus adjust the data to be from January, 1900. - tm->tm_year = static_cast(years + 2000 - TimeConstants::TIME_YEAR_BASE); + tm->tm_year = static_cast(years + 2000 - time_constants::TIME_YEAR_BASE); tm->tm_mon = static_cast(months + 2); tm->tm_mday = static_cast(remainingDays + 1); tm->tm_wday = static_cast(wday); tm->tm_yday = static_cast(yday); tm->tm_hour = - static_cast(remainingSeconds / TimeConstants::SECONDS_PER_HOUR); + static_cast(remainingSeconds / time_constants::SECONDS_PER_HOUR); tm->tm_min = - static_cast(remainingSeconds / TimeConstants::SECONDS_PER_MIN % - TimeConstants::SECONDS_PER_MIN); + static_cast(remainingSeconds / time_constants::SECONDS_PER_MIN % + time_constants::SECONDS_PER_MIN); tm->tm_sec = - static_cast(remainingSeconds % TimeConstants::SECONDS_PER_MIN); + static_cast(remainingSeconds % time_constants::SECONDS_PER_MIN); // TODO(rtenneti): Need to handle timezone and update of tm_isdst. tm->tm_isdst = 0; diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h index 552ea925c1c7dc..5e0a692d4db048 100644 --- a/libc/src/time/time_utils.h +++ b/libc/src/time/time_utils.h @@ -9,79 +9,19 @@ #ifndef LLVM_LIBC_SRC_TIME_TIME_UTILS_H #define LLVM_LIBC_SRC_TIME_TIME_UTILS_H -#include // For size_t. - +#include "hdr/types/size_t.h" +#include "hdr/types/struct_tm.h" +#include "hdr/types/time_t.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" #include "src/errno/libc_errno.h" -#include "src/time/mktime.h" +#include "time_constants.h" #include namespace LIBC_NAMESPACE_DECL { namespace time_utils { -enum Month : int { - JANUARY, - FEBRUARY, - MARCH, - APRIL, - MAY, - JUNE, - JULY, - AUGUST, - SEPTEMBER, - OCTOBER, - NOVEMBER, - DECEMBER -}; - -struct TimeConstants { - static constexpr int SECONDS_PER_MIN = 60; - static constexpr int MINUTES_PER_HOUR = 60; - static constexpr int HOURS_PER_DAY = 24; - static constexpr int DAYS_PER_WEEK = 7; - static constexpr int MONTHS_PER_YEAR = 12; - static constexpr int DAYS_PER_NON_LEAP_YEAR = 365; - static constexpr int DAYS_PER_LEAP_YEAR = 366; - - static constexpr int SECONDS_PER_HOUR = SECONDS_PER_MIN * MINUTES_PER_HOUR; - static constexpr int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; - static constexpr int NUMBER_OF_SECONDS_IN_LEAP_YEAR = - DAYS_PER_LEAP_YEAR * SECONDS_PER_DAY; - - static constexpr int TIME_YEAR_BASE = 1900; - static constexpr int EPOCH_YEAR = 1970; - static constexpr int EPOCH_WEEK_DAY = 4; - - // For asctime the behavior is undefined if struct tm's tm_wday or tm_mon are - // not within the normal ranges as defined in , or if struct tm's - // tm_year exceeds {INT_MAX}-1990, or if the below asctime_internal algorithm - // would attempt to generate more than 26 bytes of output (including the - // terminating null). - static constexpr int ASCTIME_BUFFER_SIZE = 256; - static constexpr int ASCTIME_MAX_BYTES = 26; - - /* 2000-03-01 (mod 400 year, immediately after feb29 */ - static constexpr int64_t SECONDS_UNTIL2000_MARCH_FIRST = - (946684800LL + SECONDS_PER_DAY * (31 + 29)); - static constexpr int WEEK_DAY_OF2000_MARCH_FIRST = 3; - - static constexpr int DAYS_PER400_YEARS = - (DAYS_PER_NON_LEAP_YEAR * 400) + (400 / 4) - 3; - static constexpr int DAYS_PER100_YEARS = - (DAYS_PER_NON_LEAP_YEAR * 100) + (100 / 4) - 1; - static constexpr int DAYS_PER4_YEARS = (DAYS_PER_NON_LEAP_YEAR * 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 END_OF32_BIT_EPOCH_YEAR = 2038; - - static constexpr time_t OUT_OF_RANGE_RETURN_VALUE = -1; -}; - // Update the "tm" structure's year, month, etc. members from seconds. // "total_seconds" is the number of seconds since January 1st, 1970. extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm); @@ -98,7 +38,7 @@ LIBC_INLINE time_t out_of_range() { // require it. libc_errno = EOVERFLOW; #endif - return TimeConstants::OUT_OF_RANGE_RETURN_VALUE; + return time_constants::OUT_OF_RANGE_RETURN_VALUE; } LIBC_INLINE void invalid_value() { libc_errno = EINVAL; } @@ -110,32 +50,23 @@ LIBC_INLINE char *asctime(const struct tm *timeptr, char *buffer, return nullptr; } if (timeptr->tm_wday < 0 || - timeptr->tm_wday > (TimeConstants::DAYS_PER_WEEK - 1)) { + timeptr->tm_wday > (time_constants::DAYS_PER_WEEK - 1)) { invalid_value(); return nullptr; } if (timeptr->tm_mon < 0 || - timeptr->tm_mon > (TimeConstants::MONTHS_PER_YEAR - 1)) { + timeptr->tm_mon > (time_constants::MONTHS_PER_YEAR - 1)) { invalid_value(); return nullptr; } - // TODO(rtenneti): i18n the following strings. - static const char *week_days_name[TimeConstants::DAYS_PER_WEEK] = { - "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; - - static const char *months_name[TimeConstants::MONTHS_PER_YEAR] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - - // TODO(michaelr): look into removing this call to __builtin_snprintf that may - // be emitted as a call to snprintf. Alternatively, look into using our - // internal printf machinery. + // TODO(michaelr): move this to use the strftime machinery int written_size = __builtin_snprintf( buffer, bufferLength, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", - week_days_name[timeptr->tm_wday], months_name[timeptr->tm_mon], - timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, - TimeConstants::TIME_YEAR_BASE + timeptr->tm_year); + time_constants::WEEK_DAY_NAMES[timeptr->tm_wday].data(), + time_constants::MONTH_NAMES[timeptr->tm_mon].data(), timeptr->tm_mday, + timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, + time_constants::TIME_YEAR_BASE + timeptr->tm_year); if (written_size < 0) return nullptr; if (static_cast(written_size) >= bufferLength) { diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt index da3903f3e0e494..12add224f386a8 100644 --- a/libc/test/src/time/CMakeLists.txt +++ b/libc/test/src/time/CMakeLists.txt @@ -13,6 +13,8 @@ add_libc_unittest( 20 DEPENDS libc.src.time.asctime + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_unittest( @@ -28,6 +30,8 @@ add_libc_unittest( 20 DEPENDS libc.src.time.asctime_r + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_unittest( @@ -45,7 +49,8 @@ add_libc_unittest( libc.include.time libc.hdr.types.time_t libc.src.time.ctime - libc.src.time.time_utils + libc.src.time.time_constants + libc.hdr.types.struct_tm ) add_libc_unittest( @@ -63,7 +68,8 @@ add_libc_unittest( libc.include.time libc.hdr.types.time_t libc.src.time.ctime_r - libc.src.time.time_utils + libc.src.time.time_constants + libc.hdr.types.struct_tm ) add_libc_test( @@ -74,6 +80,9 @@ add_libc_test( clock_gettime_test.cpp DEPENDS libc.src.time.clock_gettime + libc.hdr.types.time_t + libc.hdr.types.struct_timespec + libc.hdr.time_macros ) add_libc_test( @@ -94,6 +103,8 @@ add_libc_unittest( difftime_test.cpp DEPENDS libc.src.time.difftime + libc.src.time.time_constants + libc.src.__support.FPUtil.fp_bits ) add_libc_unittest( @@ -105,6 +116,7 @@ add_libc_unittest( DEPENDS libc.include.time libc.src.time.gettimeofday + libc.hdr.types.struct_timeval ) add_libc_unittest( @@ -118,6 +130,8 @@ add_libc_unittest( DEPENDS libc.src.time.gmtime libc.src.__support.CPP.limits + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_unittest( @@ -130,6 +144,8 @@ add_libc_unittest( TmMatcher.h DEPENDS libc.src.time.gmtime_r + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_unittest( @@ -146,6 +162,8 @@ add_libc_unittest( DEPENDS libc.src.time.mktime libc.src.__support.CPP.limits + libc.hdr.types.struct_tm + libc.src.time.time_constants ) add_libc_test( @@ -158,6 +176,7 @@ add_libc_test( libc.include.time libc.src.time.nanosleep libc.src.errno.errno + libc.hdr.types.struct_timespec ) add_libc_unittest( @@ -180,6 +199,7 @@ add_libc_test( timespec_get_test.cpp DEPENDS libc.src.time.timespec_get + libc.hdr.types.struct_timespec ) add_libc_test( @@ -192,4 +212,5 @@ add_libc_test( libc.include.time libc.src.time.clock libc.src.errno.errno + libc.hdr.types.clock_t ) diff --git a/libc/test/src/time/TmHelper.h b/libc/test/src/time/TmHelper.h index 5ae258461099b1..1582839ffaf2b1 100644 --- a/libc/test/src/time/TmHelper.h +++ b/libc/test/src/time/TmHelper.h @@ -9,12 +9,9 @@ #ifndef LLVM_LIBC_TEST_SRC_TIME_TMHELPER_H #define LLVM_LIBC_TEST_SRC_TIME_TMHELPER_H -#include - +#include "hdr/types/struct_tm.h" #include "src/__support/macros/config.h" -#include "src/time/time_utils.h" - -using LIBC_NAMESPACE::time_utils::TimeConstants; +#include "src/time/time_constants.h" namespace LIBC_NAMESPACE_DECL { namespace tmhelper { @@ -30,7 +27,7 @@ static inline void initialize_tm_data(struct tm *tm_data, int year, int month, .tm_mday = mday, .tm_mon = month - 1, // tm_mon starts with 0 for Jan // years since 1900 - .tm_year = year - TimeConstants::TIME_YEAR_BASE, + .tm_year = year - time_constants::TIME_YEAR_BASE, .tm_wday = wday, .tm_yday = yday, .tm_isdst = 0}; diff --git a/libc/test/src/time/TmMatcher.h b/libc/test/src/time/TmMatcher.h index 630956b0f08d66..d39ee396057b83 100644 --- a/libc/test/src/time/TmMatcher.h +++ b/libc/test/src/time/TmMatcher.h @@ -9,8 +9,7 @@ #ifndef LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H #define LLVM_LIBC_TEST_SRC_TIME_TM_MATCHER_H -#include - +#include "hdr/types/struct_tm.h" #include "src/__support/macros/config.h" #include "test/UnitTest/Test.h" diff --git a/libc/test/src/time/asctime_r_test.cpp b/libc/test/src/time/asctime_r_test.cpp index f3aadbb39de4d0..b595cfe0248667 100644 --- a/libc/test/src/time/asctime_r_test.cpp +++ b/libc/test/src/time/asctime_r_test.cpp @@ -8,12 +8,10 @@ #include "src/errno/libc_errno.h" #include "src/time/asctime_r.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmHelper.h" -using LIBC_NAMESPACE::time_utils::TimeConstants; - static inline char *call_asctime_r(struct tm *tm_data, int year, int month, int mday, int hour, int min, int sec, int wday, int yday, char *buffer) { @@ -30,7 +28,7 @@ TEST(LlvmLibcAsctimeR, Nullptr) { ASSERT_ERRNO_EQ(EINVAL); ASSERT_STREQ(nullptr, result); - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; result = LIBC_NAMESPACE::asctime_r(nullptr, buffer); ASSERT_ERRNO_EQ(EINVAL); ASSERT_STREQ(nullptr, result); @@ -42,7 +40,7 @@ TEST(LlvmLibcAsctimeR, Nullptr) { } TEST(LlvmLibcAsctimeR, ValidDate) { - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; struct tm tm_data; char *result; // 1970-01-01 00:00:00. Test with a valid buffer size. diff --git a/libc/test/src/time/clock_gettime_test.cpp b/libc/test/src/time/clock_gettime_test.cpp index 43715c0265f1f8..d3edcae00cdd01 100644 --- a/libc/test/src/time/clock_gettime_test.cpp +++ b/libc/test/src/time/clock_gettime_test.cpp @@ -6,12 +6,13 @@ // //===----------------------------------------------------------------------===// +#include "hdr/time_macros.h" +#include "hdr/types/struct_timespec.h" +#include "hdr/types/time_t.h" #include "src/__support/macros/properties/architectures.h" #include "src/time/clock_gettime.h" #include "test/UnitTest/Test.h" -#include - TEST(LlvmLibcClockGetTime, RealTime) { timespec tp; int result; diff --git a/libc/test/src/time/clock_test.cpp b/libc/test/src/time/clock_test.cpp index 05082aa2338852..8d8d89d577a987 100644 --- a/libc/test/src/time/clock_test.cpp +++ b/libc/test/src/time/clock_test.cpp @@ -6,11 +6,10 @@ // //===----------------------------------------------------------------------===// +#include "hdr/types/clock_t.h" #include "src/time/clock.h" #include "test/UnitTest/Test.h" -#include - TEST(LlvmLibcClockTest, SmokeTest) { clock_t c1 = LIBC_NAMESPACE::clock(); ASSERT_GT(c1, clock_t(0)); diff --git a/libc/test/src/time/ctime_r_test.cpp b/libc/test/src/time/ctime_r_test.cpp index 9ce6f75f754849..27011b7e0fbd67 100644 --- a/libc/test/src/time/ctime_r_test.cpp +++ b/libc/test/src/time/ctime_r_test.cpp @@ -8,18 +8,16 @@ #include "src/errno/libc_errno.h" #include "src/time/ctime_r.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmHelper.h" -using LIBC_NAMESPACE::time_utils::TimeConstants; - TEST(LlvmLibcCtimeR, Nullptr) { char *result; result = LIBC_NAMESPACE::ctime_r(nullptr, nullptr); ASSERT_STREQ(nullptr, result); - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; result = LIBC_NAMESPACE::ctime_r(nullptr, buffer); ASSERT_STREQ(nullptr, result); @@ -29,7 +27,7 @@ TEST(LlvmLibcCtimeR, Nullptr) { } TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) { - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; time_t t; char *result; // 1970-01-01 00:00:00. Test with a valid buffer size. @@ -39,7 +37,7 @@ TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) { } TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) { - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; time_t t; char *result; // 2038-01-19 03:14:07. Test with a valid buffer size. @@ -49,7 +47,7 @@ TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) { } TEST(LlvmLibcCtimeR, InvalidArgument) { - char buffer[TimeConstants::ASCTIME_BUFFER_SIZE]; + char buffer[LIBC_NAMESPACE::time_constants::ASCTIME_BUFFER_SIZE]; time_t t; char *result; t = 2147483648; diff --git a/libc/test/src/time/difftime_test.cpp b/libc/test/src/time/difftime_test.cpp index 68ff4630e61baf..4dab1ac91104ff 100644 --- a/libc/test/src/time/difftime_test.cpp +++ b/libc/test/src/time/difftime_test.cpp @@ -8,15 +8,12 @@ #include "src/__support/FPUtil/FPBits.h" #include "src/time/difftime.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/ErrnoSetterMatcher.h" #include "test/UnitTest/Test.h" -using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; -using LIBC_NAMESPACE::time_utils::TimeConstants; - TEST(LlvmLibcDifftime, SmokeTest) { - time_t t1_seconds = TimeConstants::SECONDS_PER_HOUR; + time_t t1_seconds = LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR; time_t t2_seconds = 0; LIBC_NAMESPACE::fputil::FPBits expected_fp = diff --git a/libc/test/src/time/gettimeofday_test.cpp b/libc/test/src/time/gettimeofday_test.cpp index ee934b7f3a2010..8f9f136164f574 100644 --- a/libc/test/src/time/gettimeofday_test.cpp +++ b/libc/test/src/time/gettimeofday_test.cpp @@ -6,8 +6,7 @@ // //===----------------------------------------------------------------------===// -#include - +#include "hdr/types/struct_timeval.h" #include "src/time/gettimeofday.h" #include "test/UnitTest/Test.h" diff --git a/libc/test/src/time/gmtime_r_test.cpp b/libc/test/src/time/gmtime_r_test.cpp index 2276b4803f1922..9d466f444f97ff 100644 --- a/libc/test/src/time/gmtime_r_test.cpp +++ b/libc/test/src/time/gmtime_r_test.cpp @@ -7,12 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/time/gmtime_r.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmMatcher.h" -using LIBC_NAMESPACE::time_utils::TimeConstants; - // gmtime and gmtime_r share the same code and thus didn't repeat all the tests // from gmtime. Added couple of validation tests. TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) { @@ -22,16 +20,17 @@ TEST(LlvmLibcGmTimeR, EndOf32BitEpochYear) { struct tm tm_data; struct tm *tm_data_ptr; tm_data_ptr = LIBC_NAMESPACE::gmtime_r(&seconds, &tm_data); - EXPECT_TM_EQ((tm{7, // sec - 14, // min - 3, // hr - 19, // day - 0, // tm_mon starts with 0 for Jan - 2038 - TimeConstants::TIME_YEAR_BASE, // year - 2, // wday - 7, // yday - 0}), - *tm_data_ptr); + EXPECT_TM_EQ( + (tm{7, // sec + 14, // min + 3, // hr + 19, // day + 0, // tm_mon starts with 0 for Jan + 2038 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 2, // wday + 7, // yday + 0}), + *tm_data_ptr); EXPECT_TM_EQ(*tm_data_ptr, tm_data); } @@ -43,15 +42,16 @@ TEST(LlvmLibcGmTimeR, Max64BitYear) { struct tm tm_data; struct tm *tm_data_ptr; tm_data_ptr = LIBC_NAMESPACE::gmtime_r(&seconds, &tm_data); - EXPECT_TM_EQ((tm{50, // sec - 50, // min - 12, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 2147483647 - TimeConstants::TIME_YEAR_BASE, // year - 2, // wday - 50, // yday - 0}), - *tm_data_ptr); + EXPECT_TM_EQ( + (tm{50, // sec + 50, // min + 12, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 2147483647 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 2, // wday + 50, // yday + 0}), + *tm_data_ptr); EXPECT_TM_EQ(*tm_data_ptr, tm_data); } diff --git a/libc/test/src/time/gmtime_test.cpp b/libc/test/src/time/gmtime_test.cpp index 433fbf6667057f..6af5a18d369961 100644 --- a/libc/test/src/time/gmtime_test.cpp +++ b/libc/test/src/time/gmtime_test.cpp @@ -6,32 +6,36 @@ // //===----------------------------------------------------------------------===// +#include "hdr/types/struct_tm.h" #include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN #include "src/errno/libc_errno.h" #include "src/time/gmtime.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/ErrnoSetterMatcher.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmMatcher.h" using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; -using LIBC_NAMESPACE::time_utils::TimeConstants; TEST(LlvmLibcGmTime, OutOfRange) { if (sizeof(time_t) < sizeof(int64_t)) return; time_t seconds = - 1 + INT_MAX * static_cast( - TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); + 1 + + INT_MAX * + static_cast( + LIBC_NAMESPACE::time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds); EXPECT_TRUE(tm_data == nullptr); ASSERT_ERRNO_EQ(EOVERFLOW); LIBC_NAMESPACE::libc_errno = 0; - seconds = INT_MIN * static_cast( - TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR) - - 1; + seconds = + INT_MIN * + static_cast( + LIBC_NAMESPACE::time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR) - + 1; tm_data = LIBC_NAMESPACE::gmtime(&seconds); EXPECT_TRUE(tm_data == nullptr); ASSERT_ERRNO_EQ(EOVERFLOW); @@ -43,201 +47,215 @@ TEST(LlvmLibcGmTime, InvalidSeconds) { // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59. seconds = -1; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{59, // sec - 59, // min - 23, // hr - 31, // day - 12 - 1, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 364, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{59, // sec + 59, // min + 23, // hr + 31, // day + 12 - 1, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 364, // yday + 0}), + *tm_data); // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00. seconds = 60; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 1, // min - 0, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 4, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 1, // min + 0, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 4, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidMinutes) { time_t seconds = 0; struct tm *tm_data = nullptr; // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00. - seconds = -TimeConstants::SECONDS_PER_MIN; + seconds = -LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 59, // min - 23, // hr - 31, // day - 11, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 59, // min + 23, // hr + 31, // day + 11, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00. - seconds = 60 * TimeConstants::SECONDS_PER_MIN; + seconds = 60 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 1, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 4, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 1, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 4, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidHours) { time_t seconds = 0; struct tm *tm_data = nullptr; // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00. - seconds = -TimeConstants::SECONDS_PER_HOUR; + seconds = -LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 23, // hr - 31, // day - 11, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 23, // hr + 31, // day + 11, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00. - seconds = 24 * TimeConstants::SECONDS_PER_HOUR; + seconds = 24 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 2, // day - 0, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 5, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 2, // day + 0, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 5, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidYear) { // -1 year from 1970-01-01 00:00:00 returns 1969-01-01 00:00:00. - time_t seconds = - -TimeConstants::DAYS_PER_NON_LEAP_YEAR * TimeConstants::SECONDS_PER_DAY; + time_t seconds = -LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidMonths) { time_t seconds = 0; struct tm *tm_data = nullptr; // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00. - seconds = -31 * TimeConstants::SECONDS_PER_DAY; + seconds = -31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 12 - 1, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 1, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 12 - 1, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 1, // wday + 0, // yday + 0}), + *tm_data); // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00. - seconds = - TimeConstants::DAYS_PER_NON_LEAP_YEAR * TimeConstants::SECONDS_PER_DAY; + seconds = LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1971 - TimeConstants::TIME_YEAR_BASE, // year - 5, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1971 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 5, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, InvalidDays) { time_t seconds = 0; struct tm *tm_data = nullptr; // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00. - seconds = -1 * TimeConstants::SECONDS_PER_DAY; + seconds = -1 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 31, // day - 11, // tm_mon starts with 0 for Jan - 1969 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 31, // day + 11, // tm_mon starts with 0 for Jan + 1969 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00. - seconds = 31 * TimeConstants::SECONDS_PER_DAY; + seconds = 31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 0, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 0, // wday + 0, // yday + 0}), + *tm_data); // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00. - seconds = 59 * TimeConstants::SECONDS_PER_DAY; + seconds = 59 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 2, // tm_mon starts with 0 for Jan - 1970 - TimeConstants::TIME_YEAR_BASE, // year - 0, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 2, // tm_mon starts with 0 for Jan + 1970 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 0, // wday + 0, // yday + 0}), + *tm_data); // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00. - seconds = ((2 * TimeConstants::DAYS_PER_NON_LEAP_YEAR) + 60) * - TimeConstants::SECONDS_PER_DAY; + seconds = + ((2 * LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR) + 60) * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{0, // sec - 0, // min - 0, // hr - 1, // day - 2, // tm_mon starts with 0 for Jan - 1972 - TimeConstants::TIME_YEAR_BASE, // year - 3, // wday - 0, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{0, // sec + 0, // min + 0, // hr + 1, // day + 2, // tm_mon starts with 0 for Jan + 1972 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 3, // wday + 0, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, EndOf32BitEpochYear) { @@ -245,16 +263,17 @@ TEST(LlvmLibcGmTime, EndOf32BitEpochYear) { // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC. time_t seconds = 0x7FFFFFFF; struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{7, // sec - 14, // min - 3, // hr - 19, // day - 0, // tm_mon starts with 0 for Jan - 2038 - TimeConstants::TIME_YEAR_BASE, // year - 2, // wday - 7, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{7, // sec + 14, // min + 3, // hr + 19, // day + 0, // tm_mon starts with 0 for Jan + 2038 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 2, // wday + 7, // yday + 0}), + *tm_data); } TEST(LlvmLibcGmTime, Max64BitYear) { @@ -263,28 +282,30 @@ TEST(LlvmLibcGmTime, Max64BitYear) { // Mon Jan 1 12:50:50 2170 (200 years from 1970), time_t seconds = 6311479850; struct tm *tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{50, // sec - 50, // min - 12, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 2170 - TimeConstants::TIME_YEAR_BASE, // year - 1, // wday - 50, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{50, // sec + 50, // min + 12, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 2170 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 1, // wday + 50, // yday + 0}), + *tm_data); // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year. seconds = 67767976202043050; tm_data = LIBC_NAMESPACE::gmtime(&seconds); - EXPECT_TM_EQ((tm{50, // sec - 50, // min - 12, // hr - 1, // day - 0, // tm_mon starts with 0 for Jan - 2147483647 - TimeConstants::TIME_YEAR_BASE, // year - 2, // wday - 50, // yday - 0}), - *tm_data); + EXPECT_TM_EQ( + (tm{50, // sec + 50, // min + 12, // hr + 1, // day + 0, // tm_mon starts with 0 for Jan + 2147483647 - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE, // year + 2, // wday + 50, // yday + 0}), + *tm_data); } diff --git a/libc/test/src/time/mktime_test.cpp b/libc/test/src/time/mktime_test.cpp index 84e6c7eb2c42e4..fe1116f7dd2ef1 100644 --- a/libc/test/src/time/mktime_test.cpp +++ b/libc/test/src/time/mktime_test.cpp @@ -8,7 +8,7 @@ #include "src/__support/CPP/limits.h" // INT_MAX #include "src/time/mktime.h" -#include "src/time/time_utils.h" +#include "src/time/time_constants.h" #include "test/UnitTest/ErrnoSetterMatcher.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmHelper.h" @@ -16,29 +16,37 @@ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; -using LIBC_NAMESPACE::time_utils::Month; +using LIBC_NAMESPACE::time_constants::Month; static inline constexpr int tm_year(int year) { - return year - TimeConstants::TIME_YEAR_BASE; + return year - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE; } TEST(LlvmLibcMkTime, FailureSetsErrno) { - struct tm tm_data { - .tm_sec = INT_MAX, .tm_min = INT_MAX, .tm_hour = INT_MAX, - .tm_mday = INT_MAX, .tm_mon = INT_MAX - 1, .tm_year = tm_year(INT_MAX), - .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = INT_MAX, + .tm_min = INT_MAX, + .tm_hour = INT_MAX, + .tm_mday = INT_MAX, + .tm_mon = INT_MAX - 1, + .tm_year = tm_year(INT_MAX), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } TEST(LlvmLibcMkTime, InvalidSeconds) { { // -1 second from 1970-01-01 00:00:00 returns 1969-12-31 23:59:59. - struct tm tm_data { - .tm_sec = -1, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = -1, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(-1)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 59, @@ -54,11 +62,15 @@ TEST(LlvmLibcMkTime, InvalidSeconds) { { // 60 seconds from 1970-01-01 00:00:00 returns 1970-01-01 00:01:00. - struct tm tm_data { - .tm_sec = 60, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 60, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(60)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 1, @@ -76,13 +88,17 @@ TEST(LlvmLibcMkTime, InvalidSeconds) { TEST(LlvmLibcMkTime, InvalidMinutes) { { // -1 minute from 1970-01-01 00:00:00 returns 1969-12-31 23:59:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = -1, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = -1, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-TimeConstants::SECONDS_PER_MIN)); + Succeeds(-LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 59, .tm_hour = 23, @@ -97,13 +113,17 @@ TEST(LlvmLibcMkTime, InvalidMinutes) { { // 60 minutes from 1970-01-01 00:00:00 returns 1970-01-01 01:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 60, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 60, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(60 * TimeConstants::SECONDS_PER_MIN)); + Succeeds(60 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 1, @@ -120,13 +140,17 @@ TEST(LlvmLibcMkTime, InvalidMinutes) { TEST(LlvmLibcMkTime, InvalidHours) { { // -1 hour from 1970-01-01 00:00:00 returns 1969-12-31 23:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = -1, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = -1, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-TimeConstants::SECONDS_PER_HOUR)); + Succeeds(-LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 23, @@ -141,13 +165,18 @@ TEST(LlvmLibcMkTime, InvalidHours) { { // 24 hours from 1970-01-01 00:00:00 returns 1970-01-02 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 24, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; - EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(24 * TimeConstants::SECONDS_PER_HOUR)); + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 24, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; + EXPECT_THAT( + LIBC_NAMESPACE::mktime(&tm_data), + Succeeds(24 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -163,14 +192,18 @@ TEST(LlvmLibcMkTime, InvalidHours) { TEST(LlvmLibcMkTime, InvalidYear) { // -1 year from 1970-01-01 00:00:00 returns 1969-01-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1969), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1969), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-TimeConstants::DAYS_PER_NON_LEAP_YEAR * - TimeConstants::SECONDS_PER_DAY)); + Succeeds(-LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -188,61 +221,85 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) { return; { // 2038-01-19 03:14:08 tests overflow of the second in 2038. - struct tm tm_data { - .tm_sec = 8, .tm_min = 14, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 8, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2038-01-19 03:15:07 tests overflow of the minute in 2038. - struct tm tm_data { - .tm_sec = 7, .tm_min = 15, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 15, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2038-01-19 04:14:07 tests overflow of the hour in 2038. - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 4, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 4, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2038-01-20 03:14:07 tests overflow of the day in 2038. - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 20, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 20, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2038-02-19 03:14:07 tests overflow of the month in 2038. - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::FEBRUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::FEBRUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } { // 2039-01-19 03:14:07 tests overflow of the year. - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2039), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2039), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW)); } } @@ -250,12 +307,18 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) { TEST(LlvmLibcMkTime, InvalidMonths) { { // -1 month from 1970-01-01 00:00:00 returns 1969-12-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 0, .tm_mon = -1, - .tm_year = tm_year(1970), .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0 - }; - EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-32 * TimeConstants::SECONDS_PER_DAY)); + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 0, + .tm_mon = -1, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; + EXPECT_THAT( + LIBC_NAMESPACE::mktime(&tm_data), + Succeeds(-32 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -270,13 +333,19 @@ TEST(LlvmLibcMkTime, InvalidMonths) { { // 1970-13-01 00:00:00 returns 1971-01-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 1, .tm_mon = 12, - .tm_year = tm_year(1970), .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0 - }; - EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(TimeConstants::DAYS_PER_NON_LEAP_YEAR * - TimeConstants::SECONDS_PER_DAY)); + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = 12, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; + EXPECT_THAT( + LIBC_NAMESPACE::mktime(&tm_data), + Succeeds(LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -293,13 +362,17 @@ TEST(LlvmLibcMkTime, InvalidMonths) { TEST(LlvmLibcMkTime, InvalidDays) { { // -1 day from 1970-01-01 00:00:00 returns 1969-12-31 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = (1 - 1), - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = (1 - 1), + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(-1 * TimeConstants::SECONDS_PER_DAY)); + Succeeds(-1 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -314,13 +387,17 @@ TEST(LlvmLibcMkTime, InvalidDays) { { // 1970-01-32 00:00:00 returns 1970-02-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 32, - .tm_mon = Month::JANUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 32, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(31 * TimeConstants::SECONDS_PER_DAY)); + Succeeds(31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -335,13 +412,17 @@ TEST(LlvmLibcMkTime, InvalidDays) { { // 1970-02-29 00:00:00 returns 1970-03-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 29, - .tm_mon = Month::FEBRUARY, .tm_year = tm_year(1970), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 29, + .tm_mon = Month::FEBRUARY, + .tm_year = tm_year(1970), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(59 * TimeConstants::SECONDS_PER_DAY)); + Succeeds(59 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -356,14 +437,20 @@ TEST(LlvmLibcMkTime, InvalidDays) { { // 1972-02-30 00:00:00 returns 1972-03-01 00:00:00. - struct tm tm_data { - .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 30, - .tm_mon = Month::FEBRUARY, .tm_year = tm_year(1972), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; - EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(((2 * TimeConstants::DAYS_PER_NON_LEAP_YEAR) + 60) * - TimeConstants::SECONDS_PER_DAY)); + struct tm tm_data{.tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 30, + .tm_mon = Month::FEBRUARY, + .tm_year = tm_year(1972), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; + EXPECT_THAT( + LIBC_NAMESPACE::mktime(&tm_data), + Succeeds(((2 * LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR) + + 60) * + LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 0, .tm_min = 0, .tm_hour = 0, @@ -381,11 +468,15 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { // Test for maximum value of a signed 32-bit integer. // Test implementation can encode time for Tue 19 January 2038 03:14:07 UTC. { - struct tm tm_data { - .tm_sec = 7, .tm_min = 14, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 7, + .tm_min = 14, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(0x7FFFFFFF)); EXPECT_TM_EQ((tm{.tm_sec = 7, .tm_min = 14, @@ -403,11 +494,15 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { { // 2038-01-19 03:13:59 tests that even a large seconds field is // accepted if the minutes field is smaller. - struct tm tm_data { - .tm_sec = 59, .tm_min = 13, .tm_hour = 3, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 59, + .tm_min = 13, + .tm_hour = 3, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(0x7FFFFFFF - 8)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 13, @@ -424,13 +519,18 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { { // 2038-01-19 02:59:59 tests that large seconds and minutes are // accepted if the hours field is smaller. - struct tm tm_data { - .tm_sec = 59, .tm_min = 59, .tm_hour = 2, .tm_mday = 19, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 59, + .tm_min = 59, + .tm_hour = 2, + .tm_mday = 19, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(0x7FFFFFFF - 8 - 14 * TimeConstants::SECONDS_PER_MIN)); + Succeeds(0x7FFFFFFF - 8 - + 14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 59, .tm_hour = 2, @@ -446,14 +546,19 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { { // 2038-01-18 23:59:59 tests that large seconds, minutes and hours // are accepted if the days field is smaller. - struct tm tm_data { - .tm_sec = 59, .tm_min = 59, .tm_hour = 23, .tm_mday = 18, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2038), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 59, + .tm_min = 59, + .tm_hour = 23, + .tm_mday = 18, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2038), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(0x7FFFFFFF - 8 - 14 * TimeConstants::SECONDS_PER_MIN - - 3 * TimeConstants::SECONDS_PER_HOUR)); + Succeeds(0x7FFFFFFF - 8 - + 14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN - + 3 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 59, .tm_hour = 23, @@ -469,15 +574,20 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) { { // 2038-01-18 23:59:59 tests that the final second of 2037 is // accepted. - struct tm tm_data { - .tm_sec = 59, .tm_min = 59, .tm_hour = 23, .tm_mday = 31, - .tm_mon = Month::DECEMBER, .tm_year = tm_year(2037), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 59, + .tm_min = 59, + .tm_hour = 23, + .tm_mday = 31, + .tm_mon = Month::DECEMBER, + .tm_year = tm_year(2037), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), - Succeeds(0x7FFFFFFF - 8 - 14 * TimeConstants::SECONDS_PER_MIN - - 3 * TimeConstants::SECONDS_PER_HOUR - - 18 * TimeConstants::SECONDS_PER_DAY)); + Succeeds(0x7FFFFFFF - 8 - + 14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN - + 3 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR - + 18 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY)); EXPECT_TM_EQ((tm{.tm_sec = 59, .tm_min = 59, .tm_hour = 23, @@ -496,11 +606,15 @@ TEST(LlvmLibcMkTime, Max64BitYear) { return; { // Mon Jan 1 12:50:50 2170 (200 years from 1970), - struct tm tm_data { - .tm_sec = 50, .tm_min = 50, .tm_hour = 12, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2170), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 50, + .tm_min = 50, + .tm_hour = 12, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2170), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(6311479850)); EXPECT_TM_EQ((tm{.tm_sec = 50, .tm_min = 50, @@ -516,11 +630,15 @@ TEST(LlvmLibcMkTime, Max64BitYear) { { // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year. - struct tm tm_data { - .tm_sec = 50, .tm_min = 50, .tm_hour = 12, .tm_mday = 1, - .tm_mon = Month::JANUARY, .tm_year = tm_year(2147483647), .tm_wday = 0, - .tm_yday = 0, .tm_isdst = 0 - }; + struct tm tm_data{.tm_sec = 50, + .tm_min = 50, + .tm_hour = 12, + .tm_mday = 1, + .tm_mon = Month::JANUARY, + .tm_year = tm_year(2147483647), + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0}; EXPECT_THAT(LIBC_NAMESPACE::mktime(&tm_data), Succeeds(67767976202043050)); EXPECT_TM_EQ((tm{.tm_sec = 50, .tm_min = 50, diff --git a/libc/test/src/time/nanosleep_test.cpp b/libc/test/src/time/nanosleep_test.cpp index 2a6eea4d5e1613..d4f98e29bd9804 100644 --- a/libc/test/src/time/nanosleep_test.cpp +++ b/libc/test/src/time/nanosleep_test.cpp @@ -6,8 +6,7 @@ // //===----------------------------------------------------------------------===// -#include - +#include "hdr/types/struct_timespec.h" #include "src/errno/libc_errno.h" #include "src/time/nanosleep.h" #include "test/UnitTest/ErrnoSetterMatcher.h"