[libc-commits] [libc] [libc] Implement timegm (PR #243668)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 17 17:01:21 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Michael Jones (michaelrj-google)
<details>
<summary>Changes</summary>
This function is mktime but always in UTC. It's a BSD extension, but
it's also supported on linux. This PR adds the function as well as a
bunch of tests.
Assisted-by: Automated tooling, human reviewed.
---
Patch is 20.97 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/243668.diff
15 Files Affected:
- (modified) libc/config/baremetal/aarch64/entrypoints.txt (+1)
- (modified) libc/config/baremetal/arm/entrypoints.txt (+1)
- (modified) libc/config/baremetal/hexagon/entrypoints.txt (+1)
- (modified) libc/config/baremetal/riscv/entrypoints.txt (+1)
- (modified) libc/config/linux/aarch64/entrypoints.txt (+1)
- (modified) libc/config/linux/riscv/entrypoints.txt (+1)
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
- (modified) libc/include/time.yaml (+7)
- (modified) libc/src/time/CMakeLists.txt (+18)
- (modified) libc/src/time/time_utils.cpp (+5-2)
- (added) libc/src/time/timegm.cpp (+53)
- (added) libc/src/time/timegm.h (+27)
- (modified) libc/test/src/time/CMakeLists.txt (+22)
- (added) libc/test/src/time/timegm_test.cpp (+354)
- (modified) libc/utils/docgen/time.yaml (+3)
``````````diff
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 0fb0107bca1883..ca4f9529a5329b 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -298,6 +298,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.localtime
libc.src.time.localtime_r
libc.src.time.mktime
+ libc.src.time.timegm
libc.src.time.strftime
libc.src.time.strftime_l
libc.src.time.timespec_get
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 89f02bdd938d62..d949da5f68d6b6 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -298,6 +298,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.localtime
libc.src.time.localtime_r
libc.src.time.mktime
+ libc.src.time.timegm
libc.src.time.strftime
libc.src.time.strftime_l
libc.src.time.timespec_get
diff --git a/libc/config/baremetal/hexagon/entrypoints.txt b/libc/config/baremetal/hexagon/entrypoints.txt
index 891b563e8c91e1..89954a28c2747b 100644
--- a/libc/config/baremetal/hexagon/entrypoints.txt
+++ b/libc/config/baremetal/hexagon/entrypoints.txt
@@ -289,6 +289,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.localtime
libc.src.time.localtime_r
libc.src.time.mktime
+ libc.src.time.timegm
libc.src.time.strftime
libc.src.time.strftime_l
libc.src.time.timespec_get
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index b0ff79e477d94a..635558f85e5467 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -290,6 +290,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.localtime
libc.src.time.localtime_r
libc.src.time.mktime
+ libc.src.time.timegm
libc.src.time.strftime
libc.src.time.strftime_l
libc.src.time.timespec_get
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 291b11da09d6ad..22b8e722c03fb6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1506,6 +1506,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.gmtime
libc.src.time.gmtime_r
libc.src.time.mktime
+ libc.src.time.timegm
libc.src.time.clock_nanosleep
libc.src.time.nanosleep
libc.src.time.time
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index b8082355d8c5af..fe7e8d094ba490 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1726,6 +1726,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.localtime
libc.src.time.localtime_r
libc.src.time.mktime
+ libc.src.time.timegm
libc.src.time.clock_nanosleep
libc.src.time.nanosleep
libc.src.time.strftime
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 59d5a4ab376732..0f50f0188ddf6e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1730,6 +1730,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.localtime
libc.src.time.localtime_r
libc.src.time.mktime
+ libc.src.time.timegm
libc.src.time.clock_nanosleep
libc.src.time.nanosleep
libc.src.time.strftime
diff --git a/libc/include/time.yaml b/libc/include/time.yaml
index adacfab194f822..750b678819117b 100644
--- a/libc/include/time.yaml
+++ b/libc/include/time.yaml
@@ -124,6 +124,13 @@ functions:
return_type: time_t
arguments:
- type: struct tm *
+ - name: timegm
+ standards:
+ - stdc
+ - posix
+ return_type: time_t
+ arguments:
+ - type: struct tm *
- name: clock_nanosleep
standards:
- posix
diff --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index 10415731078965..275b8411030a2b 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -203,6 +203,24 @@ add_entrypoint_object(
libc.src.__support.macros.null_check
)
+add_entrypoint_object(
+ timegm
+ SRCS
+ timegm.cpp
+ HDRS
+ timegm.h
+ DEPENDS
+ .time_utils
+ .time_constants
+ libc.include.time
+ libc.src.errno.errno
+ libc.hdr.types.time_t
+ libc.hdr.types.struct_tm
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
+)
+
add_entrypoint_object(
strftime
SRCS
diff --git a/libc/src/time/time_utils.cpp b/libc/src/time/time_utils.cpp
index 8c4b4a3f3e60c6..a325958e1006d2 100644
--- a/libc/src/time/time_utils.cpp
+++ b/libc/src/time/time_utils.cpp
@@ -68,9 +68,12 @@ cpp::optional<time_t> mktime_internal(const tm *tm_out) {
// TODO: https://github.com/llvm/llvm-project/issues/121962
// Need to handle timezone and update of tm_isdst.
+ // Widen before multiplying so large tm_min/tm_hour values cannot overflow
+ // int.
time_t seconds = static_cast<time_t>(
- tm_out->tm_sec + tm_out->tm_min * time_constants::SECONDS_PER_MIN +
- tm_out->tm_hour * time_constants::SECONDS_PER_HOUR +
+ static_cast<int64_t>(tm_out->tm_sec) +
+ static_cast<int64_t>(tm_out->tm_min) * time_constants::SECONDS_PER_MIN +
+ static_cast<int64_t>(tm_out->tm_hour) * time_constants::SECONDS_PER_HOUR +
total_days * time_constants::SECONDS_PER_DAY);
return seconds;
}
diff --git a/libc/src/time/timegm.cpp b/libc/src/time/timegm.cpp
new file mode 100644
index 00000000000000..88b67a92dd7cae
--- /dev/null
+++ b/libc/src/time/timegm.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of timegm.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/time/timegm.h"
+#include "hdr/types/struct_tm.h"
+#include "hdr/types/time_t.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/time/time_constants.h"
+#include "src/time/time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(time_t, timegm, (struct tm * tm)) {
+ LIBC_CRASH_ON_NULLPTR(tm);
+
+ // NOTE: Currently mktime_internal interprets tm as UTC because LLVM-libc does
+ // not support timezones yet (see issue #121962). Once timezone support is
+ // added to mktime, timegm must continue to interpret tm strictly as UTC
+ // (ignoring TZ / local timezone and tm_isdst).
+ auto seconds = time_utils::mktime_internal(tm);
+ if (!seconds) {
+ libc_errno = time_utils::TIME_OVERFLOW;
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
+ }
+
+ auto status = time_utils::update_from_seconds(*seconds, tm);
+ if (!status.has_value()) {
+ libc_errno = status.error();
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
+ }
+
+#if defined(__linux__)
+ tm->tm_gmtoff = 0;
+ tm->tm_zone = "GMT";
+#endif
+
+ return *seconds;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/timegm.h b/libc/src/time/timegm.h
new file mode 100644
index 00000000000000..a9198ea5476fef
--- /dev/null
+++ b/libc/src/time/timegm.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header of timegm.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_TIME_TIMEGM_H
+#define LLVM_LIBC_SRC_TIME_TIMEGM_H
+
+#include "hdr/types/struct_tm.h"
+#include "hdr/types/time_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+time_t timegm(struct tm *tm);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_TIME_TIMEGM_H
diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index f2d71dba95516e..411e9b5a1b85fd 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -265,6 +265,28 @@ add_libc_test(
libc.src.time.time_constants
)
+add_libc_test(
+ timegm_test
+ SUITE
+ libc-time-tests
+ SRCS
+ timegm_test.cpp
+ CXX_STANDARD
+ 20
+ COMPILE_OPTIONS
+ ${time_test_copts}
+ DEPENDS
+ .time_test_utils
+ libc.hdr.types.struct_tm
+ libc.hdr.types.time_t
+ libc.src.__support.CPP.limits
+ libc.src.time.time_constants
+ libc.src.time.time_utils
+ libc.src.time.timegm
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_test(
clock_nanosleep_test
SUITE
diff --git a/libc/test/src/time/timegm_test.cpp b/libc/test/src/time/timegm_test.cpp
new file mode 100644
index 00000000000000..d652dfc21353f1
--- /dev/null
+++ b/libc/test/src/time/timegm_test.cpp
@@ -0,0 +1,354 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for timegm.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/types/struct_tm.h"
+#include "hdr/types/time_t.h"
+#include "src/__support/CPP/limits.h" // INT_MAX
+#include "src/time/time_constants.h"
+#include "src/time/time_utils.h"
+#include "src/time/timegm.h"
+#include "test/UnitTest/ErrnoCheckingTest.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_constants::Month;
+using LlvmLibcTimegmTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+static constexpr int tm_year(int year) {
+ return year - LIBC_NAMESPACE::time_constants::TIME_YEAR_BASE;
+}
+
+TEST_F(LlvmLibcTimegmTest, Epoch) {
+ const tm expected{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 1,
+ .tm_mon = Month::JANUARY,
+ .tm_year = tm_year(1970),
+ .tm_wday = 4,
+ .tm_yday = 0,
+ .tm_isdst = 0};
+ tm tm_data = expected;
+ tm_data.tm_wday = 0;
+ tm_data.tm_yday = 0;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(0)));
+ EXPECT_TM_EQ(expected, tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, BeforeEpoch) {
+ const tm expected{.tm_sec = 40,
+ .tm_min = 17,
+ .tm_hour = 20,
+ .tm_mday = 20,
+ .tm_mon = Month::JULY,
+ .tm_year = tm_year(1969),
+ .tm_wday = 0,
+ .tm_yday = 200,
+ .tm_isdst = 0};
+ tm tm_data = expected;
+ tm_data.tm_wday = 3;
+ tm_data.tm_yday = 0;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(-14182940)));
+ EXPECT_TM_EQ(expected, tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, LeapYear2000) {
+ const tm expected{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 29,
+ .tm_mon = Month::FEBRUARY,
+ .tm_year = tm_year(2000),
+ .tm_wday = 2,
+ .tm_yday = 59,
+ .tm_isdst = 0};
+ tm tm_data = expected;
+ tm_data.tm_wday = 0;
+ tm_data.tm_yday = 0;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(951782400)));
+ EXPECT_TM_EQ(expected, tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, NonLeapYear2100) {
+ // 2100 is not a leap year, so February 29th normalizes to March 1st.
+ tm tm_data{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 29,
+ .tm_mon = Month::FEBRUARY,
+ .tm_year = tm_year(2100),
+ .tm_wday = 0,
+ .tm_yday = 0,
+ .tm_isdst = 0};
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(4107542400)));
+ EXPECT_TM_EQ((tm{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 1,
+ .tm_mon = Month::MARCH,
+ .tm_year = tm_year(2100),
+ .tm_wday = 1,
+ .tm_yday = 59,
+ .tm_isdst = 0}),
+ tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, NormalizeMonth) {
+ // Month 12 of 2023 is January 2024.
+ tm tm_data{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 1,
+ .tm_mon = 12,
+ .tm_year = tm_year(2023),
+ .tm_wday = 0,
+ .tm_yday = 0,
+ .tm_isdst = 0};
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(1704067200)));
+ EXPECT_TM_EQ((tm{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 1,
+ .tm_mon = Month::JANUARY,
+ .tm_year = tm_year(2024),
+ .tm_wday = 1,
+ .tm_yday = 0,
+ .tm_isdst = 0}),
+ tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, NormalizeDayZero) {
+ // Day 0 of March 2024 is the last day of February 2024.
+ tm tm_data{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 0,
+ .tm_mon = Month::MARCH,
+ .tm_year = tm_year(2024),
+ .tm_wday = 0,
+ .tm_yday = 0,
+ .tm_isdst = 0};
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(1709164800)));
+ EXPECT_TM_EQ((tm{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 29,
+ .tm_mon = Month::FEBRUARY,
+ .tm_year = tm_year(2024),
+ .tm_wday = 4,
+ .tm_yday = 59,
+ .tm_isdst = 0}),
+ tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, NormalizeLargeMinutes) {
+ // INT_MAX minutes after the epoch must not overflow int arithmetic.
+ tm tm_data{.tm_sec = 0,
+ .tm_min = INT_MAX,
+ .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::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(128849018820)));
+ EXPECT_TM_EQ((tm{.tm_sec = 0,
+ .tm_min = 7,
+ .tm_hour = 2,
+ .tm_mday = 23,
+ .tm_mon = Month::JANUARY,
+ .tm_year = tm_year(6053),
+ .tm_wday = 4,
+ .tm_yday = 22,
+ .tm_isdst = 0}),
+ tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, Year2038) {
+ constexpr time_t INT32_MAX_SECONDS = 2147483647;
+ tm expected{.tm_sec = 7,
+ .tm_min = 14,
+ .tm_hour = 3,
+ .tm_mday = 19,
+ .tm_mon = Month::JANUARY,
+ .tm_year = tm_year(2038),
+ .tm_wday = 2,
+ .tm_yday = 18,
+ .tm_isdst = 0};
+ tm tm_data = expected;
+ tm_data.tm_wday = 0;
+ tm_data.tm_yday = 0;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data), Succeeds(INT32_MAX_SECONDS));
+ EXPECT_TM_EQ(expected, tm_data);
+
+ // One second later does not fit in 32 bits, but time_t is 64-bit.
+ expected.tm_sec = 8;
+ tm_data = expected;
+ tm_data.tm_wday = 0;
+ tm_data.tm_yday = 0;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(INT32_MAX_SECONDS + 1));
+ EXPECT_TM_EQ(expected, tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, IgnoresAndClearsIsdst) {
+ const tm base{.tm_sec = 0,
+ .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};
+
+ tm tm_data = base;
+ tm_data.tm_isdst = -1;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(0)));
+ EXPECT_EQ(tm_data.tm_isdst, 0);
+
+ tm_data = base;
+ tm_data.tm_isdst = 1;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(0)));
+ EXPECT_EQ(tm_data.tm_isdst, 0);
+}
+
+#ifdef __linux__
+TEST_F(LlvmLibcTimegmTest, SetsUtcOffset) {
+ tm tm_data{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 12,
+ .tm_mday = 1,
+ .tm_mon = Month::JUNE,
+ .tm_year = tm_year(2024),
+ .tm_wday = 0,
+ .tm_yday = 0,
+ .tm_isdst = 0};
+ tm_data.tm_gmtoff = 3600;
+ tm_data.tm_zone = nullptr;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(1717243200)));
+ EXPECT_EQ(tm_data.tm_gmtoff, 0L);
+ EXPECT_STREQ(tm_data.tm_zone, "GMT");
+}
+#endif
+
+TEST_F(LlvmLibcTimegmTest, Overflow) {
+ // Month 12 of the largest tm_year needs a tm_year past INT_MAX.
+ tm tm_data{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 1,
+ .tm_mon = 12,
+ .tm_year = INT_MAX,
+ .tm_wday = 0,
+ .tm_yday = 0,
+ .tm_isdst = 0};
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Fails(LIBC_NAMESPACE::time_utils::TIME_OVERFLOW,
+ static_cast<time_t>(-1)));
+}
+
+TEST_F(LlvmLibcTimegmTest, NullPtr) {
+ EXPECT_DEATH([] { LIBC_NAMESPACE::timegm(nullptr); }, WITH_SIGNAL(-1));
+}
+
+TEST_F(LlvmLibcTimegmTest, NormalizeMultipleFields) {
+ tm tm_data{.tm_sec = 61,
+ .tm_min = -1,
+ .tm_hour = 25,
+ .tm_mday = 0,
+ .tm_mon = -1,
+ .tm_year = tm_year(2024),
+ .tm_wday = 0,
+ .tm_yday = 0,
+ .tm_isdst = 0};
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(1701392401)));
+ EXPECT_TM_EQ((tm{.tm_sec = 1,
+ .tm_min = 0,
+ .tm_hour = 1,
+ .tm_mday = 1,
+ .tm_mon = Month::DECEMBER,
+ .tm_year = tm_year(2023),
+ .tm_wday = 5,
+ .tm_yday = 334,
+ .tm_isdst = 0}),
+ tm_data);
+}
+
+TEST_F(LlvmLibcTimegmTest, AdditionalDates) {
+ const tm expected[] = {
+ tm{.tm_sec = 59,
+ .tm_min = 59,
+ .tm_hour = 23,
+ .tm_mday = 31,
+ .tm_mon = Month::DECEMBER,
+ .tm_year = tm_year(1969),
+ .tm_wday = 3,
+ .tm_yday = 364,
+ .tm_isdst = 0},
+ tm{.tm_sec = 15,
+ .tm_min = 30,
+ .tm_hour = 12,
+ .tm_mday = 29,
+ .tm_mon = Month::FEBRUARY,
+ .tm_year = tm_year(2000),
+ .tm_wday = 2,
+ .tm_yday = 59,
+ .tm_isdst = 0},
+ tm{.tm_sec = 0,
+ .tm_min = 0,
+ .tm_hour = 0,
+ .tm_mday = 1,
+ .tm_mon = Month::MARCH,
+ .tm_year = tm_year(1900),
+ .tm_wday = 4,
+ .tm_yday = 59,
+ .tm_isdst = 0},
+ };
+
+ tm tm_data = expected[0];
+ tm_data.tm_wday = 0;
+ tm_data.tm_yd...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/243668
More information about the libc-commits
mailing list