fix(libc): format strftime and asctime fields as measured on a PS5 #10129

Merged
orezraey merged 19 commits from upstream-pr/10129 into main 2026-10-25 01:02:09 +00:00
orezraey commented 2026-10-23 10:35:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-23T10:35:12Z.


What

strftime and asctime measured on a retail PS5 (firmware 13.60) with five homebrew probes calling libSceLibcInternal.sprx from a payload process. The sources and console logs are in this gist (strf to strf4).

strftime was FreeBSD's; the console's is a different implementation. It now follows the console:

Input Console Before
%k, %l, %P, %s, %+, %v, %Q and any other unknown conversion copied as written (%k) formatted as FreeBSD (%k → 13), or the letter alone (%Q → Q)
flags and widths (%-d, %_d, %0e, %^a, %#b, %10Y) copied as written applied
E or O before a conversion ignored (%Ec = %c); before an unknown one dropped (%EE → %E, %Ek → %k) the same for known ones; %EE → EE
a negative weekday or month Sun or Jan, also for %A and %B; %m 00 ?
weekday 7+ or month 12+ empty name; %m 13, 14, …; %u and %w the value mod 10 ?
a negative numeric field (%d, %e, %H, %I, %M, %S, %j, %U, %W) 0 the negative number
a field of 100 or more wraps at 100 (%j at 1000): %H of 101 is 01 printed in full
%I (hour + 11) % 12 + 1, so hour -1 is 11 and -12 is 00 hour % 12 or 12
%Y, %G the year mod 10000, and 0000 before year 0 the full year, signed
%y, %g the last two digits, also for negative years (year -1 → 99) the same, with a sign in places
%C tm_year / 100 + 19 mod 100 (year 1 → 01, year 10001 → 00) the year / 100
%U, %W the weekday is (tm_wday + 7) % 7 and (tm_wday + 6) % 7 with the C remainder, so weekday 9 counts as 2 and -19 as -5 from tm_wday as given
%z the guest zone's offset, one hour later when tm_isdst is positive; tm_gmtoff ignored, also printed for tm_isdst -1 tm_gmtoff, nothing for tm_isdst -1
%Z tzname[tm_isdst > 0]; tm_zone ignored tm_zone, else tzname
a conversion that does not fit not written at all (ab%Ycd in 5 bytes writes only ab) and the call returns 0; a composite is written part by part (%t%D in 8 bytes leaves \t10/02/) written up to the limit

%c is %a %b %e %H:%M:%S %Y, %x and %D are %m/%d/%y, and %r is %I:%M:%S %p, as before.

asctime uses the same field rules: "%s %s%3d %02d:%02d:%02d %04d\n" with those names, day, time and year (Fri Oct 56 00:61:61 2026 for day 123456 and hour -10, year 10000 as 0000). Before, the year was printed in full and an out-of-range name was ???.

Not followed:

  • The console crashed the probe on a format ending in %E or %O; here that throws.
  • %G, %g and %V throw for a negative tm_yday. The console gave %V 01, 00, 00 and 52 for tm_yday -25, -13, -12 and -1 with tm_wday 5, and 52 for -10 with tm_wday 4; neither FreeBSD's ISO-week loop nor the variants I tried reproduce these, so the rule is unknown. They also throw for a tm_yday past the end of the year and a tm_wday below -6: tm_yday 400 of 2026 rolls into week 01 of 2027, but 415 of 1997 stays week 59 and 365 of 2013 week 53, and tm_wday -12 with tm_yday 18 is week 04 where FreeBSD's loop gives 03 (JJenkx, ntamas94). Every ISO case inside the year with tm_wday from -6 up matches, including the year boundaries. The ISO-week arithmetic is now 64-bit, so out-of-range fields no longer overflow.
  • %Z prints tzname. The console's tzname and %Z were both empty in the payload process; AnyPS5's tzname keeps the host's names (separate debt entry).

The debt entries record all of these, and that fegetexceptflag with a mask wider than 0x3f, which now also stores MXCSR control bits, was not measured.

The fetestexcept change moved to #10175 and the memmem/memccpy note to #10176, as requested in review.

Tested

  • Head after merging main (f26434afa3, with #10706): 6b1962e978. The test takes its declarations from libc/include/Api.hpp. guest_crt, guest_mktime, guest_strftime, guest_time_conversion, guest_time_extras and guest_wide_formatting pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); public_headers audit and check_conventions pass.
    Windows 11, MinGW (winlibs), at head c1ce36f9ee: this branch with current main (938812df14) merged in and #10134's commit 2151929559 kept.
  • guest_strftime is rewritten from the console logs: the conversions, the alternative forms, weekday and month -3 to 14, fields from -25 to 123456, years from -12345 to 12345, 14 ISO-week cases, the buffer limits and asctime. On main it reports 47 mismatches.
  • guest_time_conversion now checks the console's handling of the unknown and modified conversions, in place of FreeBSD's.
  • Since 974e0a89f2, guest_strftime also checks %U and %W on the ten out-of-range rows JJenkx and ntamas94 posted and the two composite rows that do not fit (%t%D in 8 bytes, %X%#H in 2); with the Time.cpp before that commit it reports 5 failures. The unknown ISO cases are not pinned in tests (removed in 68a4482acd at oneandonlydean's request); they still throw in Time.cpp with their debt entry.
  • guest_strftime, guest_time_conversion, guest_wide_formatting, guest_crt, guest_mktime and guest_time_extras pass three runs in a row. git diff --check is clean.
  • tools/check_conventions.py passes.

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 under Silent stubs in their library's technical debt file
  • 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: #10134, #6713, #8937, #10113, #10226, #10345 (both edit the same libc.md section; this branch carries its commit until it merges); #6713, #8937, #10113, #10226, #10345 edit the same lines too (merge order only); #9613 edits the same lines too (merge order only); #10706 removes the test declarations this PR adds to GuestStrftime.cpp (merge order only); #10742 adds an entry next to the same debt line (merge order only)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4981439573 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10129). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-23T10:35:12Z. --- ### What `strftime` and `asctime` measured on a retail PS5 (firmware 13.60) with five homebrew probes calling `libSceLibcInternal.sprx` from a payload process. The sources and console logs are in [this gist](https://gist.github.com/orezraey/d3a3271c811a0d5cb6294cf73dd67564) (`strf` to `strf4`). **`strftime` was FreeBSD's; the console's is a different implementation.** It now follows the console: | Input | Console | Before | |---|---|---| | `%k`, `%l`, `%P`, `%s`, `%+`, `%v`, `%Q` and any other unknown conversion | copied as written (`%k`) | formatted as FreeBSD (`%k` → `13`), or the letter alone (`%Q` → `Q`) | | flags and widths (`%-d`, `%_d`, `%0e`, `%^a`, `%#b`, `%10Y`) | copied as written | applied | | `E` or `O` before a conversion | ignored (`%Ec` = `%c`); before an unknown one dropped (`%EE` → `%E`, `%Ek` → `%k`) | the same for known ones; `%EE` → `EE` | | a negative weekday or month | `Sun` or `Jan`, also for `%A` and `%B`; `%m` 00 | `?` | | weekday 7+ or month 12+ | empty name; `%m` 13, 14, …; `%u` and `%w` the value mod 10 | `?` | | a negative numeric field (`%d`, `%e`, `%H`, `%I`, `%M`, `%S`, `%j`, `%U`, `%W`) | 0 | the negative number | | a field of 100 or more | wraps at 100 (`%j` at 1000): `%H` of 101 is `01` | printed in full | | `%I` | `(hour + 11) % 12 + 1`, so hour -1 is `11` and -12 is `00` | `hour % 12` or 12 | | `%Y`, `%G` | the year mod 10000, and 0000 before year 0 | the full year, signed | | `%y`, `%g` | the last two digits, also for negative years (year -1 → `99`) | the same, with a sign in places | | `%C` | `tm_year / 100 + 19` mod 100 (year 1 → `01`, year 10001 → `00`) | the year / 100 | | `%U`, `%W` | the weekday is `(tm_wday + 7) % 7` and `(tm_wday + 6) % 7` with the C remainder, so weekday 9 counts as 2 and -19 as -5 | from `tm_wday` as given | | `%z` | the guest zone's offset, one hour later when `tm_isdst` is positive; `tm_gmtoff` ignored, also printed for `tm_isdst` -1 | `tm_gmtoff`, nothing for `tm_isdst` -1 | | `%Z` | `tzname[tm_isdst > 0]`; `tm_zone` ignored | `tm_zone`, else `tzname` | | a conversion that does not fit | not written at all (`ab%Ycd` in 5 bytes writes only `ab`) and the call returns 0; a composite is written part by part (`%t%D` in 8 bytes leaves `\t10/02/`) | written up to the limit | `%c` is `%a %b %e %H:%M:%S %Y`, `%x` and `%D` are `%m/%d/%y`, and `%r` is `%I:%M:%S %p`, as before. `asctime` uses the same field rules: `"%s %s%3d %02d:%02d:%02d %04d\n"` with those names, day, time and year (`Fri Oct 56 00:61:61 2026` for day 123456 and hour -10, year 10000 as `0000`). Before, the year was printed in full and an out-of-range name was `???`. **Not followed:** - The console crashed the probe on a format ending in `%E` or `%O`; here that throws. - `%G`, `%g` and `%V` throw for a negative `tm_yday`. The console gave `%V` 01, 00, 00 and 52 for `tm_yday` -25, -13, -12 and -1 with `tm_wday` 5, and 52 for -10 with `tm_wday` 4; neither FreeBSD's ISO-week loop nor the variants I tried reproduce these, so the rule is unknown. They also throw for a `tm_yday` past the end of the year and a `tm_wday` below -6: `tm_yday` 400 of 2026 rolls into week 01 of 2027, but 415 of 1997 stays week 59 and 365 of 2013 week 53, and `tm_wday` -12 with `tm_yday` 18 is week 04 where FreeBSD's loop gives 03 ([JJenkx](https://github.com/boykopovar/AnyPS5/pull/10129#issuecomment-6375836309), [ntamas94](https://github.com/boykopovar/AnyPS5/pull/10129#issuecomment-6376980726)). Every ISO case inside the year with `tm_wday` from -6 up matches, including the year boundaries. The ISO-week arithmetic is now 64-bit, so out-of-range fields no longer overflow. - `%Z` prints `tzname`. The console's `tzname` and `%Z` were both empty in the payload process; AnyPS5's `tzname` keeps the host's names (separate debt entry). The debt entries record all of these, and that `fegetexceptflag` with a mask wider than 0x3f, which now also stores MXCSR control bits, was not measured. The `fetestexcept` change moved to #10175 and the `memmem`/`memccpy` note to #10176, as requested in review. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Head after merging `main` (`f26434afa3`, with #10706): `6b1962e978`. The test takes its declarations from `libc/include/Api.hpp`. `guest_crt`, `guest_mktime`, `guest_strftime`, `guest_time_conversion`, `guest_time_extras` and `guest_wide_formatting` pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); `public_headers` audit and `check_conventions` pass. Windows 11, MinGW (winlibs), at head `c1ce36f9ee`: this branch with current `main` (`938812df14`) merged in and #10134's commit `2151929559` kept. - `guest_strftime` is rewritten from the console logs: the conversions, the alternative forms, weekday and month -3 to 14, fields from -25 to 123456, years from -12345 to 12345, 14 ISO-week cases, the buffer limits and `asctime`. On `main` it reports 47 mismatches. - `guest_time_conversion` now checks the console's handling of the unknown and modified conversions, in place of FreeBSD's. - Since `974e0a89f2`, `guest_strftime` also checks `%U` and `%W` on the ten out-of-range rows JJenkx and ntamas94 posted and the two composite rows that do not fit (`%t%D` in 8 bytes, `%X%#H` in 2); with the `Time.cpp` before that commit it reports 5 failures. The unknown ISO cases are not pinned in tests (removed in `68a4482acd` at oneandonlydean's request); they still throw in `Time.cpp` with their debt entry. - `guest_strftime`, `guest_time_conversion`, `guest_wide_formatting`, `guest_crt`, `guest_mktime` and `guest_time_extras` pass three runs in a row. `git diff --check` is clean. - `tools/check_conventions.py` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: #10134, #6713, #8937, #10113, #10226, #10345 (both edit the same `libc.md` section; this branch carries its commit until it merges); #6713, #8937, #10113, #10226, #10345 edit the same lines too (merge order only); #9613 edits the same lines too (merge order only); #10706 removes the test declarations this PR adds to `GuestStrftime.cpp` (merge order only); #10742 adds an entry next to the same debt line (merge order only) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-23 10:51:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T10:51:10Z.

Review: CHANGES_REQUESTED


The linked probe reports substantive strftime/asctime differences and the test matrix is broad. GCC 12.2 syntax checks passed for the changed source and tests; exact-worktree conventions, diff-check, and main-sync also pass (6 ahead/29 behind; merge clean). The author reports seven related Windows CTests passing three times; I did not run the full CTest suite locally. Please split the independent fetestexcept implementation/test/debt change into a separate topic PR from the strftime/asctime implementation, as required by the repository’s one-branch-per-topic rule. The non-exported memmem/memccpy note is also separate from this formatter behavior and should move to a focused documentation change if retained. In addition, coordinate the debt-file integration with #10134: the pairwise heads conflict in the guest-time-conversion / __isfinitel section. Refresh the formatter branch, rerun its focused tests and overlap/convention checks, and retain the measured probe caveats.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627220839 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10129#pullrequestreview-5627220839). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T10:51:10Z. **Review:** CHANGES_REQUESTED --- The linked probe reports substantive strftime/asctime differences and the test matrix is broad. GCC 12.2 syntax checks passed for the changed source and tests; exact-worktree conventions, diff-check, and main-sync also pass (6 ahead/29 behind; merge clean). The author reports seven related Windows CTests passing three times; I did not run the full CTest suite locally. Please split the independent `fetestexcept` implementation/test/debt change into a separate topic PR from the `strftime`/`asctime` implementation, as required by the repository’s one-branch-per-topic rule. The non-exported `memmem`/`memccpy` note is also separate from this formatter behavior and should move to a focused documentation change if retained. In addition, coordinate the debt-file integration with #10134: the pairwise heads conflict in the guest-time-conversion / `__isfinitel` section. Refresh the formatter branch, rerun its focused tests and overlap/convention checks, and retain the measured probe caveats.
MarcZX10 commented 2026-10-23 13:23:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T13:23:30Z.

Review: CHANGES_REQUESTED


Re-review of current head ed75f2fee18f75f03c79dfb2c68b5824b06ea049. The unrelated fetestexcept and memmem work has been split out, and this PR now declares Depends on: #10134; the remaining changed files are the formatter, its tests and the debt note. Exact-head conventions and git diff --check pass, and the author reports the focused formatter tests passing three consecutive runs after the refresh.

The PR is currently CONFLICTING / DIRTY against current main, so the current merged tree cannot be built or reviewed as a landing candidate. Please rebase onto current main, preserve the #10134 prerequisite and both sets of debt evidence, rerun the focused formatter/time tests, and update the report to the resulting exact head. This is the remaining integration blocker; no closure is requested.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5628841186 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10129#pullrequestreview-5628841186). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T13:23:30Z. **Review:** CHANGES_REQUESTED --- Re-review of current head `ed75f2fee18f75f03c79dfb2c68b5824b06ea049`. The unrelated `fetestexcept` and `memmem` work has been split out, and this PR now declares `Depends on: #10134`; the remaining changed files are the formatter, its tests and the debt note. Exact-head conventions and `git diff --check` pass, and the author reports the focused formatter tests passing three consecutive runs after the refresh. The PR is currently `CONFLICTING` / `DIRTY` against current `main`, so the current merged tree cannot be built or reviewed as a landing candidate. Please rebase onto current `main`, preserve the #10134 prerequisite and both sets of debt evidence, rerun the focused formatter/time tests, and update the report to the resulting exact head. This is the remaining integration blocker; no closure is requested.
MarcZX10 commented 2026-10-23 13:56:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T13:56:24Z.

Review: APPROVED


Re-review of exact current head c1ce36f9eea92e4333e945172593911573299754.

The prior blockers are resolved: fetestexcept and the unrelated memmem/memccpy note are split into separate PRs; the branch now declares its #10134 dependency; and it is clean against current main (938812df14). The remaining diff is limited to strftime/asctime, focused tests, and the corresponding libc debt note. The measured PS5 behavior and the explicitly unmeasured cases are documented in the PR.

I independently ran tools/check_conventions.py on this exact head, git diff --check, and GCC C++20 -Wall -Wextra -Werror -fsyntax-only on the changed Time.cpp; all passed. The author reports six focused Windows CTests passing three consecutive times. Current hosted eligibility, conventions, labels, report, relinker (Ubuntu/Windows/macOS including x86_64), ASan relinker, register-context, and write-watch checks pass. Linux and Windows full product build jobs were skipped by path policy; I did not run the six CTests locally.

The #10134 dependency is explicit and its commit is present in this branch as described. Approving this exact head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5629248383 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10129#pullrequestreview-5629248383). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T13:56:24Z. **Review:** APPROVED --- Re-review of exact current head `c1ce36f9eea92e4333e945172593911573299754`. The prior blockers are resolved: `fetestexcept` and the unrelated `memmem`/`memccpy` note are split into separate PRs; the branch now declares its #10134 dependency; and it is clean against current `main` (`938812df14`). The remaining diff is limited to `strftime`/`asctime`, focused tests, and the corresponding libc debt note. The measured PS5 behavior and the explicitly unmeasured cases are documented in the PR. I independently ran `tools/check_conventions.py` on this exact head, `git diff --check`, and GCC C++20 `-Wall -Wextra -Werror -fsyntax-only` on the changed `Time.cpp`; all passed. The author reports six focused Windows CTests passing three consecutive times. Current hosted eligibility, conventions, labels, report, relinker (Ubuntu/Windows/macOS including x86_64), ASan relinker, register-context, and write-watch checks pass. Linux and Windows full product build jobs were skipped by path policy; I did not run the six CTests locally. The #10134 dependency is explicit and its commit is present in this branch as described. Approving this exact head.
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!10129
No description provided.