fix(libc): format strftime and asctime as the PS5 libc does #5540

Closed
hugodearaujo wants to merge 1 commit from upstream-pr/5540 into main
hugodearaujo commented 2026-10-13 23:16:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hugodearaujo on 2026-10-13T23:16:01Z.


What

strftime and asctime followed FreeBSD 11. A PS5's libSceLibcInternal.sprx (firmware 13.60) has a C99 formatter with different rules, and 118 of 175 format and date pairs gave a different result. I measured the console's functions from a payload, over about 9,200 formats and broken-down times, then rewrote both functions to its rules.

PS5 main
conversions outside C99 (%k, %l, %s, %v, %+, %P, flags such as %-d, %_d, %5Y, %^a) copied as written: %k FreeBSD extensions expanded, other letters written without the %
E and O accepted before any conversion and ignored (%Ea is %a); before an unknown one, %E] gives %] only on some conversions
%z the system zone, plus an hour when tm_isdst is positive from tm_year 86, and tm_gmtoff ignored: -0300 here tm_gmtoff: +0100
%Z always empty tm_zone
a field out of range clamped at 0 and cut to the field width (tm_hour 100 is 00, tm_year 8100 is 0000); a weekday or month outside its table is an empty name, the first name when negative printed as is, ? names
a buffer that is too small each piece (a conversion, or one literal character) is copied only if it fits whole; the call then returns 0 and keeps what it wrote partial pieces
asctime %c and a line feed (" Jan 2 03:04:05 2025\n" for tm_wday 7) "??? ..." names, five-digit years

Common formats with valid dates (%c, %Y-%m-%d, %H:%M:%S, week numbers of real dates) were already right and stay unchanged.

Two console results are not reproduced and are listed in the debt:

  • for tm_year below 0, the console's %z copies unrelated memory (text from its error-message table, up to the buffer size);
  • for tm_wday or tm_yday outside their range, its ISO week (%G, %g, %V) differs.

A format that ends in %E or %O makes the console loop forever, so I could only measure it with a watchdog thread. AnyPS5 writes the two characters and returns instead.

Tested

  • PS5 (firmware 13.60): payloads resolve strftime and asctime by NID in libSceLibcInternal.sprx:
    • every %c, %Ec and %Oc over printable characters;
    • each tm field swept out of range, over all conversions and asctime;
    • week numbers around every new year from 1995 to 2032;
    • a 7,200-case ISO grid;
    • %z from 1960 to 2040 with and without daylight saving;
    • every buffer size up to 26 bytes.
  • A host build of the same cases against this branch matches all 9,174 measurements outside the two cases above. Restricted to in-range tm_wday and tm_yday, the 931 ISO cases all match.
  • Linux: full CTest 1072/1073. guest_strftime and guest_time_conversion now hold the console's results; guest_strftime fails with the old Time.cpp. agc_driver_tessellation segfaults the same way on main.
  • Windows Server 2025 VM, WinLibs GCC 15.2, CI configure flags: CTest 1100 tests with no failures (the GPU tests skip in this VM).
  • Merged cleanly with main and the 13 open PRs that touch Time.cpp, its tests or docs/dev/debt/libc.md.
  • ASTRO BOT (PPSA21567) imports one of these functions, but no title I have was seen calling them, so this is console evidence only.

Checklist

  • Based on current main; no other open PR implements the same functions
  • 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
  • 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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4850684395 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5540). Originally posted by [hugodearaujo](https://github.com/hugodearaujo) on 2026-10-13T23:16:01Z. --- ### What `strftime` and `asctime` followed FreeBSD 11. A PS5's `libSceLibcInternal.sprx` (firmware 13.60) has a C99 formatter with different rules, and 118 of 175 format and date pairs gave a different result. I measured the console's functions from a payload, over about 9,200 formats and broken-down times, then rewrote both functions to its rules. | | PS5 | `main` | |---|---|---| | conversions outside C99 (`%k`, `%l`, `%s`, `%v`, `%+`, `%P`, flags such as `%-d`, `%_d`, `%5Y`, `%^a`) | copied as written: `%k` | FreeBSD extensions expanded, other letters written without the `%` | | `E` and `O` | accepted before any conversion and ignored (`%Ea` is `%a`); before an unknown one, `%E]` gives `%]` | only on some conversions | | `%z` | the system zone, plus an hour when `tm_isdst` is positive from `tm_year` 86, and `tm_gmtoff` ignored: `-0300` here | `tm_gmtoff`: `+0100` | | `%Z` | always empty | `tm_zone` | | a field out of range | clamped at 0 and cut to the field width (`tm_hour` 100 is `00`, `tm_year` 8100 is `0000`); a weekday or month outside its table is an empty name, the first name when negative | printed as is, `?` names | | a buffer that is too small | each piece (a conversion, or one literal character) is copied only if it fits whole; the call then returns 0 and keeps what it wrote | partial pieces | | `asctime` | `%c` and a line feed (`" Jan 2 03:04:05 2025\n"` for `tm_wday` 7) | `"??? ..."` names, five-digit years | Common formats with valid dates (`%c`, `%Y-%m-%d`, `%H:%M:%S`, week numbers of real dates) were already right and stay unchanged. Two console results are not reproduced and are listed in the debt: - for `tm_year` below 0, the console's `%z` copies unrelated memory (text from its error-message table, up to the buffer size); - for `tm_wday` or `tm_yday` outside their range, its ISO week (`%G`, `%g`, `%V`) differs. A format that ends in `%E` or `%O` makes the console loop forever, so I could only measure it with a watchdog thread. AnyPS5 writes the two characters and returns instead. ### Tested - PS5 (firmware 13.60): payloads resolve `strftime` and `asctime` by NID in `libSceLibcInternal.sprx`: - every `%c`, `%Ec` and `%Oc` over printable characters; - each `tm` field swept out of range, over all conversions and `asctime`; - week numbers around every new year from 1995 to 2032; - a 7,200-case ISO grid; - `%z` from 1960 to 2040 with and without daylight saving; - every buffer size up to 26 bytes. - A host build of the same cases against this branch matches all 9,174 measurements outside the two cases above. Restricted to in-range `tm_wday` and `tm_yday`, the 931 ISO cases all match. - Linux: full CTest 1072/1073. `guest_strftime` and `guest_time_conversion` now hold the console's results; `guest_strftime` fails with the old `Time.cpp`. `agc_driver_tessellation` segfaults the same way on `main`. - Windows Server 2025 VM, WinLibs GCC 15.2, CI configure flags: CTest 1100 tests with no failures (the GPU tests skip in this VM). - Merged cleanly with `main` and the 13 open PRs that touch `Time.cpp`, its tests or `docs/dev/debt/libc.md`. - ASTRO BOT (PPSA21567) imports one of these functions, but no title I have was seen calling them, so this is console evidence only. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed 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 - [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)
oneandonlydean commented 2026-10-14 04:42:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T04:42:32Z.


Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161062217 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5540#issuecomment-6161062217). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T04:42:32Z. --- Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!5540
No description provided.