Repository navigation
fix(date): support %s, %F, %T and other GNU format specifiers - #214
Conversation
|
@strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. I found no blocking issues. Every specifier you added matches GNU date byte for byte (TZ=UTC LC_ALL=C) across 223 timestamps, including the AM/PM boundaries, leap days, day 366, and 2100 (not a leap year). %%Y no longer gets expanded a second time, and plain date prints the same as before.
Non-blocking question: should %:z be added in this PR? Agents often write ISO-8601 timestamps as date '+%FT%T%:z', and that currently prints 2025-11-06T11:34:56%:z. A few other specifiers GNU supports also still print literally: %k %l %P %h %C %r %x %X %q %G %V %U. Most of them would be one more line in the match at src/commands/date.rs:177. Leaving them for a follow-up under #207 is fine too.
✅ What I verified
- Head
29966af.cargo test --lib date: 6 passed.cargo test --test shell_integration date: 8 passed.cargo fmt --checkis clean, and clippy reports no warnings indate.rs. - Compared
format_datewith GNU coreutils 9.1 on 223 timestamps for every specifier listed in the help text, plus%%Yand%%%Y: 0 mismatches. - I also ran the built
strands-shell -cbinary next to hostdate. Every specifier the PR claims matched at the same second. - The new tests check real behaviour. On base,
date_epoch_secondswould get a literal%s, anddate_percent_escapewould get%2025 100%%. - The help text lists exactly what was added. The README and bindings don't document
date.
Appendix: non-blocking (1)
- ⚪ This predates the PR:
%cincludesUTC(date.rs:183-186), while GNU+%cin the C locale printsThu Nov 6 11:34:56 2025. Plaindaterelies on this format and matches GNU, so changing it would need a separate default-format path. I didn't file an issue for it because it's minor.
Description
date +FORMATonly knew a handful of specifiers and expanded them one at a time, so anything else came out wrong without any error.date +%s, which people use all the time for timing, printed a literal%s. And because expanded text got re-scanned,%%Yturned into the year instead of%Y:Now the format string is expanded in a single pass and covers the common GNU specifiers (
%s,%F,%T,%N,%j,%A,%B,%z,%Z,%%and friends). Unknown ones are printed as-is, like GNU does.date,date -uand the specifiers that worked before give the same output as before.I compared the output against GNU
datein UTC for fixed timestamps, including edge cases like midnight, Sundays and day 366 of a leap year. I left out GNU's padding flags like%-dand-dparsing, those can come separately.Related Issues
Part of #207
Documentation PR
N/A
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.