fix(libc): convert mktime before 1900 from the zone's own offsets on macOS #6686
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6686"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T04:48:44Z.
What
On macOS a guest
mktimeof a time before 1900 returns -1, althoughlocaltimeconverts the same time.mktimehands the broken-down time to the host'smktime, and macOS's refuses every year before 1900. A standalone program on this Mac (TZ=UTC-2, 1 January at noon) gets -1 for the years 1, 1000, 1500, 1570, 1582, 1600, 1700, 1800, 1850 and 1883, and the right value from 1900 on.On macOS
mktimenow solves a local time before 2 January 1900 directly. It subtracts the offsetlocaltime_rgives for a candidate time from the local fields, and repeats until the offset holds, at most four times.localtime_rfollows the zone's history: local mean time before its first transition, then early standard offsets such as Kolkata's MMT or Buenos Aires's CMT. Somktime(localtime(t))returnstin region zones as well as fixed-offset ones. From 2 January 1900 on, the host'smktimestill converts.The first version moved such a time 400-year cycles later, as Windows does (#4811). jotitadev found in review that this applies the zone's modern rules to the moved year, so region zones were off by the LMT difference. The second commit replaces that on macOS.
The constants block is shared with Windows and keeps Windows's values (years 70 to 1100, first ordinary second one day, last 32535215999). The Windows conversion is otherwise unchanged, and Linux doesn't compile any of the changed lines. A
tm_isdstset on a date where the zone had no daylight offset is taken as standard time. FreeBSD'stime1would shift it by the difference between one of the zone's daylight and standard types. Thelibc.mdentry forlocaltime, mktimerecords this.Commits:
Tested
macOS 26.7.1, Apple M2, AppleClang 21.0.0, x86-64 build under Rosetta, Release.
maindoes not build libc on macOS for other reasons as well (#4627 addresses them), so the test ran in a local tree ofmaince991f1ecwith #4627, #4630, #6124 and mugurc's macOS branches merged. This branch'sTime.cppis identical to the one tested, apart from #4627'stm_zoneline.main'sTime.cpp:guest_time_conversionaborts withmktime did not invert localtime outside 1970 to 3000. Of its ten times, -12622780801 (1570) and -62135596800 (year 1) get -1 frommktime; the other eight invert.localtimegave and with -1.guest_time_conversionpasses 5 runs of 5.tools/check_conventions.py --base origin/mainandgit diff --checkpass.Windows 11 (WinLibs GCC 15.2.0 r7, UCRT, Release) and Ubuntu 24.04 under WSL2 (GCC 13.3.0, Release), head
0e6ad36femerged ontomain5d9dcc42d:guest_time_conversionpasses 3 runs of 3 with the change, and 3 runs of 3 withlibc/src/Time.cppas onmainand the tests kept. The new commits change only macOS lines inTime.cpp. The new test case also runs on Linux and hasn't been run there yet.Overlap: the overlap check could not compare 23 pull requests that conflict with
main; none of their changes toTime.cpptouchesmktimeor the 400-year cycle code.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T05:32:22Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/tests/GuestTimeConversion.cppL299-300Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
docs/dev/debt/libc.mdcore/libs/prx/libc/src/Time.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/tests/GuestTimeConversion.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/src/Time.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mddocs/dev/debt/libc.mdImported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-16T05:36:06Z.
Review: COMMENTED
Checked the macOS branch against region time zones, head
0e6ad36fe, basemain5d9dcc42d.guest_time_conversionrunsCheckLocalRangeunderTZ=UTC-2, a fixed offset. With a region zone, which is what a Mac has by default (/etc/localtime), macOS'slocaltime_rgives times before the zone's first transition its LMT (or other historical) offset, while the shiftedmktimehere converts in the year + 400k, where the zone's modern rules apply. Somktime(localtime(t))no longer returnst:localtime_roffsetmktime(localtime(t)) - tWith
tm_isdst = -1a summer date also picks up daylight time from the shifted year, though the zone had none then: 1811-07-23 gives -3178 in Los Angeles and -3992 in Berlin. The 1900-01-01 row comes from the one-day margin (firstOrdinary), which also shifts the first day of 1900.On Windows the same shift stays consistent, as far as I know, because the CRT applies the current rules to every year. On macOS one way to keep it consistent would be to take the offset from
localtime_ritself, which handles these years: start fromapproximate - gmtoff(approximate)and redo that once or twice until the offset holds. In the mirror below that gives 0 for every pre-1900 row in the table; the only other difference is the instant of Los Angeles's 1883 switch to PST, a local time that occurs twice. RunningCheckLocalRangeonce more under a region zone (America/Los_Angeles, say) would cover it.Method: a standalone mirror of the
__APPLE__path oflibc_mktime_nid_postfix(sameordinarytest,firstOrdinary,calendarCycles,tm_isdstcarried over, then hostmktimeplus the cycles; civil-day helpers in place ofutcSecondsandutcCalendarFields). I fed itlocaltime_r(t)for the times in the table. It gives 0 for every row underUTC-2, which matches your 5/5. Same numbers on arm64 and x86_64 under Rosetta.Time.cppfrom this head also passes-fsyntax-onlyon both architectures, except thetm_zoneconst error at its line 85, whichmainhas too (line 75). I did not run the fullguest_time_conversion(maindoesn't build libc on macOS).Machine: Apple M2 Max, macOS 27.2 (26B5091g), Apple clang 21.0.0.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T07:33:44Z.
Windows and Linux results for
0e6ad36fe4, merged ontomain5d9dcc42d2:main, new tests keptguest_time_conversion3/3guest_time_conversion3/3Reverted for the middle column:
libc/src/Time.cpp. The test passes either way on both hosts, as expected for a change that only alters the macOS path. The pre-1900mktimehandling shows no change on Windows and Linux.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T07:34:07Z.
A correction to my comment above: this change is not macOS-only. It also rewrites the Windows branch of
libc_mktime_nid_postfix, using the newHOST_ORDINARY_*/HOST_FIRST_SECONDconstants. On Windows they keep the old values: years 70 to 1100, andfirstOrdinary= 0 + 86400, as before. So the Windows 3/3 is a check that the rewrite keeps Windows behaviour. Linux is outside the_WIN32 || __APPLE__block, so it is unchanged.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-16T09:30:07Z.
@jotitadev thanks, you're right, and the table holds on this Mac too.
A non-aborting copy of the new test against the first version (
0e6ad36fe) gets 26 of 36 round trips off, by exactly your numbers: +422 s in Los Angeles, -392 in Berlin, +1408 and -530 in Kolkata, -3228 and -4608 in Buenos Aires.69aa703c6takes your approach on macOS: a local time before 2 January 1900 is solved from the offsetlocaltime_rgives, repeated until it holds, at most four times. That also removes the 1 January 1900 row.guest_time_conversionnow converts nine times, from the year 1 to 1950, through those four zones, with the daylight flaglocaltimegave and with -1. It passes 5 runs of 5.A
tm_isdstset where the zone had no daylight offset is taken as standard time. FreeBSD 11'stime1would shift it by the difference between one of the zone's daylight and standard types instead. That's in the debt entry.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T18:02:46Z.
Integration check: issue #6910 has a ready change that makes local-time conversions apply
TZfrom the guest environment. In a combined tree,CheckRegionZoneshere currently sets the hostTZdirectly withsetenv, while the guest environment remains at the test program’sUTC-2; the next conversion would restoreUTC-2and the region-zone round trips would no longer exercise those regions. Could we let this PR land first, then I can carry #6910 with its combined test setting the guestTZthroughsetenv_nid_postfix? If you prefer a different integration order, I can adapt around that.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T18:16:17Z.
I opened #6916 carrying the guest-TZ change. The PR body and its libc debt note both call out the
CheckRegionZonesinteraction: after guest TZ has been set, the test’s host-sidesetenvcan be overwritten by the next guest conversion. Could you update that region-zone test to set the guest TZ, or otherwise verify the combined test? I will keep the integration requirement visible and will not treat the two changes as independently mergeable without the combined test passing.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T18:31:55Z.
Correction to my earlier integration warning: I composed #6686 head
69aa703c6and #6916 in a temporary tree. After resolving only the overlapping test-file edits while retaining both sets of checks,guest_time_conversionpassed. I kept #6686’s original host-sidesetenv("TZ", zone, 1)and added a temporary assertion that the 2024-01-01 conversion had the expected offsets for Los Angeles, Berlin, Kolkata and Buenos Aires; all four passed.applyGuestZone()does not overwrite a host-side zone change when the guestTZsnapshot itself is unchanged. My earlier warning and request to changeCheckRegionZoneswere mistaken; no change to your test is needed.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T09:01:35Z.
Checked the macOS offset solve against the Windows branch (constants and
firstOrdinaryunchanged there, Linux doesn't compile the changed lines), the region-zone round-trip test and thelibc.mddebt entry. Merging through the queue; its Linux build covers the new test case.