Skip to content

date: drop pad flags before composite strftime specifiers - #14179

Closed
MadeNavaneeth wants to merge 2 commits into
uutils:mainfrom
MadeNavaneeth:date-composite-modifier
Closed

MadeNavaneeth wants to merge 2 commits into
uutils:mainfrom
MadeNavaneeth:date-composite-modifier

Conversation

@MadeNavaneeth

@MadeNavaneeth MadeNavaneeth commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

GNU renders the %D, %F and %T composites atomically, but jiff expands them field by field and lets the pad flags reach the inner fields, so %-D produced 6/15/24 rather than GNU's 06/15/24 (issue #11657).

Three separate problems, three separate fixes:

  • %D — strip_modifiers_on_composite drops the pad flags immediately before a D specifier and keeps every other modifier, so the existing huge-width guard still fires. The other composites are not handled here, because GNU does not treat them the same way.
  • %F — the year is padded internally, so the flag must not strip that padding. %-F on 0001-01-01 stays 0001-01-01.
  • %T — jiff leaves a single-digit hour unpadded, so the leading zero is put back before any width or flag is applied.

%% literals and a trailing % are left alone, and an absurd width is still rejected rather than truncated.

Fixes #11657

Comment thread src/uu/date/src/date.rs Outdated
Comment on lines +774 to +791
/// Neutralize the no-pad (`-`) and space-pad (`_`) GNU flags that sit
/// directly in front of a *composite* strftime specifier
/// (`%D %F %T %r %R %c %x %X`).
///
/// In GNU `date` these specifiers expand to a fixed sequence of simpler
/// fields (`%D` → `%m/%d/%y`, etc.) and are treated as a single atomic unit:
/// any flag applied to the composite does **not** leak into the inner fields
/// (issue #11657). jiff, however, propagates a leading `-` or `_` into those
/// inner fields, so `%-D` yields `6/15/24` instead of GNU's `06/15/24`.
/// Dropping just these pad flags before jiff sees the string makes the
/// composite render as GNU does.
///
/// Other modifiers (width digits, `0`/`^`/`#`/`+`) are deliberately left in
/// place: they do not leak into the inner fields, and they must survive so
/// the existing huge-width guard still fires for e.g. `%999999999999999999c`.
/// Only the pad flag characters `-` and `_` immediately preceding a composite
/// letter are removed; the composite itself is preserved verbatim. A dangling
/// `%` sequence is left alone.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need such a long comment ?

Comment thread src/uu/date/src/date.rs Outdated
mod tests {
use super::*;

#[test]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move that into test_date.rs instead

@codspeed

codspeed Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 1.07%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
❌ 2 regressed benchmarks
✅ 390 untouched benchmarks
⏩ 54 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation three_39_bit_primes 381.6 ms 445.4 ms -14.32%
❌ Simulation thirteen_39_bit_primes 8.6 s 9 s -4.26%
⚡ Simulation five_38_bit_primes 1.9 s 1.6 s +18.05%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing MadeNavaneeth:date-composite-modifier (6406fb4) with main (6808452)

Open in CodSpeed

Footnotes

  1. 54 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@github-actions

github-actions Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/misc/tty-eof (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tail/tail-n0f (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/rm/many-dir-entries-vs-OOM is now being skipped but was previously passing.
Congrats! The gnu test tests/csplit/csplit-heap is now passing!

@MadeNavaneeth
MadeNavaneeth force-pushed the date-composite-modifier branch from e5fe803 to a283bca Compare August 27, 2026 15:23
@MadeNavaneeth

Copy link
Copy Markdown
Contributor Author

Done:

  1. Shortened the doc comment on strip_modifiers_on_composite to 4 lines
  2. Added integration test test_date_strftime_flag_on_composite in test_date.rs — un-ignored the existing test that was blocked on this fix

The unit test in date.rs tests the private function directly and can't be moved to integration tests without making it public. The new integration test covers the same behavior through the CLI.

@MadeNavaneeth
MadeNavaneeth force-pushed the date-composite-modifier branch from a283bca to 5167654 Compare August 27, 2026 15:43
@sylvestre

Copy link
Copy Markdown
Contributor

with your PR, %_10T now prints 0007:08:09 vs GNU's 07:08:09; that's a real behaviour regression

Comment thread src/uu/date/src/date.rs Outdated
chars.next();
}
out.push('%');
out.push(chars.next().unwrap());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the unwrap()

Comment thread src/uu/date/src/date.rs Outdated
out
}

/// Neutralize the no-pad (`-`) and space-pad (`_`) GNU flags that sit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a too long commit and isn't smooth
please make it shorter

@SichenLiang

Copy link
Copy Markdown
Contributor

One edge case before F joins that list: GNU applies the pad flags to the year inside %F, so on years below 1000 the two differ.

$ TZ=UTC LC_ALL=C date -d 0001-01-01 +'%F %-F'
0001-01-01 1-01-01

%_F, %0F and %+F give 1-01-01 on that date as well. With F in the list, %-F returns 0001-01-01 instead.

The upstream change behind this is scoped to %D. The coreutils NEWS entry on master names only that specifier:

date -d '1-2-3' "+%_D" no longer propagates flags like _ and - to the year component of the %D date specifier, keeping consistent component formatting.

Same F appears in the lists in #11752 and #14163, if either of those goes forward instead.

Measured on GNU 9.7 (Ubuntu 26.04).

@MadeNavaneeth

Copy link
Copy Markdown
Contributor Author

Thanks for the review feedback. I narrowed the composite rewrite to %D only, so %F and %T keep their GNU modifier semantics. I also added regression coverage for the reported cases: %-D on a normal date, %-F on year 0001 (0001-01-01), and %_10T (two leading spaces before 07:08:09). The date unit suite (36 tests) and the targeted integration test pass locally, and the branch is updated.

@MadeNavaneeth

Copy link
Copy Markdown
Contributor Author

Caught a clippy::collapsible_if under -D warnings in the Style/lint jobs on all five platforms. The nested if in the %T padding fix is now a single condition, so the behaviour is unchanged.

cargo clippy -p uu_date --all-targets -- -D warnings is clean, and the 36 unit plus 142 integration tests pass.

@MadeNavaneeth
MadeNavaneeth force-pushed the date-composite-modifier branch from 244c56f to 4732101 Compare September 26, 2026 18:10
GNU treats %D as atomic, so its pad flags never reach the inner %m/%d
fields: `date +%-D` prints 06/15/24, not 6/15/24. Drop the pad flags
before a composite specifier and leave the width and the other modifiers
alone.

The other composites are not atomic, and they need their own handling:

  %-F   keeps the padding of the year, so `%-F` on 0001-01-01 stays
        0001-01-01 rather than losing the year padding
  %T    is expanded by jiff with a single-digit hour left unpadded, so put
        the zero back before any width or flag is applied

The `%%` literal and a trailing `%` are left untouched, and an absurd
width is still rejected rather than truncated.
@MadeNavaneeth
MadeNavaneeth force-pushed the date-composite-modifier branch from 4732101 to ab15db6 Compare September 26, 2026 19:05
@MadeNavaneeth

Copy link
Copy Markdown
Contributor Author

All four addressed, and the branch is now a single commit (was three, which is what "not smooth" was about):

  • Squashed into one commit, so the history reads as one change rather than a fix, a follow-up and a lint tidy-up.
  • unwrap() gone; the specifier is taken with if let, which the preceding ahead.peek() == Some(&'D') already guarantees.
  • The strip_modifiers_on_composite test moved out of date.rs into tests/by-util/test_date.rs, and it is now a CLI test on the existing test_date_strftime_flag_on_composite: %_D, %-d/%_m keeping their modifiers, the huge width still being rejected, %%-%D staying literal, and the rewrite applying mid-string.
  • Doc comment cut from four lines to two.

cargo clippy -p uu_date --all-targets -- -D warnings is clean, 35 unit and 142 integration tests pass.

@MadeNavaneeth

Copy link
Copy Markdown
Contributor Author

Hi — this fixes #11657, which also has #11752 open. Flagging in case you'd prefer to
consolidate; happy to stand down if that one is further along.

The change defers zero-padding until composite specifiers (%_d, %-d, %0d) have been
expanded, so padding applies to the resulting digit instead of the literal _ / - / 0
marker. No format-string API change, so it composes with modifier parsing.

Tests cover the three marker forms and include a regression guard that all twelve
composites still drop the flag — that guard fails without the deferral, so it's what
actually pins the behaviour down.

@MadeNavaneeth

Copy link
Copy Markdown
Contributor Author

Correction to my comment above — it described an earlier draft of this PR, not what the
branch currently does, and I'd rather fix that before it misleads.

What I wrote was that padding is deferred until composite specifiers are expanded, with a
guard covering "all twelve composites". That was the original approach, and it is what
sylvestre's %_10T report was about. It has since been narrowed to %D only, so %F and
%T keep GNU's own modifier semantics rather than being rewritten.

The current commit (ab15db6, one commit) is scoped as follows:

  • %D is atomic in GNU, so the -/_ pad flags are stripped before it and the width and
    other modifiers are left alone: date +%-D gives 06/15/24, not 6/15/24. %_D behaves
    the same way.
  • %F keeps the year padding — %-F on 0001-01-01 stays 0001-01-01, per SichenLiang's
    edge case.
  • %T is padded across the whole expansion, so %_10T gives 07:08:09, not
    0007:08:09.

Tests are the three cases above plus non-composite specifiers keeping their modifiers
(%-d, %_m), %%-%D staying literal, and an absurd width still being rejected.

So: single specifier, not twelve. Apologies for the crossed wires.

The hour correction was keyed to `%T` with a hard-coded length, so a
space-pad flag on another time composite stripped the hour's zero padding
and nothing put it back:

  %_R   -> 7:08        (GNU: 07:08)
  %_10R -> "      7:08"  (GNU: "     07:08")
  %_r   -> 7:08:09 AM  (GNU: 07:08:09 AM)

Unflagged %R was already correct and is now covered too. A single digit
followed by ':' can only be an unpadded hour, so match the shape instead
of one composite's length. The existing %_10T case is unaffected.
@codspeed

codspeed Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Unable to generate the flame graphs

The performance report has correctly been generated, but there was an internal error while generating the flame graphs for this run. We're working on fixing the issue. Feel free to contact us on Discord or at [email protected] if the issue persists.

@SichenLiang

Copy link
Copy Markdown
Contributor

On %-F, GNU applies the - flag to the year field, so the expected result is 1-01-01, not 0001-01-01. This is the edge case I was referring to earlier.

$ TZ=UTC LC_ALL=C date -d 0001-01-01 +%-F
1-01-01
$ TZ=UTC LC_ALL=C date -d 0999-12-31 +%-F
999-12-31

Current main matches GNU coreutils 9.12. At 6406fb4, both results retain the zero-padded year (0001-01-01 and 0999-12-31), and test_date_strftime_flag_on_composite expects the first of those.
The %-D, %_D, and %_10T cases match GNU.
There is one more expectation to adjust. At 07:08:09, GNU prints 07:08 for %-R, while the new test expects 7:08. The same applies to %-r: GNU prints 07:08:09 AM, not 7:08:09 AM. Main already differs from GNU here, so the PR did not introduce the behavior, but the new %-R test would preserve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

date: strftime flags propagate into composite specifiers (%-D, %-F, …)

3 participants