[libc-commits] [libc] 8ac9c19 - [libc] Implement timegm (#243668)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 17 23:52:01 PST 2026
Author: Michael Jones
Date: 2026-11-18T07:51:56Z
New Revision: 8ac9c196cea90a0daa8a66894f6d4032ebd6dbad
URL: https://github.com/llvm/llvm-project/commit/8ac9c196cea90a0daa8a66894f6d4032ebd6dbad
DIFF: https://github.com/llvm/llvm-project/commit/8ac9c196cea90a0daa8a66894f6d4032ebd6dbad.diff
LOG: [libc] Implement timegm (#243668)
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.
Added:
libc/src/time/timegm.cpp
libc/src/time/timegm.h
libc/test/src/time/timegm_test.cpp
Modified:
libc/config/baremetal/aarch64/entrypoints.txt
libc/config/baremetal/arm/entrypoints.txt
libc/config/baremetal/hexagon/entrypoints.txt
libc/config/baremetal/riscv/entrypoints.txt
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/time.yaml
libc/src/time/CMakeLists.txt
libc/src/time/time_utils.cpp
libc/test/src/time/CMakeLists.txt
libc/utils/docgen/time.yaml
Removed:
################################################################################
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index d5ae19be949836..e3d5c8c0a68399 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 cf6fcc97c21759..ed7b683676be19 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 b8451998151abb..b83f4d11379c4d 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 1c66546a34b248..e2de9fb04985b7 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 23e3a1e5cea676..c46362ef148499 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1518,6 +1518,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 5432ad8493875e..35ab0d8640830e 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1740,6 +1740,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 5ec38cdb0f48a6..e381e45d9bba7f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1744,6 +1744,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..ede55e3d501f8c 100644
--- a/libc/src/time/time_utils.cpp
+++ b/libc/src/time/time_utils.cpp
@@ -68,9 +68,13 @@ 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_yday = 0;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(-1)));
+ EXPECT_TM_EQ(expected[0], tm_data);
+
+ tm_data = expected[1];
+ tm_data.tm_wday = 0;
+ tm_data.tm_yday = 0;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(951827415)));
+ EXPECT_TM_EQ(expected[1], tm_data);
+
+ tm_data = expected[2];
+ tm_data.tm_wday = 0;
+ tm_data.tm_yday = 0;
+ EXPECT_THAT(LIBC_NAMESPACE::timegm(&tm_data),
+ Succeeds(static_cast<time_t>(-2203891200)));
+ EXPECT_TM_EQ(expected[2], tm_data);
+}
diff --git a/libc/utils/docgen/time.yaml b/libc/utils/docgen/time.yaml
index d2302a0118d0c8..1a01f4f6acb6d7 100644
--- a/libc/utils/docgen/time.yaml
+++ b/libc/utils/docgen/time.yaml
@@ -101,6 +101,9 @@ functions:
time:
c-definition: 7.27.2.4
in-latest-posix: ''
+ timegm:
+ c-definition: 7.29.2.4
+ in-latest-posix: ''
timespec_get:
c-definition: 7.27.2.5
timer_create:
More information about the libc-commits
mailing list