fix(libc): convert times into the guest's 56-byte tm on Windows #1606

Merged
mohdfaour03 merged 1 commit from upstream-pr/1606 into main 2026-10-10 01:14:41 +00:00
mohdfaour03 commented 2026-10-08 14:14:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mohdfaour03 on 2026-10-08T14:14:12Z.


What

gmtime, localtime, mktime, strftime and asctime in libc took and returned the host's std::tm. The guest's FreeBSD struct tm is 56 bytes: the nine calendar ints, then long tm_gmtoff at offset 40 and char* tm_zone at offset 48. On Linux glibc's std::tm has the same layout, so nothing was wrong there. On Windows std::tm is 36 bytes, so:

  • the pointer gmtime/localtime return points at a 36-byte thread-local; a title reading tm_gmtoff or tm_zone reads 16 bytes past it;
  • gmtime_s/localtime_s never write tm_gmtoff and tm_zone into the guest's buffer;
  • UCRT's gmtime_s only accepts 1969-12-31 12:00:00 to 3001-01-19 21:59:59 UTC (measured on this host), so gmtime(-86400) returned null where the console converts it.

The change adds GuestTm (core/libs/prx/libc/include/GuestTime.hpp, with static_asserts on the offsets and size) and converts through it on both hosts:

  • gmtime/gmtime_s compute the UTC fields from the day count (Hinnant's civil_from_days), so every time whose year fits an int converts; tm_gmtoff is 0 and tm_zone is "UTC", which is what the FreeBSD libc reports (glibc's gmtime_r says "GMT").
  • localtime/localtime_s copy the host's calendar fields and fill tm_gmtoff and tm_zone from the CRT on Windows (_get_timezone, _get_dstbias, _tzname) and from the host tm on Linux.
  • mktime normalises through the host and then refills the guest tm with localtime_s, so tm_gmtoff/tm_zone are set as on the console.
  • strftime/asctime copy the guest tm into a host tm before calling the host.

On Windows localtime and mktime still go through the CRT and keep its ~1970 to January 3001 range and daylight-saving rule; that gap is recorded in TechnicalDebt. gmtime has no such limit any more.

The test (guest_time_conversion, already registered in ctest) now uses GuestTm and additionally checks: 1969-12-31 23:59:59, 1969-01-01, 2000-02-29, 3001-01-01, 9999-12-31 and 0001-01-01 (calendar fields, tm_wday, tm_yday), that tm_gmtoff/tm_zone are written over a 0xAA-filled buffer, INT64_MIN and INT64_MAX return null, tm_gmtoff == 7200 for TZ=UTC-2, a localtime → mktime round trip that renormalises tm_wday/tm_yday and sets the zone fields, and strftime on a guest tm.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 (15.2.0posix-14.0.0-ucrt-r7), x86-64, Release, BUILD_TESTING=ON.

  • Against main's Time.cpp, a small driver linking the same translation unit prints sizeof(std::tm) = 36, gmtime(1969-01-01) -> nullptr, and after gmtime_s into a 0xAA-filled 56-byte buffer the long at offset 40 is still 0xAAAAAAAAAAAAAAAA.
  • The new test compiled standalone with g++ -std=c++20 -O2 -Wall -Wextra -Werror -pthread passes.
  • Full build, cmake --build build --target libs, then ctest --test-dir build -R '^guest_time_conversion$' --repeat until-fail:3: passes 3/3 against the built libc DLL.
  • Full ctest --test-dir build --output-on-failure --timeout 120: 439 passed, 13 skipped, 6 failed out of 445. The 6 failures are all agc_driver_* shader execution tests on this host's Intel integrated GPU (driver 32.0.101.8831): agc_driver_flat_store and agc_driver_global_vcc_base time out, agc_driver_sdwa_float_selectors, agc_driver_vop1_float_unary, agc_driver_vop3_mad_i64 and agc_driver_vopc_compare fail. agc_driver_vop3_mad_i64 and agc_driver_flat_store on Intel are #1246 and #1256; none of them call libc's time functions. The libc tests that do (file_position, guest_standard_streams, guest_filesystem) pass.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.
  • Not run on Linux (no Linux host here); the Linux path keeps localtime_r and only replaces gmtime_r with the pure-arithmetic conversion that the test covers on both hosts.
  • No title or homebrew run.

Checklist

  • Based on current main; no other open PR implements the same functions (searched PRs for gmtime, localtime, mktime, strftime, tm_gmtoff; #1107 is merged and only fixed the race)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt — none added; the remaining Windows localtime/mktime range is recorded in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source — none added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4788131329 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1606). Originally posted by [mohdfaour03](https://github.com/mohdfaour03) on 2026-10-08T14:14:12Z. --- ### What `gmtime`, `localtime`, `mktime`, `strftime` and `asctime` in libc took and returned the host's `std::tm`. The guest's FreeBSD `struct tm` is 56 bytes: the nine calendar `int`s, then `long tm_gmtoff` at offset 40 and `char* tm_zone` at offset 48. On Linux glibc's `std::tm` has the same layout, so nothing was wrong there. On Windows `std::tm` is 36 bytes, so: - the pointer `gmtime`/`localtime` return points at a 36-byte thread-local; a title reading `tm_gmtoff` or `tm_zone` reads 16 bytes past it; - `gmtime_s`/`localtime_s` never write `tm_gmtoff` and `tm_zone` into the guest's buffer; - UCRT's `gmtime_s` only accepts 1969-12-31 12:00:00 to 3001-01-19 21:59:59 UTC (measured on this host), so `gmtime(-86400)` returned null where the console converts it. The change adds `GuestTm` (`core/libs/prx/libc/include/GuestTime.hpp`, with `static_assert`s on the offsets and size) and converts through it on both hosts: - `gmtime`/`gmtime_s` compute the UTC fields from the day count (Hinnant's `civil_from_days`), so every time whose year fits an `int` converts; `tm_gmtoff` is 0 and `tm_zone` is `"UTC"`, which is what the FreeBSD libc reports (glibc's `gmtime_r` says `"GMT"`). - `localtime`/`localtime_s` copy the host's calendar fields and fill `tm_gmtoff` and `tm_zone` from the CRT on Windows (`_get_timezone`, `_get_dstbias`, `_tzname`) and from the host `tm` on Linux. - `mktime` normalises through the host and then refills the guest `tm` with `localtime_s`, so `tm_gmtoff`/`tm_zone` are set as on the console. - `strftime`/`asctime` copy the guest `tm` into a host `tm` before calling the host. On Windows `localtime` and `mktime` still go through the CRT and keep its ~1970 to January 3001 range and daylight-saving rule; that gap is recorded in TechnicalDebt. `gmtime` has no such limit any more. The test (`guest_time_conversion`, already registered in ctest) now uses `GuestTm` and additionally checks: 1969-12-31 23:59:59, 1969-01-01, 2000-02-29, 3001-01-01, 9999-12-31 and 0001-01-01 (calendar fields, `tm_wday`, `tm_yday`), that `tm_gmtoff`/`tm_zone` are written over a `0xAA`-filled buffer, `INT64_MIN` and `INT64_MAX` return null, `tm_gmtoff == 7200` for `TZ=UTC-2`, a `localtime` → `mktime` round trip that renormalises `tm_wday`/`tm_yday` and sets the zone fields, and `strftime` on a guest `tm`. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 (`15.2.0posix-14.0.0-ucrt-r7`), x86-64, Release, `BUILD_TESTING=ON`. - Against `main`'s `Time.cpp`, a small driver linking the same translation unit prints `sizeof(std::tm) = 36`, `gmtime(1969-01-01) -> nullptr`, and after `gmtime_s` into a `0xAA`-filled 56-byte buffer the `long` at offset 40 is still `0xAAAAAAAAAAAAAAAA`. - The new test compiled standalone with `g++ -std=c++20 -O2 -Wall -Wextra -Werror -pthread` passes. - Full build, `cmake --build build --target libs`, then `ctest --test-dir build -R '^guest_time_conversion$' --repeat until-fail:3`: passes 3/3 against the built `libc` DLL. - Full `ctest --test-dir build --output-on-failure --timeout 120`: 439 passed, 13 skipped, 6 failed out of 445. The 6 failures are all `agc_driver_*` shader execution tests on this host's Intel integrated GPU (driver 32.0.101.8831): `agc_driver_flat_store` and `agc_driver_global_vcc_base` time out, `agc_driver_sdwa_float_selectors`, `agc_driver_vop1_float_unary`, `agc_driver_vop3_mad_i64` and `agc_driver_vopc_compare` fail. `agc_driver_vop3_mad_i64` and `agc_driver_flat_store` on Intel are #1246 and #1256; none of them call libc's time functions. The libc tests that do (`file_position`, `guest_standard_streams`, `guest_filesystem`) pass. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - Not run on Linux (no Linux host here); the Linux path keeps `localtime_r` and only replaces `gmtime_r` with the pure-arithmetic conversion that the test covers on both hosts. - No title or homebrew run. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (searched PRs for gmtime, localtime, mktime, strftime, tm_gmtoff; #1107 is merged and only fixed the race) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) — none added; the remaining Windows `localtime`/`mktime` range is recorded in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source — none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-10 00:20:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T00:20:16Z.


Checked on Linux: build + ctest of libs/libc green (guest_time_conversion included; it doesn't build without the change). Layout asserts (size 56, tm_gmtoff at 40, tm_zone at 48), the civil-from-days arithmetic and the Windows tm_gmtoff computation read correctly; Windows CI is green on this head.

Added by us on top of a rebase onto current main:

  • Moved the TechnicalDebt line out of the end of "Functional" (everyone appends there; it conflicted with main) and ended it with a period.

Merging once CI is green on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6091530066 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1606#issuecomment-6091530066). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T00:20:16Z. --- Checked on Linux: build + ctest of libs/libc green (`guest_time_conversion` included; it doesn't build without the change). Layout asserts (size 56, `tm_gmtoff` at 40, `tm_zone` at 48), the civil-from-days arithmetic and the Windows `tm_gmtoff` computation read correctly; Windows CI is green on this head. Added by us on top of a rebase onto current `main`: - Moved the TechnicalDebt line out of the end of "Functional" (everyone appends there; it conflicted with `main`) and ended it with a period. Merging once CI is green on the rebased head.
github-actions[bot] commented 2026-10-10 00:54:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T00:54:34Z.


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.

Pull request Conflicting files (lines on main) Same exports Same new files
#545 core/libs/prx/libc/src/Time.cpp L10
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6091861781 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1606#issuecomment-6091861781). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T00:54:34Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #545 | `core/libs/prx/libc/src/Time.cpp` [L10](https://github.com/boykopovar/AnyPS5/blob/7703fda15e4ab4025dd817eae5964315e6dd134b/core/libs/prx/libc/src/Time.cpp#L10) | | |
Sign in to join this conversation.
No reviewers
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!1606
No description provided.