Skip to content

Comments

date: fix %+ and %_ modifier edge cases#10999

Open
naoNao89 wants to merge 4 commits intouutils:mainfrom
naoNao89:fix-date-modifier-edge-cases
Open

date: fix %+ and %_ modifier edge cases#10999
naoNao89 wants to merge 4 commits intouutils:mainfrom
naoNao89:fix-date-modifier-edge-cases

Conversation

@naoNao89
Copy link
Contributor

Fixes #10957

%+ without width now omits sign for 4-digit years; %_ now pads to default width. Adds get_default_width() and explicit_width parameter

Fixes uutils#10957:

1. %+ without explicit width now correctly omits sign for years
   with <= 4 digits. Sign is only added when explicit width is
   provided or year exceeds default width.

2. %_ without explicit width now correctly pads to the specifier's
   default width (e.g., %_m produces " 6" instead of "6").

Changes:
- Add get_default_width() function mapping specifiers to POSIX
  default widths
- Modify apply_modifiers() to accept explicit_width parameter
- Update tests for new behavior

Test results:
- date -d '1999-06-01' '+%+Y' -> "1999" (was "+1999")
- date -d '1999-06-01' '+%_m' -> " 6" (was "6")
@naoNao89 naoNao89 force-pushed the fix-date-modifier-edge-cases branch from c00657a to 1db79d3 Compare February 17, 2026 17:59
@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/tail/pipe-f2. tests/tail/pipe-f2 is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/pr/bounded-memory (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tty/tty-eof (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/cp/link-heap is now passing!

Comment on lines +149 to +150
let explicit_width = !width_str.is_empty();
let modified = apply_modifiers(&formatted, flags, width, spec, explicit_width);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you introduce explicit_width? If I look at the tests, there seems to be a connection between width and explicit_width: if width is 0, explicit_width is false. And if width is greater than 0, explicit_width is always true. And thus it seems unnecessary to pass explicit_width to apply_modifiers. Maybe I'm missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tks, i found it.

Format GNU UU Gap
%0e (day=5) 05 5 UU ignores 0 flag on %e
%0k (hour=5) 05 5 UU ignores 0 flag on %k

%([_0^#+-]*)(\d*)(:*[a-zA-Z]) always consumes 0 as a flag (group 1), never as width digits (group 2) that never "0"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oke fixed

- Add is_space_padded_specifier() to recognize %e, %k, %l as space-padded
- Fix %C default width from 4 to 2 (century is 00-99)
- Fix effective_width logic to apply default width when padding flag
  changes the default padding character
- Add strip logic for space→zero padding conversion
@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/tail/tail-n0f is now passing!

@sylvestre
Copy link
Contributor

Please also add integration tests in test_date.rs
Thanks

@github-actions
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/date/date-locale-hour is no longer failing!

@sylvestre
Copy link
Contributor

please merge the tests into a single tests with a loop with input/expected as a datastrucutre

@naoNao89 naoNao89 force-pushed the fix-date-modifier-edge-cases branch from 6d02dc1 to 18a1300 Compare February 21, 2026 17:13
@github-actions
Copy link

GNU testsuite comparison:

GNU test failed: tests/date/resolution. tests/date/resolution is passing on 'main'. Maybe you have to rebase?
Skipping an intermittent issue tests/tail/symlink (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/date/date-locale-hour is no longer failing!

Add consolidated integration test for format modifier behavior without
explicit width, covering the new features in format_modifiers.rs:

- is_space_padded_specifier(): Tests %e, %k, %l default space padding and
  %0e, %0k, %0l zero-padding override
- get_default_width(): Tests %_d, %_m, %_H, %_Y, %_C, %_j using default widths
- explicit_width parameter: Tests %+Y with/without explicit width

Uses a data-driven test structure with all cases in a single vector.
@sylvestre
Copy link
Contributor

please run pre-commit install
it will avoid some rustfmt issues

@naoNao89
Copy link
Contributor Author

sr, the precommit causes issues with SELinux, but since am using macOS, I always run with --no-verify :))

@naoNao89 naoNao89 force-pushed the fix-date-modifier-edge-cases branch from 18a1300 to d3cae2a Compare February 22, 2026 12:33
@github-actions
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/date/date-locale-hour is no longer failing!
Congrats! The gnu test tests/tail/retry is no longer failing!

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: edge cases for modifiers

3 participants