From 7069d891cdc0e84cb1762ba73ed8a2251565cd02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B8=D1=88=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0?= Date: Thu, 26 Dec 2024 14:53:54 +0500 Subject: [PATCH] refactor: fix tests --- libc/src/time/mktime.cpp | 2 +- libc/src/time/time_utils.cpp | 137 ++++++++++++------------ libc/src/time/time_utils.h | 8 +- libc/test/src/time/asctime_test.cpp | 12 +-- libc/test/src/time/gmtime_r_test.cpp | 9 +- libc/test/src/time/localtime_r_test.cpp | 8 +- 6 files changed, 90 insertions(+), 86 deletions(-) diff --git a/libc/src/time/mktime.cpp b/libc/src/time/mktime.cpp index 52a8116bd5761b0..72cd229120538da 100644 --- a/libc/src/time/mktime.cpp +++ b/libc/src/time/mktime.cpp @@ -113,7 +113,7 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) { total_days * TimeConstants::SECONDS_PER_DAY; // Update the tm structure's year, month, day, etc. from seconds. - if (time_utils::update_from_seconds(seconds, tm_out, false) < 0) + if (time_utils::update_from_seconds(seconds, tm_out) < 0) return time_utils::out_of_range(); return static_cast(seconds); diff --git a/libc/src/time/time_utils.cpp b/libc/src/time/time_utils.cpp index 2b8a8171396c332..44681190ee03151 100644 --- a/libc/src/time/time_utils.cpp +++ b/libc/src/time/time_utils.cpp @@ -85,7 +85,7 @@ char *get_env_var(const char *var_name) { // // Compute the number of months from the remaining days. Finally, adjust years // to be 1900 and months to be from January. -int64_t update_from_seconds(int64_t total_seconds, struct tm *tm, bool local) { +int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) { // Days in month starting from March in the year 2000. static const char daysInMonth[] = {31 /* Mar */, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29}; @@ -171,66 +171,6 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm, bool local) { if (years > INT_MAX || years < INT_MIN) return time_utils::out_of_range(); - int offset; - int dst; - - offset = 0; - dst = is_dst(tm); - if (local) { - char *tz_filename = get_env_var("TZ"); - if (tz_filename[0] == '\0') { - char localtime[15] = "/etc/localtime"; - size_t i = 0; - while (localtime[i] != '\0') { - tz_filename[i] = localtime[i]; - i++; - } - } else { - char tmp[64]; - char prefix[21] = "/usr/share/zoneinfo/"; - size_t i = 0; - while (prefix[i] != '\0') { - tmp[i] = prefix[i]; - i++; - } - - i = 0; - while (tz_filename[i] != '\0') { - tmp[i + 20] = tz_filename[i]; - i++; - } - - tz_filename = tmp; - while (tz_filename[i] != '\0') { - if (tz_filename[i] == (char)0xFFFFFFAA) { - tz_filename[i] = '\0'; - } - i++; - } - } - - acquire_file(tz_filename); - - size_t filesize; - filesize = static_cast(lseek(fd, 0, SEEK_END)); - if (filesize < 0) { - close(fd); - return 0; - } - lseek(fd, 0, 0); - - timezone::tzset *ptr_tzset = timezone::get_tzset(fd, filesize); - if (ptr_tzset == nullptr) { - return time_utils::out_of_range(); - } - - for (size_t i = 0; i < *ptr_tzset->ttinfo->size; i++) { - if (dst == ptr_tzset->ttinfo[i].tt_isdst) { - offset = static_cast(ptr_tzset->ttinfo[i].tt_utoff / 3600); - } - } - } - // 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); @@ -247,16 +187,79 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm, bool local) { tm->tm_sec = static_cast(remainingSeconds % TimeConstants::SECONDS_PER_MIN); - if (local) { - tm->tm_hour += offset; - tm->tm_isdst = dst; + return 0; +} + +struct tm *get_localtime(time_t *t_ptr) { + struct tm *tm; + int offset; + int dst; + + tm = nullptr; + offset = 0; + dst = is_dst(tm); + update_from_seconds(*t_ptr, tm); + char *tz_filename = get_env_var("TZ"); + if (tz_filename[0] == '\0') { + char localtime[15] = "/etc/localtime"; + size_t i = 0; + while (localtime[i] != '\0') { + tz_filename[i] = localtime[i]; + i++; + } + } else { + char tmp[64]; + char prefix[21] = "/usr/share/zoneinfo/"; + size_t i = 0; + while (prefix[i] != '\0') { + tmp[i] = prefix[i]; + i++; + } + + i = 0; + while (tz_filename[i] != '\0') { + tmp[i + 20] = tz_filename[i]; + i++; + } - if (file_usage == 1) { - release_file(fd); + tz_filename = tmp; + while (tz_filename[i] != '\0') { + if (tz_filename[i] == (char)0xFFFFFFAA) { + tz_filename[i] = '\0'; } + i++; + } } - return 0; + acquire_file(tz_filename); + + size_t filesize; + filesize = static_cast(lseek(fd, 0, SEEK_END)); + if (filesize < 0) { + close(fd); + return nullptr; + } + lseek(fd, 0, 0); + + timezone::tzset *ptr_tzset = timezone::get_tzset(fd, filesize); + if (ptr_tzset == nullptr) { + return nullptr; + } + + for (size_t i = 0; i < *ptr_tzset->ttinfo->size; i++) { + if (dst == ptr_tzset->ttinfo[i].tt_isdst) { + offset = static_cast(ptr_tzset->ttinfo[i].tt_utoff / 3600); + } + } + + tm->tm_hour += offset; + tm->tm_isdst = dst; + + if (file_usage == 1) { + release_file(fd); + } + + return tm; } unsigned char is_dst(struct tm *tm) { diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h index 149de541bbab4b7..7aad91a6b6721d4 100644 --- a/libc/src/time/time_utils.h +++ b/libc/src/time/time_utils.h @@ -86,7 +86,7 @@ struct TimeConstants { // 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, bool local); +extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm); extern unsigned char is_dst(struct tm *tm); extern char *get_env_var(const char *var_name); extern timezone::tzset *get_tzset(int fd, size_t filesize); @@ -153,7 +153,7 @@ LIBC_INLINE char *asctime(const struct tm *timeptr, char *buffer, LIBC_INLINE struct tm *gmtime_internal(const time_t *timer, struct tm *result) { int64_t seconds = *timer; // Update the tm structure's year, month, day, etc. from seconds. - if (update_from_seconds(seconds, result, false) < 0) { + if (update_from_seconds(seconds, result) < 0) { out_of_range(); return nullptr; } @@ -166,7 +166,7 @@ LIBC_INLINE struct tm *localtime(const time_t *t_ptr) { int64_t time = *t_ptr; // Update the tm structure's year, month, day, etc. from seconds. - if (update_from_seconds(time, &result, true) < 0) { + if (update_from_seconds(time, &result) < 0) { out_of_range(); return nullptr; } @@ -179,7 +179,7 @@ LIBC_INLINE struct tm *localtime_internal(const time_t *t_ptr, int64_t t = *t_ptr; // Update the tm structure's year, month, day, etc. from seconds. - if (update_from_seconds(t, input, true) < 0) { + if (update_from_seconds(t, input) < 0) { out_of_range(); return nullptr; } diff --git a/libc/test/src/time/asctime_test.cpp b/libc/test/src/time/asctime_test.cpp index 3d9f82c916ecfff..92a0de25dbc740a 100644 --- a/libc/test/src/time/asctime_test.cpp +++ b/libc/test/src/time/asctime_test.cpp @@ -21,7 +21,7 @@ static inline char *call_asctime(struct tm *tm_data, int year, int month, TEST(LlvmLibcAsctime, Nullptr) { char *result; result = asctime(nullptr); - //ASSERT_ERRNO_EQ(EINVAL); + // ASSERT_ERRNO_EQ(EINVAL); ASSERT_STREQ(nullptr, result); } @@ -51,7 +51,7 @@ TEST(LlvmLibcAsctime, InvalidWday) { 0, // sec 7, // wday 0); // yday - //ASSERT_ERRNO_EQ(EINVAL); + // ASSERT_ERRNO_EQ(EINVAL); } // Months are from January to December. Test passing invalid value in month. @@ -68,7 +68,7 @@ TEST(LlvmLibcAsctime, InvalidMonth) { 0, // sec 4, // wday 0); // yday - //ASSERT_ERRNO_EQ(EINVAL); + // ASSERT_ERRNO_EQ(EINVAL); // Test with month = 13. call_asctime(&tm_data, @@ -80,7 +80,7 @@ TEST(LlvmLibcAsctime, InvalidMonth) { 0, // sec 4, // wday 0); // yday - //ASSERT_ERRNO_EQ(EINVAL); + // ASSERT_ERRNO_EQ(EINVAL); } TEST(LlvmLibcAsctime, ValidWeekdays) { @@ -208,6 +208,6 @@ TEST(LlvmLibcAsctime, Max64BitYear) { 50, // sec 2, // wday 50); // yday - //ASSERT_ERRNO_EQ(EOVERFLOW); - //ASSERT_STREQ(nullptr, result); + // ASSERT_ERRNO_EQ(EOVERFLOW); + // ASSERT_STREQ(nullptr, result); } diff --git a/libc/test/src/time/gmtime_r_test.cpp b/libc/test/src/time/gmtime_r_test.cpp index 5c6838b135844f2..d7cb1bdf117f4f5 100644 --- a/libc/test/src/time/gmtime_r_test.cpp +++ b/libc/test/src/time/gmtime_r_test.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/time/gmtime_r.h" +#include "src/time/localtime.h" #include "src/time/time_utils.h" #include "test/UnitTest/Test.h" #include "test/src/time/TmMatcher.h" @@ -15,7 +16,7 @@ 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) { +TEST(LlvmLibcGmTimeR, 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. time_t seconds = 0x7FFFFFFF; @@ -33,9 +34,9 @@ using LIBC_NAMESPACE::time_utils::TimeConstants; 0}), *tm_data_ptr); EXPECT_TM_EQ(*tm_data_ptr, tm_data); -}*/ +} -/*TEST(LlvmLibcGmTimeR, Max64BitYear) { +TEST(LlvmLibcGmTimeR, Max64BitYear) { if (sizeof(time_t) == 4) return; // Test for Tue Jan 1 12:50:50 in 2,147,483,647th year. @@ -54,4 +55,4 @@ using LIBC_NAMESPACE::time_utils::TimeConstants; 0}), *tm_data_ptr); EXPECT_TM_EQ(*tm_data_ptr, tm_data); -}*/ +} diff --git a/libc/test/src/time/localtime_r_test.cpp b/libc/test/src/time/localtime_r_test.cpp index 8f426ce3d824886..c4fb25f1fe84165 100644 --- a/libc/test/src/time/localtime_r_test.cpp +++ b/libc/test/src/time/localtime_r_test.cpp @@ -111,20 +111,20 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32IntDst) { ASSERT_EQ(121, input.tm_year); ASSERT_EQ(6, input.tm_mon); ASSERT_EQ(25, input.tm_mday); - //ASSERT_EQ(17, input.tm_hour); + // ASSERT_EQ(17, input.tm_hour); ASSERT_EQ(4, input.tm_min); ASSERT_EQ(25, input.tm_sec); ASSERT_EQ(0, input.tm_wday); ASSERT_EQ(205, input.tm_yday); - //ASSERT_EQ(1, input.tm_isdst); + // ASSERT_EQ(1, input.tm_isdst); ASSERT_EQ(121, result->tm_year); ASSERT_EQ(6, result->tm_mon); ASSERT_EQ(25, result->tm_mday); - //ASSERT_EQ(17, result->tm_hour); + // ASSERT_EQ(17, result->tm_hour); ASSERT_EQ(4, result->tm_min); ASSERT_EQ(25, result->tm_sec); ASSERT_EQ(0, result->tm_wday); ASSERT_EQ(205, result->tm_yday); - //ASSERT_EQ(1, result->tm_isdst); + // ASSERT_EQ(1, result->tm_isdst); }