libc: strftime returns 0 and writes nothing for the FreeBSD conversions %k, %l, %s, %v and %+ on Windows #3106

Closed
opened 2026-10-10 22:18:01 +00:00 by husseinkad · 0 comments
husseinkad commented 2026-10-10 22:18:01 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T22:18:01Z.


What

The guest strftime (core/libs/prx/libc/src/Time.cpp) passes the format to the Windows CRT, which does not know %k, %l, %s, %v and %+: the call returns 0 and writes nothing. FreeBSD's strftime(3) documents all five:

  • %k is the hour (24-hour clock), blank-padded; %l the hour (12-hour clock), blank-padded
  • %s is the seconds since the Epoch: mktime of the broken-down time in the local time zone, as in FreeBSD 9 and 11 (current FreeBSD uses tm_gmtoff), -1 if it cannot be represented
  • %v is %e-%b-%Y
  • %+ is %a %b %e %H:%M:%S %Z %Y in the C locale, with %Z taken from the broken-down time's tm_zone (or tzname[tm_isdst != 0] when it is null)

Reproduce

Windows 11, WinLibs GCC 15.2.0 r7, main at the time of writing: strftime(buffer, 64, "%k|%l", &tm) returns 0 for a valid tm (FreeBSD: for hour 22, 22|10).

Fix

A tested fix is on my fork: husseinkad:fix/libc-time-strftime-freebsd-conversions (five commits on main; PR #2702, closed by the intake check): Time.cpp expands the five conversions itself before calling the host strftime. E and O modifiers in front of them are accepted as in FreeBSD's _fmt(); %%k stays literal and %E%/%O% are copied as one unit so the next character stays literal; a format without any of the five goes to the host untouched; the caller's tm is never modified. What is still different (padding flags on the new conversions, an unknown conversion or a trailing lone %, a doubled modifier, %P, a standalone %z/%Z using the host zone) is recorded in TechnicalDebt; closed PR #2665 was about %z/%Z.

guest_time_conversion covers all five conversions, hours 0/12/13 and single-digit padding, exact fit and one byte short, %%k/%%%k, %E%k and friends, the E/O forms, a tm_zone that differs from the host's, a null tm_zone, and %s against mktime; it fails without the change. Differential runs against main's Time.cpp built from the same sources: about 3 million format/tm/size cases (7 time-zone settings, extreme tm values, buffer sizes 0 to 255 and 4 MB formats) against an independent FreeBSD strftime reference: 0 differences for formats without the new conversions, no overrun, the caller's tm unchanged, about 52,500 cases fixed; and all 111,111 formats of length 0 to 5 over %EOkls+vZa against a reference written from FreeBSD's _fmt() (read at releng/9.3, 11.0, 13.2 and 14.0): 0 mismatches. Linux not run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795777254 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3106). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T22:18:01Z. --- ### What The guest `strftime` (`core/libs/prx/libc/src/Time.cpp`) passes the format to the Windows CRT, which does not know `%k`, `%l`, `%s`, `%v` and `%+`: the call returns 0 and writes nothing. FreeBSD's `strftime(3)` documents all five: - `%k` is the hour (24-hour clock), blank-padded; `%l` the hour (12-hour clock), blank-padded - `%s` is the seconds since the Epoch: `mktime` of the broken-down time in the local time zone, as in FreeBSD 9 and 11 (current FreeBSD uses `tm_gmtoff`), `-1` if it cannot be represented - `%v` is `%e-%b-%Y` - `%+` is `%a %b %e %H:%M:%S %Z %Y` in the C locale, with `%Z` taken from the broken-down time's `tm_zone` (or `tzname[tm_isdst != 0]` when it is null) ### Reproduce Windows 11, WinLibs GCC 15.2.0 r7, `main` at the time of writing: `strftime(buffer, 64, "%k|%l", &tm)` returns 0 for a valid `tm` (FreeBSD: for hour 22, `22|10`). ### Fix A tested fix is on my fork: [`husseinkad:fix/libc-time-strftime-freebsd-conversions`](https://github.com/husseinkad/AnyPS5/tree/fix/libc-time-strftime-freebsd-conversions) (five commits on `main`; PR #2702, closed by the intake check): `Time.cpp` expands the five conversions itself before calling the host `strftime`. `E` and `O` modifiers in front of them are accepted as in FreeBSD's `_fmt()`; `%%k` stays literal and `%E%`/`%O%` are copied as one unit so the next character stays literal; a format without any of the five goes to the host untouched; the caller's `tm` is never modified. What is still different (padding flags on the new conversions, an unknown conversion or a trailing lone `%`, a doubled modifier, `%P`, a standalone `%z`/`%Z` using the host zone) is recorded in TechnicalDebt; closed PR #2665 was about `%z`/`%Z`. `guest_time_conversion` covers all five conversions, hours 0/12/13 and single-digit padding, exact fit and one byte short, `%%k`/`%%%k`, `%E%k` and friends, the `E`/`O` forms, a `tm_zone` that differs from the host's, a null `tm_zone`, and `%s` against `mktime`; it fails without the change. Differential runs against `main`'s `Time.cpp` built from the same sources: about 3 million format/tm/size cases (7 time-zone settings, extreme `tm` values, buffer sizes 0 to 255 and 4 MB formats) against an independent FreeBSD `strftime` reference: 0 differences for formats without the new conversions, no overrun, the caller's `tm` unchanged, about 52,500 cases fixed; and all 111,111 formats of length 0 to 5 over `%EOkls+vZa` against a reference written from FreeBSD's `_fmt()` (read at releng/9.3, 11.0, 13.2 and 14.0): 0 mismatches. Linux not run. 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#3106
No description provided.