[libc-commits] [libc] 83ca6fc - [libc] Fix gmtime and mktime for years before zero (#236515)
via libc-commits
libc-commits at lists.llvm.org
Thu Nov 5 09:54:44 PST 2026
Author: Mike Lothian
Date: 2026-11-05T09:54:39-08:00
New Revision: 83ca6fc7d67979609fa30dbf0d919b7b2a5416fa
URL: https://github.com/llvm/llvm-project/commit/83ca6fc7d67979609fa30dbf0d919b7b2a5416fa
DIFF: https://github.com/llvm/llvm-project/commit/83ca6fc7d67979609fa30dbf0d919b7b2a5416fa.diff
LOG: [libc] Fix gmtime and mktime for years before zero (#236515)
yrs % 4 is negative for a year before zero, which moved the month out of
range and aborted on the assertion that follows. Mask the low two bits
instead, which is the non-negative remainder
Add the year before zero and year zero to the gmtime test
AI disclosure: this patch was written with Claude Code. I reviewed the
change and ran the new test; it fails without the fix and passes with it
Assisted-by: Claude
Added:
Modified:
libc/src/time/time_utils.cpp
libc/test/src/time/gmtime_test.cpp
Removed:
################################################################################
diff --git a/libc/src/time/time_utils.cpp b/libc/src/time/time_utils.cpp
index beb6d3d2130bcd..eecac3f356bb59 100644
--- a/libc/src/time/time_utils.cpp
+++ b/libc/src/time/time_utils.cpp
@@ -162,8 +162,10 @@ ErrorOr<void> update_from_seconds(time_t total_seconds, tm *tm) {
// N packs month (high 16 bits) and day-part (low 16 bits).
// (yrs % 4) * 512 corrects a 1/4-day-per-year drift introduced by
// skipping the explicit day-of-year step. 512 = 2^16 / 32 / 4, i.e.
- // one quarter of a "fake 32-day month" in 16-bit space.
- int64_t n_val = (yrs % 4) * 512 + shift - static_cast<int64_t>(ypt);
+ // one quarter of a "fake 32-day month" in 16-bit space. yrs is negative
+ // before year zero and % 4 would then be negative too, so the remainder
+ // is taken with & 3, which stays in [0, 3] for two's complement values.
+ int64_t n_val = (yrs & 3) * 512 + shift - static_cast<int64_t>(ypt);
int64_t d_val =
static_cast<int64_t>((static_cast<UInt128>(n_val & 65535) * C3) >> 64);
diff --git a/libc/test/src/time/gmtime_test.cpp b/libc/test/src/time/gmtime_test.cpp
index de3f8fe9d883ca..2c67c76e70afd4 100644
--- a/libc/test/src/time/gmtime_test.cpp
+++ b/libc/test/src/time/gmtime_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for gmtime ----------------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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 gmtime.
+///
+//===----------------------------------------------------------------------===//
#include "hdr/errno_macros.h"
@@ -436,6 +441,8 @@ TEST_F(LlvmLibcGmTime, KeyYears) {
};
TestCase cases[] = {
+ {-62198755200LL, -1, 5, "-0001-01-01 (before year zero)"},
+ {-62167219200LL, 0, 6, "0000-01-01 (leap year zero)"},
{-2208988800, 1900, 1, "1900-01-01 Monday (non-leap century)"},
{0, 1970, 4, "1970-01-01 Thursday (epoch)"},
{915148800, 1999, 5, "1999-01-01 Friday"},
More information about the libc-commits
mailing list