libc: localtime, mktime and ctime fail before 1970 and after 3000 on Windows; fix branch ready #4807
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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-12T22:30:30Z.
On Windows,
localtime,localtime_s,mktimeandctimefail for every time before 1970 and for every time after about January 3001.localtimereturns NULL,mktimereturns -1 andctimereturns??? ??? ?? ??:??:?? ????. The Linux build converts all of them. A title that takes a date of birth, a file time of -1, or a timestamp it has not filled in yet throughlocaltimegets a null pointer back.This is the gap docs/dev/debt/libc.md records: "outside that range
localtimereturns null andmktimereturns -1, where the console converts". #4277 wrote the limitation intoguest_mktime("Windows pre-epoch support remains as documented in TechnicalDebt"). This is a fix for it.Cause.
localtime_s_nid_postfixandlibc_mktime_nid_postfixcall the C runtime'slocaltime_sandmktime, which only accept 1970 to 3000.Observed on
main0428328b7, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release,TZ=UTC-2:localtime(-1)localtime(-2208988800)localtime(40000000000)mktimeof 1969-06-15 12:00:00mktimeof 1970-01-01 00:00:00 (two hours before the epoch in this zone)mktimeof year 70, month -1, day 1mktimeof 3100-06-15 12:00:00ctime(-86400)??? ??? ?? ??:??:?? ????I have not found a title that stops on this. It came out of running one probe of the libc functions on the Windows build and on the Linux build and comparing the two outputs.
Fix, ready to cherry-pick.
DavidSamir:fix/libc-localtime-outside-1970-3000-windows(a133bd4db, one commit onmain0428328b7, four files, +73/-22). I can't open it as a PR while #4532 is open (one open PR without a merged one).Windows only. The Gregorian calendar repeats every 400 years: 146097 days, a whole number of weeks, so the same dates fall on the same weekdays and the same years are leap years. When the C runtime refuses a time,
localtime_sconverts the same moment a whole number of those cycles away, inside 1970 to 2370, and moves the year back;mktimedoes the reverse, moving the year into that window and the result back. A conversion the C runtime accepts today is made by the same single call as before, so nothing changes inside 1970 to 3000. When a conversion outside the range succeeds,errnois left as it was.What this does not give: the zone's historical daylight-saving rules. A time outside the range gets the rule the C runtime applies to the year it is converted in, which is how the C runtime treats years inside the range too. The entry in
docs/dev/debt/libc.mdis rewritten to say that.Not changed:
strftime's%s, which calls the host'smktimedirectly and still prints -1 outside the range. #4543 rewrites that function, so I left it alone; it is one line to route through the same conversion once that lands.Tests:
guest_time_conversiongainsCheckLocalRange: for ten times from year 1 to year 9999,localtime_sandlocaltimemust succeed with the fieldsgmtimegives for the time plus the offset,mktimemust turn them back into the same time with the same fields,ctimemust not return question marks anderrnomust stay 0;mktimemust normalize month -1 of 1970 to December 1969 and month 17 of 3100 to June 3101;localtime_s(INT64_MIN)must still fail.guest_mktimeloses its three Windows exceptions: the negative times and theUTC5zone it skipped on Windows now run there, and the check that 1969-12-31 23:59:59 fails on Windows is removed.Checked on Windows (a133bd4db on
main0428328b7, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):Time.cppas onmainand the tests kept,guest_time_conversionandguest_mktimeboth fail (Local conversion failed). With the fix both pass 5/5 (--repeat until-fail:5), andguest_utime,guest_utime_forwarding,guest_rtc,guest_file_timestamps,guest_freebsd_clocksandguest_gameupdatepass 3/3.mktimeof the 15th of every month in every seventh year from 1500 to 3600), run under nine settings ofTZ(unset,UTC,UTC-2,UTC5,UTC+12,UTC-14,PST8PDT,EST5EDT,NZST-12NZDT): withmain's libraries 1,374,526 conversions fail in each; with the branch's none fail, everylocaltimehas the fieldsgmtimegives for the time plustm_gmtoff, everymktimereturns the timelocaltimewas given, and consecutive seconds stay in order across each boundary. No conversion inside 1970 to 3000 fails in either.TZ: 60 to 63 lines differ betweenmainand the branch, and every one of them is a NULL, a -1 or a row of question marks onmain. UnderUTC,UTC-2,UTC5,UTC+12andUTC-14the branch's fields equal the Linux build's on every line except at the two ends of the range anintyear can hold (about 2,147,483,647 years either way): there a time whose local year still fits converts on the branch and fails on Linux, and the failures leaveEINVALwhere Linux leaves 75, which is #4015's subject.gmtime,strftime,strtod,strtol, the character classes, the multibyte conversions, the environment,snprintf,sscanf, the clocks), run with both sets of libraries: three lines differ, themktimeandlocaltimecalls before 1970.localtime_sand half a millionlocaltime_splusmktime, nine passes each in three alternating runs:localtime_s38 to 43 ns withmain's libraries and 39 to 45 ns with the branch's; the pair 119 to 148 ns and 127 to 154 ns, about 8 ns more, which ismktimereadingerrnoso it can put it back.pollin both (#3644). I do not know whether any of them converts a time outside the range.ctest --parallel 4: 1001 of 1001 pass (7 skipped, no display).tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchlists #4015 for two places inTime.cpp(it setsEOVERFLOWwhere these functions still fail) and #4543 and #4681 for the neighbouring lines ofdocs/dev/debt/libc.md; none of them changes the range.mainhas moved toee4962190since; the branch merges onto it without conflict, and I have not rebuilt there.Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
guest_time_conversionandguest_mktimepass 3/3, so the new checks match the existing Linux behaviour. Only those targets andlibswere built there.Not checked on a console or with a commercial title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T22:39:49Z.
Carried in #4811, keeping your commit and authorship. I added the full Linux run (Ubuntu 24.04, GCC 13.3): all 324
guest_*tests pass in two runs, and the newCheckLocalRangeand theguest_mktimechanges also pass withmain'sTime.cpp, so Linux is unchanged. The Windows results in the PR are yours from this issue. Thanks for the branch.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T01:45:14Z.
An update before this is reviewed: the branch is now 9cb4e3630 (was a133bd4db), still one commit on
main0428328b7, four files, +94/-22.The first version made
mktimecall the C runtime and, when that returned -1, call it again 400 years away. @oneandonlydean's review of #4532 says a second attempt after an error is not allowed and the path has to be chosen before the call, somktimeno longer does that:gmtimeuses, on the fields as given). When that is more than a day inside 1970 to 3000, it makes the one C runtime call it makes today. Otherwise it converts the same fields a whole number of 400-year cycles away, normalized first, and moves the result back. One call either way, and its error stands.localtime_schooses the same way from the time value (inside 0 to 32535215999, the C runtime's last second, or not). It never called twice in the version that was posted.int,mktimereturns -1 and leaves the caller's fields alone, as onmain.errnoany more, because no call fails on the way to a success.guest_time_conversiongains one more check:mktimeof day -36500 of January 1970 must give -3153693600 with normalized fields.Rechecked on Windows with 9cb4e3630, same machine and method as in the issue:
Time.cppas onmain,guest_time_conversionandguest_mktimefail; with the fix both pass 5/5, and the six neighbouring tests pass 3/3.TZsettings: no conversion fails, no field differs fromgmtimeplus the offset, everymktimereturns the timelocaltimewas given. The 85 single conversions: every line that changes againstmainwas a failure there, and under the five fixed-offset zones the fields equal the Linux build's except at the two ends of theintyear range, as before.main, the same three.localtime_s40 to 46 ns withmain's libraries and 39 to 49 ns with the branch's;localtime_splusmktime118 to 169 ns and 123 to 156 ns. The 8 ns the first version added tomktimeis gone.ctest --parallel 4: 1001 of 1001 pass.tools/check_conventions.py --base origin/mainpasses.Linux (WSL2 Ubuntu 24.04, GCC 13.3.0):
guest_time_conversionandguest_mktimepass 3/3 on 9cb4e3630.@milescoviello, #4811 carries the first version; the branch above is the one to take.
AI-assisted: yes