fix(libc): normalize a pre-1970 calendar before the Windows host mktime #12549
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12549"
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 JJenkx on 2026-10-28T19:11:55Z.
What
On Windows, host
mktimecan reject a calendar whose raw year is before 1969 even when its fields normalize into representable early 1970. Rebuild nonordinary fields from normalized UTC seconds before passing them to the host.The initial PR also computed a 400-year window shift for every such calendar. On macOS that could move a valid far-future calendar into a historical time zone: equivalent representations of 3520-10-22 18:22:37 in Pacific/Kiritimati differed by 88,800 seconds. The review follow-up keeps
moved = 0for in-range dates and computes the cycle shift only when the normalized timestamp is actually outside the host range. The Windows early-year normalization remains in place.Add equivalent-calendar tests for years 2320, 2390 and 3520 through both exported entry points. POSIX also exercises America/New_York and Pacific/Kiritimati; both hosts exercise EST5EDT, and the existing fixed-zone/early-year regressions remain. These tests compare equivalent calendars and normalized fields rather than assume one host's historical offset.
Integration with #10409: its current head already produces identical conflict blocks in four files against
main319621ba2eas against this updated PR. Our normalization change adds no conflict block, and does not replace its separate 36-byte GuestTm ABI fix. Author coordination requested: proposed order is #12549 first, then #10409 refreshed while retaining the normalization guard/tests. Author confirmation remains pending; no agreement is presumed.Tested
Exact updated head
5d02c43cc567d638269e1ae818fd7030cc1a32a7, based onmain319621ba2e578bd053b0baba2877fe70520e0737. This is a normal fast-forward functional follow-up, with no rebase or merge commits.mktime refused a calendar that normalizes into 1970Groups:
guest_mktime,guest_time_conversion,guest_time_extras,guest_strftime,guest_system_service_time_zoneandguest_utime. All ran as actual local project test executables. The patched library is built separately; tests link the unpatched library from the same source.Fresh SDK comparison: each arm uses the original study's 15 fixed inputs and the same 20,000 deterministic random calendars, with process-local UTC0 and a verified zero epoch anchor. The two SDK modules run natively on Linux; main, previous PR and updated DLLs each run in separate Windows processes. The probe uses canonical GuestTm storage with guard bytes, compares only the nine guest fields and timestamp, and reuses no old scratch executable. It does not change the system time zone.
Main-to-PR results change in nine rows; six become exact SDK matches, and no previously matching main row becomes a mismatch. The 6,651 residual rows are pre-existing behavior: 6,650 explicit positive-DST-hint rows (host CRT subtracts an hour in UTC0, SDK ignores the hint) and the 1899 case where SDK returns -1. This is not a claim of full 20,015-case SDK equivalence. These original-study inputs cover early-year normalization; the separate tests cover far-future equivalent calendars.
macOS qualification limit: the reviewer independently measured the historical-zone problem on macOS. Our replay exposes the exact macOS branch of Time.cpp through private preprocessor substitutions while retaining Linux/glibc host types and timezone data. It reproduces the 88,800-second Kiritimati example and verifies the guard fix, but is not actual updated macOS runtime qualification. Please rerun the new head on macOS; no full macOS library result is claimed from hosted relinker jobs.
Source reproducer, guarded calendar rows, both-host builds/runtime logs, negative control, JUnit results and #10409 conflict comparison. SHA-256
19703ea57d4ce7cc73c19ec50d7457c759ff26105fa70d1e6dac16703f29c454(download verified). No SDK/game binaries or disassembly included. No game/GPU test, console execution or performance claim is made. Windows validation sources and builds were restored after the private control snapshots.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 MarcZX10 on 2026-10-28T19:43:26Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The calendar change is narrowly scoped, but its merge with open PR #10409 conflicts in
Time.cppandGuestMktime.cpp, and the checklist currently says there is no dependency.What I ran
ba5890f3c060c957b7469a9b8fd733fcab90cc9aagainst base319621ba2e578bd053b0baba2877fe70520e0737; merge tree6ac1c750f9e77ab1461cab334f9d14d8d40be88dis clean.git diff --check refs/review/current-main...refs/review/12549— pass, no output.git merge-tree --write-tree refs/review/10409 refs/review/12549; the combined merge reports content conflicts inTime.cpp,GuestMktime.cpp,GuestStrftime.cpp, andGuestTimeConversion.cpp.Results
Time.cppandGuestMktime.cppRaw combined-merge conflict output
Negative control
Time.cppFindings
Blocking
Time.cppand itsGuestMktime.cpptest. The repository contribution rules require agreeing which overlapping PR goes first and declaring it withDepends on. Please coordinate the order with #10409's author, update this checklist, and rebase/restack or otherwise resolve the combined changes. Re-run the focused tests after integration.Non-blocking
The one-line normalization condition is consistent with the reported Windows UCRT failure mode, and no separate code defect was established in this source review. Author-reported 20,015-calendar SDK comparison and focused Windows results were not independently reproduced here.
Sources
Not verified
I did not run Windows or macOS builds/tests, the 20,015-calendar comparison, the base negative control, or console behavior. Hosted macOS jobs were still pending at review time.
To get this to a good state
Coordinate and declare the integration order with #10409, then rerun the focused time suite and recheck the combined merge. I will review the updated head and repeat the relevant checks.
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T19:51:22Z.
Review: COMMENTED
I reviewed head
ba5890f3c0on macOS. The body says that platform was left to CI. The new condition does change macOS results, and for far-future dates some of them are wrong.Setup. macOS 26.2, Apple clang,
-arch x86_64under Rosetta,-O1 -fsanitize=address,undefined, the target's-include HostStandardStreams.hpp. Standalone build ofTime.cpp,Environment.cpp,Multibyte.cppwithGuestMktime.cpp.guest_mktimepasses 50/50 with no sanitizer reports, and also underTZ=UTC,America/New_YorkandPacific/Kiritimati. Withmain'sTime.cppit passes on macOS too. That's expected, since macOS already normalizes these calendars throughtimeOfLocalSeconds.Differential, head vs
main. A harness feeds both builds the same 40,000 random calendars (out-of-range fields, years -2000..3000,tm_isdst-1/0/1). I compared the returned seconds against Pythonzoneinfoas the oracle. Only rows where the oracle gives one unambiguous answer are counted.UTC0/UTC-2/UTC5America/New_YorkEurope/LondonAustralia/Lord_HowePacific/KiritimatiEvery regression is in a year ≥ 2300. Here is a minimal pair. These two calendars name the same instant, 3520-10-22 18:22:37:
On head, the second one comes back as 23 October 19:02:37, 88,800 s later, with
tm_gmtoff+14:00. That's the −10:40 offset Kiritimati used before 1979.Cause.
!ordinarynow also sends in-range calendars into the block, and that block still computesmovedas whole 400-year cycles. On macOSfirstOrdinaryis 1900-01-02. A year-3520 calendar is therefore moved into 1920 and converted there by the hostmktime, which applies the zone's 1920 rules.mainonly moved calendars that were actually outside the host range. Withordinaryfields, head is the same asmain(first row), so this only affects calendars with out-of-range fields.Suggested fix. Keep the new entry condition, but only move the window when it's really out of range:
An in-range calendar is then rebuilt from its normalized UTC fields with
moved = 0. UCRT gets ordinary fields, which is the point of this PR, and nothing gets moved across zone history. On macOS with this change:guest_mktime(head test) passes.mainin Kiritimati, plus seeds 11/12.mainin New York (8), London (5) and Lord Howe (3) are all cases wheremainwas wrong and the oracle agrees with the change. These aretm_isdst = -1calendars whose out-of-range fields land on a DST transition, which macOSmktimeresolves differently.movedleft out on Windows the early-1970 cases go through the same path as head, but I couldn't run that. Please rerun your 20,015-calendar comparison with the suggested change.I'd also add a test that exercises this: one non-normalized calendar in a far-future year in a zone whose pre-1970 offsets differ from its current ones, checking that it matches the normalized form. The current zones (
UTC0,UTC-2,UTC5) have no history, so this path can't show up in them.Gates.
check_conventions --base upstream/mainpasses andgit merge-treeagainstupstream/main319621ba2eis clean.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T19:55:45Z.
Review: CHANGES_REQUESTED
Thanks, the early-1970 Windows case is real and the one-line approach is right. Two things to fix:
core/libs/prx/libc/src/Time.cpp:571-572:!ordinarynow sends in-range calendars (with out-of-range fields) into the block that computesmovedas whole 400-year cycles.firstOrdinaryis 1900-01-02 on macOS, so a year-3520 calendar is moved into ~1920 and converted with that zone's historical rules. @rooklz measured it on macOS againstzoneinfo(40,000 random calendars): 0 changes inUTC0/UTC-2/UTC5, but regressions inPacific/Kiritimati,Australia/Lord_Howe,America/New_YorkandEurope/London, all in years ≥ 2300. Minimal pair:{1620, 9, 22, 18:22:37}and{1620, 21, -343, 18:22:37}name the same instant but head returns different times for the second. The same window shift applies on Windows for years pastfirstOrdinary+ 400 years. Fix: keep the new entry condition and only computemovedwhenapproximate < firstOrdinary || approximate > HOST_LAST_SECOND - SECONDS_PER_DAY; otherwisemoved = 0. @rooklz reports this removes the differences on macOS. Please rerun your 20,015-calendar Windows comparison with it.core/libs/tests/GuestMktime.cpp:61: the zones the test runs (UTC0,UTC-2,UTC5) have no history, so this path can't fail there. Add a case with a far-future non-normalized calendar in a zone whose old offsets differ from its current ones (for examplePacific/KiritimatiorAmerica/New_York), checking it equals the normalized form's result.Time.cppandGuestMktime.cppand the combined merge conflicts. Please agree the order with its author and declare it underDepends on.