fix(libc): convert local times before 1970 and after 3000 on Windows #4811
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4811"
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 milescoviello on 2026-10-12T22:39:40Z.
What
Fixes #4807. Carries @DavidSamir's fix branch from that issue (
DavidSamir:fix/libc-localtime-outside-1970-3000-windows,9cb4e363), keeping the commit and its authorship; it cherry-picks onto currentmainwithout changes.On Windows,
localtime,localtime_s,mktimeandctimefailed for every time before 1970 and after about January 3001, because they call the C runtime'slocaltime_sandmktime, which only accept that range:localtimereturned NULL,mktime-1 andctime??? ??? ?? ??:??:?? ????, where the console and the Linux build convert.docs/dev/debt/libc.mdrecorded the gap. The Gregorian calendar repeats every 400 years (146097 days, a whole number of weeks), so when the C runtime refuses a time,localtime_sconverts the same moment a whole number of cycles away inside 1970 to 2370 and moves the year back, andmktimedoes the reverse. The path is chosen before the one C runtime call, from the time value forlocaltime_sand from the broken-down time formktime, so no call is retried after an error anderrnois not saved and restored; a conversion inside the range is made by the same single call as before. The debt entry now records what remains: a time outside the range gets the daylight-saving rule the C runtime applies to the year it is converted in. Linux is unchanged.guest_time_conversiongainsCheckLocalRange(ten times from year 1 to 9999 throughlocaltime_s,localtime,mktimeandctime, normalisation across 1969/1970 and 3100/3101,localtime_s(INT64_MIN)still failing), andguest_mktimeloses its three Windows-only exceptions.Tested
main'sTime.cppboth tests fail and with the fix both pass 5/5; a probe of 2,360,002 conversions under nineTZsettings has 1,374,526 failures withmain's libraries and none with the branch's, everylocaltimematchinggmtimeplustm_gmtoffand everymktimeround-tripping; eleven catalog titles end the same way with both; fullctest1001/1001; no measurable cost inside the range.main0c3dc9ca.guest_time_conversion,guest_mktime,guest_utime,guest_rtc,guest_file_timestampsandguest_freebsd_clockspass, and all 324guest_*tests pass in two runs. The new checks also pass withmain'sTime.cpp, as expected since only the Windows path changes. I could not run Windows myself.tools/check_conventions.py --base upstream/mainpasses. The debt entry it rewrites sits next to the one my #4681 adds indocs/dev/debt/libc.md, so whichever of the two lands second needs a one-line rebase, which I'll do.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T23:07:25Z.
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/prx/libc/src/Time.cppL277-280, L289-290docs/dev/debt/libc.mdL54-55Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T01:45:16Z.
The branch in #4807 has been updated to 9cb4e3630:
mktimeno longer calls the C runtime a second time after a failure, it chooses the path from the broken-down time before the one call (the rule from the review of #4532). Details and the rerun results are in my comment on #4807. This PR still carries a133bd4db; could you take the new commit?AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-13T01:50:16Z.
Updated #4811 to your 9cb4e3630 (now
712609065on currentmain). Linux rechecked on the new version: all 324guest_*tests pass in two runs,guest_time_conversionandguest_mktimealso pass withmain'sTime.cpp, and a MinGW-w64-fsyntax-only -Wall -Wextracompile ofTime.cppis clean. The PR body now describes the single-call version.AI-assisted: yes