libc: localtime, mktime and ctime fail before 1970 and after 3000 on Windows; fix branch ready #4807

Closed
opened 2026-10-12 22:30:30 +00:00 by DavidSamir · 2 comments
DavidSamir commented 2026-10-12 22:30:30 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-12T22:30:30Z.


On Windows, localtime, localtime_s, mktime and ctime fail for every time before 1970 and for every time after about January 3001. localtime returns NULL, mktime returns -1 and ctime returns ??? ??? ?? ??:??:?? ????. 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 through localtime gets a null pointer back.

This is the gap docs/dev/debt/libc.md records: "outside that range localtime returns null and mktime returns -1, where the console converts". #4277 wrote the limitation into guest_mktime ("Windows pre-epoch support remains as documented in TechnicalDebt"). This is a fix for it.

Cause. localtime_s_nid_postfix and libc_mktime_nid_postfix call the C runtime's localtime_s and mktime, which only accept 1970 to 3000.

Observed on main 0428328b7, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, TZ=UTC-2:

call Windows Linux build
localtime(-1) NULL 1970-01-01 01:59:59
localtime(-2208988800) NULL 1900-01-01 02:00:00
localtime(40000000000) NULL 3237-07-20 01:06:40
mktime of 1969-06-15 12:00:00 -1 -17244000
mktime of 1970-01-01 00:00:00 (two hours before the epoch in this zone) -1 -7200
mktime of year 70, month -1, day 1 -1 -2685600, normalized to 1969-12-01
mktime of 3100-06-15 12:00:00 -1 35673645600
ctime(-86400) ??? ??? ?? ??:??:?? ???? the date

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 on main 0428328b7, 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_s converts the same moment a whole number of those cycles away, inside 1970 to 2370, and moves the year back; mktime does 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, errno is 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.md is rewritten to say that.

Not changed: strftime's %s, which calls the host's mktime directly 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_conversion gains CheckLocalRange: for ten times from year 1 to year 9999, localtime_s and localtime must succeed with the fields gmtime gives for the time plus the offset, mktime must turn them back into the same time with the same fields, ctime must not return question marks and errno must stay 0; mktime must normalize month -1 of 1970 to December 1969 and month 17 of 3100 to June 3101; localtime_s(INT64_MIN) must still fail.
  • guest_mktime loses its three Windows exceptions: the negative times and the UTC5 zone 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 main 0428328b7, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):

  • With Time.cpp as on main and the tests kept, guest_time_conversion and guest_mktime both fail (Local conversion failed). With the fix both pass 5/5 (--repeat until-fail:5), and guest_utime, guest_utime_forwarding, guest_rtc, guest_file_timestamps, guest_freebsd_clocks and guest_gameupdate pass 3/3.
  • A probe that converts 2,360,002 times (about every 11 days from year -248 to year 10526, every second for 100,000 seconds either side of ten boundaries including 0, the two 400-year marks and the C runtime's limits, and mktime of the 15th of every month in every seventh year from 1500 to 3600), run under nine settings of TZ (unset, UTC, UTC-2, UTC5, UTC+12, UTC-14, PST8PDT, EST5EDT, NZST-12NZDT): with main's libraries 1,374,526 conversions fail in each; with the branch's none fail, every localtime has the fields gmtime gives for the time plus tm_gmtoff, every mktime returns the time localtime was given, and consecutive seconds stay in order across each boundary. No conversion inside 1970 to 3000 fails in either.
  • The same probe's 85 single conversions, printed field by field under each TZ: 60 to 63 lines differ between main and the branch, and every one of them is a NULL, a -1 or a row of question marks on main. Under UTC, UTC-2, UTC5, UTC+12 and UTC-14 the branch's fields equal the Linux build's on every line except at the two ends of the range an int year 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 leave EINVAL where Linux leaves 75, which is #4015's subject.
  • A probe of 431 lines over the libc functions (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, the mktime and localtime calls before 1970.
  • Timing inside the range, two million localtime_s and half a million localtime_s plus mktime, nine passes each in three alternating runs: localtime_s 38 to 43 ns with main'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 is mktime reading errno so it can put it back.
  • Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), PSXS5 (PPSA97510), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end at the same place in both, with the same number of stderr lines; PSXS5 stops on poll in both (#3644). I do not know whether any of them converts a time outside the range.
  • Full ctest --parallel 4: 1001 of 1001 pass (7 skipped, no display).
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch lists #4015 for two places in Time.cpp (it sets EOVERFLOW where these functions still fail) and #4543 and #4681 for the neighbouring lines of docs/dev/debt/libc.md; none of them changes the range. main has moved to ee4962190 since; 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_conversion and guest_mktime pass 3/3, so the new checks match the existing Linux behaviour. Only those targets and libs were built there.

Not checked on a console or with a commercial title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5821347816 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4807). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-12T22:30:30Z. --- On Windows, `localtime`, `localtime_s`, `mktime` and `ctime` fail for every time before 1970 and for every time after about January 3001. `localtime` returns NULL, `mktime` returns -1 and `ctime` returns `??? ??? ?? ??:??:?? ????`. 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 through `localtime` gets a null pointer back. This is the gap [docs/dev/debt/libc.md](https://github.com/boykopovar/AnyPS5/blob/0428328b7/docs/dev/debt/libc.md?plain=1#L53) records: "outside that range `localtime` returns null and `mktime` returns -1, where the console converts". #4277 wrote the limitation into `guest_mktime` ("Windows pre-epoch support remains as documented in TechnicalDebt"). This is a fix for it. **Cause.** [`localtime_s_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/0428328b7/core/libs/prx/libc/src/Time.cpp#L275-L296) and [`libc_mktime_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/0428328b7/core/libs/prx/libc/src/Time.cpp#L310-L318) call the C runtime's `localtime_s` and `mktime`, which only accept 1970 to 3000. Observed on `main` 0428328b7, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, `TZ=UTC-2`: | call | Windows | Linux build | |---|---|---| | `localtime(-1)` | NULL | 1970-01-01 01:59:59 | | `localtime(-2208988800)` | NULL | 1900-01-01 02:00:00 | | `localtime(40000000000)` | NULL | 3237-07-20 01:06:40 | | `mktime` of 1969-06-15 12:00:00 | -1 | -17244000 | | `mktime` of 1970-01-01 00:00:00 (two hours before the epoch in this zone) | -1 | -7200 | | `mktime` of year 70, month -1, day 1 | -1 | -2685600, normalized to 1969-12-01 | | `mktime` of 3100-06-15 12:00:00 | -1 | 35673645600 | | `ctime(-86400)` | `??? ??? ?? ??:??:?? ????` | the date | 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`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libc-localtime-outside-1970-3000-windows) (a133bd4db, one commit on `main` 0428328b7, 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_s` converts the same moment a whole number of those cycles away, inside 1970 to 2370, and moves the year back; `mktime` does 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, `errno` is 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.md` is rewritten to say that. Not changed: `strftime`'s `%s`, which calls the host's `mktime` directly 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_conversion` gains `CheckLocalRange`: for ten times from year 1 to year 9999, `localtime_s` and `localtime` must succeed with the fields `gmtime` gives for the time plus the offset, `mktime` must turn them back into the same time with the same fields, `ctime` must not return question marks and `errno` must stay 0; `mktime` must normalize month -1 of 1970 to December 1969 and month 17 of 3100 to June 3101; `localtime_s(INT64_MIN)` must still fail. - `guest_mktime` loses its three Windows exceptions: the negative times and the `UTC5` zone 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 `main` 0428328b7, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `Time.cpp` as on `main` and the tests kept, `guest_time_conversion` and `guest_mktime` both fail (`Local conversion failed`). With the fix both pass 5/5 (`--repeat until-fail:5`), and `guest_utime`, `guest_utime_forwarding`, `guest_rtc`, `guest_file_timestamps`, `guest_freebsd_clocks` and `guest_gameupdate` pass 3/3. - A probe that converts 2,360,002 times (about every 11 days from year -248 to year 10526, every second for 100,000 seconds either side of ten boundaries including 0, the two 400-year marks and the C runtime's limits, and `mktime` of the 15th of every month in every seventh year from 1500 to 3600), run under nine settings of `TZ` (unset, `UTC`, `UTC-2`, `UTC5`, `UTC+12`, `UTC-14`, `PST8PDT`, `EST5EDT`, `NZST-12NZDT`): with `main`'s libraries 1,374,526 conversions fail in each; with the branch's none fail, every `localtime` has the fields `gmtime` gives for the time plus `tm_gmtoff`, every `mktime` returns the time `localtime` was given, and consecutive seconds stay in order across each boundary. No conversion inside 1970 to 3000 fails in either. - The same probe's 85 single conversions, printed field by field under each `TZ`: 60 to 63 lines differ between `main` and the branch, and every one of them is a NULL, a -1 or a row of question marks on `main`. Under `UTC`, `UTC-2`, `UTC5`, `UTC+12` and `UTC-14` the branch's fields equal the Linux build's on every line except at the two ends of the range an `int` year 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 leave `EINVAL` where Linux leaves 75, which is #4015's subject. - A probe of 431 lines over the libc functions (`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, the `mktime` and `localtime` calls before 1970. - Timing inside the range, two million `localtime_s` and half a million `localtime_s` plus `mktime`, nine passes each in three alternating runs: `localtime_s` 38 to 43 ns with `main`'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 is `mktime` reading `errno` so it can put it back. - Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), PSXS5 (PPSA97510), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end at the same place in both, with the same number of stderr lines; PSXS5 stops on `poll` in both (#3644). I do not know whether any of them converts a time outside the range. - Full `ctest --parallel 4`: 1001 of 1001 pass (7 skipped, no display). - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` lists #4015 for two places in `Time.cpp` (it sets `EOVERFLOW` where these functions still fail) and #4543 and #4681 for the neighbouring lines of `docs/dev/debt/libc.md`; none of them changes the range. `main` has moved to ee4962190 since; 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_conversion` and `guest_mktime` pass 3/3, so the new checks match the existing Linux behaviour. Only those targets and `libs` were built there. Not checked on a console or with a commercial title. AI-assisted: yes
milescoviello commented 2026-10-12 22:39:49 +00:00 (Migrated from github.com)
Author
Contributor

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 new CheckLocalRange and the guest_mktime changes also pass with main's Time.cpp, so Linux is unchanged. The Windows results in the PR are yours from this issue. Thanks for the branch.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6133982458 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4807#issuecomment-6133982458). Originally posted by [milescoviello](https://github.com/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 new `CheckLocalRange` and the `guest_mktime` changes also pass with `main`'s `Time.cpp`, so Linux is unchanged. The Windows results in the PR are yours from this issue. Thanks for the branch. AI-assisted: yes
DavidSamir commented 2026-10-13 01:45:14 +00:00 (Migrated from github.com)
Author
Contributor

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 main 0428328b7, four files, +94/-22.

The first version made mktime call 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, so mktime no longer does that:

  • It works out from the broken-down time alone where the time lies (the same calendar arithmetic gmtime uses, 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_s chooses 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.
  • When the converted year does not fit an int, mktime returns -1 and leaves the caller's fields alone, as on main.
  • Nothing saves and restores errno any more, because no call fails on the way to a success.

guest_time_conversion gains one more check: mktime of day -36500 of January 1970 must give -3153693600 with normalized fields.

Rechecked on Windows with 9cb4e3630, same machine and method as in the issue:

  • With Time.cpp as on main, guest_time_conversion and guest_mktime fail; with the fix both pass 5/5, and the six neighbouring tests pass 3/3.
  • The 2,360,002-conversion probe under the nine TZ settings: no conversion fails, no field differs from gmtime plus the offset, every mktime returns the time localtime was given. The 85 single conversions: every line that changes against main was a failure there, and under the five fixed-offset zones the fields equal the Linux build's except at the two ends of the int year range, as before.
  • The 431-line libc probe: three lines differ from main, the same three.
  • Timing inside the range, nine passes each in three alternating runs: localtime_s 40 to 46 ns with main's libraries and 39 to 49 ns with the branch's; localtime_s plus mktime 118 to 169 ns and 123 to 156 ns. The 8 ns the first version added to mktime is gone.
  • The eleven catalog titles end at the same place with both sets of libraries, with the same number of stderr lines.
  • Full ctest --parallel 4: 1001 of 1001 pass. tools/check_conventions.py --base origin/main passes.

Linux (WSL2 Ubuntu 24.04, GCC 13.3.0): guest_time_conversion and guest_mktime pass 3/3 on 9cb4e3630.

@milescoviello, #4811 carries the first version; the branch above is the one to take.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136264361 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4807#issuecomment-6136264361). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T01:45:14Z. --- An update before this is reviewed: the branch is now 9cb4e3630 (was a133bd4db), still one commit on `main` 0428328b7, four files, +94/-22. The first version made `mktime` call 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, so `mktime` no longer does that: - It works out from the broken-down time alone where the time lies (the same calendar arithmetic `gmtime` uses, 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_s` chooses 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. - When the converted year does not fit an `int`, `mktime` returns -1 and leaves the caller's fields alone, as on `main`. - Nothing saves and restores `errno` any more, because no call fails on the way to a success. `guest_time_conversion` gains one more check: `mktime` of day -36500 of January 1970 must give -3153693600 with normalized fields. Rechecked on Windows with 9cb4e3630, same machine and method as in the issue: - With `Time.cpp` as on `main`, `guest_time_conversion` and `guest_mktime` fail; with the fix both pass 5/5, and the six neighbouring tests pass 3/3. - The 2,360,002-conversion probe under the nine `TZ` settings: no conversion fails, no field differs from `gmtime` plus the offset, every `mktime` returns the time `localtime` was given. The 85 single conversions: every line that changes against `main` was a failure there, and under the five fixed-offset zones the fields equal the Linux build's except at the two ends of the `int` year range, as before. - The 431-line libc probe: three lines differ from `main`, the same three. - Timing inside the range, nine passes each in three alternating runs: `localtime_s` 40 to 46 ns with `main`'s libraries and 39 to 49 ns with the branch's; `localtime_s` plus `mktime` 118 to 169 ns and 123 to 156 ns. The 8 ns the first version added to `mktime` is gone. - The eleven catalog titles end at the same place with both sets of libraries, with the same number of stderr lines. - Full `ctest --parallel 4`: 1001 of 1001 pass. `tools/check_conventions.py --base origin/main` passes. Linux (WSL2 Ubuntu 24.04, GCC 13.3.0): `guest_time_conversion` and `guest_mktime` pass 3/3 on 9cb4e3630. @milescoviello, #4811 carries the first version; the branch above is the one to take. AI-assisted: yes
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#4807
No description provided.