fix(libc): convert times into the guest's 56-byte tm on Windows #1606
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1606"
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 mohdfaour03 on 2026-10-08T14:14:12Z.
What
gmtime,localtime,mktime,strftimeandasctimein libc took and returned the host'sstd::tm. The guest's FreeBSDstruct tmis 56 bytes: the nine calendarints, thenlong tm_gmtoffat offset 40 andchar* tm_zoneat offset 48. On Linux glibc'sstd::tmhas the same layout, so nothing was wrong there. On Windowsstd::tmis 36 bytes, so:gmtime/localtimereturn points at a 36-byte thread-local; a title readingtm_gmtoffortm_zonereads 16 bytes past it;gmtime_s/localtime_snever writetm_gmtoffandtm_zoneinto the guest's buffer;gmtime_sonly accepts 1969-12-31 12:00:00 to 3001-01-19 21:59:59 UTC (measured on this host), sogmtime(-86400)returned null where the console converts it.The change adds
GuestTm(core/libs/prx/libc/include/GuestTime.hpp, withstatic_asserts on the offsets and size) and converts through it on both hosts:gmtime/gmtime_scompute the UTC fields from the day count (Hinnant'scivil_from_days), so every time whose year fits anintconverts;tm_gmtoffis 0 andtm_zoneis"UTC", which is what the FreeBSD libc reports (glibc'sgmtime_rsays"GMT").localtime/localtime_scopy the host's calendar fields and filltm_gmtoffandtm_zonefrom the CRT on Windows (_get_timezone,_get_dstbias,_tzname) and from the hosttmon Linux.mktimenormalises through the host and then refills the guesttmwithlocaltime_s, sotm_gmtoff/tm_zoneare set as on the console.strftime/asctimecopy the guesttminto a hosttmbefore calling the host.On Windows
localtimeandmktimestill go through the CRT and keep its ~1970 to January 3001 range and daylight-saving rule; that gap is recorded in TechnicalDebt.gmtimehas no such limit any more.The test (
guest_time_conversion, already registered in ctest) now usesGuestTmand 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), thattm_gmtoff/tm_zoneare written over a0xAA-filled buffer,INT64_MINandINT64_MAXreturn null,tm_gmtoff == 7200forTZ=UTC-2, alocaltime→mktimeround trip that renormalisestm_wday/tm_ydayand sets the zone fields, andstrftimeon a guesttm.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.main'sTime.cpp, a small driver linking the same translation unit printssizeof(std::tm) = 36,gmtime(1969-01-01) -> nullptr, and aftergmtime_sinto a0xAA-filled 56-byte buffer thelongat offset 40 is still0xAAAAAAAAAAAAAAAA.g++ -std=c++20 -O2 -Wall -Wextra -Werror -pthreadpasses.cmake --build build --target libs, thenctest --test-dir build -R '^guest_time_conversion$' --repeat until-fail:3: passes 3/3 against the builtlibcDLL.ctest --test-dir build --output-on-failure --timeout 120: 439 passed, 13 skipped, 6 failed out of 445. The 6 failures are allagc_driver_*shader execution tests on this host's Intel integrated GPU (driver 32.0.101.8831):agc_driver_flat_storeandagc_driver_global_vcc_basetime out,agc_driver_sdwa_float_selectors,agc_driver_vop1_float_unary,agc_driver_vop3_mad_i64andagc_driver_vopc_comparefail.agc_driver_vop3_mad_i64andagc_driver_flat_storeon 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/mainandgit diff --checkpass.localtime_rand only replacesgmtime_rwith the pure-arithmetic conversion that the test covers on both hosts.Checklist
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)NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt — none added; the remaining Windowslocaltime/mktimerange is recorded 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 adriadam10 on 2026-10-10T00:20:16Z.
Checked on Linux: build + ctest of libs/libc green (
guest_time_conversionincluded; it doesn't build without the change). Layout asserts (size 56,tm_gmtoffat 40,tm_zoneat 48), the civil-from-days arithmetic and the Windowstm_gmtoffcomputation read correctly; Windows CI is green on this head.Added by us on top of a rebase onto current
main:main) and ended it with a period.Merging once CI is green on the rebased head.
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.
main)core/libs/prx/libc/src/Time.cppL10