Skip to content

date: fix strftime flags and widths on %N (nanoseconds)#11778

Open
sjhddh wants to merge 2 commits intouutils:mainfrom
sjhddh:fix/date-strftime-nanosecond-flags
Open

date: fix strftime flags and widths on %N (nanoseconds)#11778
sjhddh wants to merge 2 commits intouutils:mainfrom
sjhddh:fix/date-strftime-nanosecond-flags

Conversation

@sjhddh
Copy link
Copy Markdown

@sjhddh sjhddh commented Apr 12, 2026

Summary

Fixes #11658

GNU date treats %N width as precision (number of fractional-second digits) and its zeros as significant content, not padding. Previously uutils ignored or mishandled flags/widths on %N:

  • %-N at @0 produced 0 instead of 000000000
  • %_3N at @0 produced 0 instead of 0
  • %3N at @0 produced 0 instead of 000

Changes

  • Added apply_nanosecond_modifiers() in format_modifiers.rs with special handling for %N:
    • %-N: preserve all digits unchanged (zeros are content, not padding)
    • %3N: truncate to first 3 fractional digits
    • %_3N: truncate to 3 digits, replace trailing zeros with spaces
    • %_N: full 9 digits, trailing zeros replaced with spaces
  • Added unit tests for nanosecond modifier handling (zero and non-zero nanos)
  • Un-ignored the existing integration test test_date_strftime_n_width_and_flags

Test plan

  • All 23 format_modifiers unit tests pass (including 2 new nanosecond tests)
  • All 129 test_date integration tests pass (previously ignored test now runs)
  • Verified: %-N at @0000000000
  • Verified: %_3N at @00 (0 + two spaces)
  • Verified: %3N at @0000

🤖 Generated with Claude Code

GNU date treats %N width as precision (number of fractional-second
digits) and its zeros as significant content rather than padding.
Previously uutils ignored flags/widths on %N, producing incorrect
output (e.g. `%-N` at @0 gave "0" instead of "000000000").

Add special handling for the %N specifier in apply_modifiers:
- %-N: preserve all digits (zeros are content, not padding)
- %3N: truncate to 3 fractional digits
- %_3N: truncate to 3 digits, replace trailing zeros with spaces
- %_N: full 9 digits, trailing zeros replaced with spaces

Un-ignore the existing integration test for issue uutils#11658.

Closes uutils#11658

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tty/tty-eof (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/dd/no-allocate is now passing!
Congrats! The gnu test tests/printf/printf-surprise is now passing!

@cakebaker
Copy link
Copy Markdown
Contributor

Can you please run cargo clippy? Thanks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sjhddh
Copy link
Copy Markdown
Author

sjhddh commented Apr 13, 2026

Ran cargo clippy -p uu_date — clean, no warnings:

    Checking uu_date v0.8.0 (/Users/jiahao/Git/coreutils-work/src/uu/date)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 10.92s

(The previous clippy fixes from commit ccff54f already resolved the chars_last_cmp and unnecessary_wraps lints.)

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 ignores flags and widths on %N (nanoseconds)

2 participants