Skip to content

date: keep leading zeros of %N when a width is given - #15698

Merged
cakebaker merged 1 commit into
uutils:mainfrom
sarankumar1325:fix/date-n-width-leading-zeros
Oct 24, 2026
Merged

cakebaker merged 1 commit into
uutils:mainfrom
sarankumar1325:fix/date-n-width-leading-zeros

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

Fixes #15697

apply_modifiers stripped the leading zeros of the 9-digit %N value before truncating it to the requested width. As a result, %3N printed 512 for 0.005123456 (GNU prints 005), and consecutive %s%3N timestamps could go backwards.

%N is a fraction, not an integer, so it now goes through its own small helper:

  • a width keeps the first width digits, leading zeros included;
  • a wider width pads on the right (%12N gives 005123456000, not 000005123456);
  • inside that width, trailing zeros become the pad character (%_3N of .050 gives 05 ), and the - flag drops them (%-3N gives 05);
  • with no width, %N, %-N and %+N stay 9 digits, and %_N space-pads the trailing zeros as before.

The two %N-only branches left in the generic padding path could no longer be reached, so I removed them.

Testing

  • I compared uutils against GNU date 9.7 on 24 flag/width combinations (%N %3N %_3N %-3N %_N %-N %12N %_12N %03N %+3N %^3N %1N %-9N %-12N %_-3N %-_3N %_03N %0_3N %+N %+12N %_+3N %0N %_9N %9N), each at @0.005123456, @0.123, @0 and @0.05. All outputs match. Before this change, 13 of them differed.
  • New test test_date_strftime_n_width_keeps_leading_zeros in tests/by-util/test_date.rs. It fails on main and passes with this change.
  • cargo test --features date --no-default-features -- test_date (193 passed), cargo test -p uu_date (42 passed), and cargo clippy --all-targets -- -D warnings for the date feature are all clean.

I used an AI assistant to help write this change. I reviewed every line, and I worked out the expected outputs by running GNU date as a black box, not from its source.

%3N stripped the leading zeros of the nanosecond field before truncating
it, so 0.005123456 printed 512 instead of 005, and %s%3N timestamps
could go backwards. Handle %N as a fraction: a width keeps the first
digits and pads on the right, trailing zeros become the pad character,
and the - flag drops them. This matches GNU for the flag and width
combinations covered by the new test.

Fixes uutils#15697
Copilot AI balanced review requested due to automatic review settings October 23, 2026 15:46

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@cakebaker
cakebaker merged commit 469c9ce into uutils:main Oct 24, 2026
100 of 101 checks passed
@cakebaker

Copy link
Copy Markdown
Contributor

Thanks for your PR!

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: %3N (width on %N) drops leading zeros of nanoseconds, giving wrong/non-monotonic milliseconds

3 participants