Skip to content

date: print jiff-only conversions %f, %.f, %Q, %:Q literally - #16520

Closed
emirmrkaljevic wants to merge 3 commits into
uutils:mainfrom
emirmrkaljevic:date-jiff-only-specifiers
Closed

emirmrkaljevic wants to merge 3 commits into
uutils:mainfrom
emirmrkaljevic:date-jiff-only-specifiers

Conversation

@emirmrkaljevic

Copy link
Copy Markdown
Contributor

Fixes #16519.

jiff knows %f, %.f (with an optional precision), %Q and %:Q; GNU date does not and prints them unchanged. Before handing the format to jiff, escape the % of those conversions (including any flags, width or colons), next to the existing %O handling.

$ TZ=UTC LC_ALL=C date -d @1.5 '+%Q|%:Q|%f|%.3f|%-f'
%Q|%:Q|%f|%.3f|%-f

GNU also pads an unknown conversion to its width (%5Q gives %5Q); that is not covered here.

Added test_date_non_gnu_specifiers_are_literal.

Found with behave.

@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will regress 2 benchmarks

2 warnings, 1 note

⚠️ 124 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 11 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 12 improved benchmarks
❌ 2 regressed benchmarks
✅ 247 untouched benchmarks
🆕 4 new benchmarks
⏩ 214 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ CPU Simulation socket_addrs_direct[100000] 104.7 µs 132.4 µs -20.91%
❌ CPU Simulation hostname_ip_lookup[100000] 155.6 µs 185.6 µs -16.16%
⚡ CPU Simulation split_lines 14.4 ms 4.3 ms ×3.3
⚡ CPU Simulation split_numeric_suffix 14.6 ms 4.5 ms ×3.2
⚡ CPU Simulation factor_small_factors_and_a_large_prime 1,012.7 µs 682.1 µs +48.47%
⚡ CPU Simulation join_partial_overlap 18.7 ms 15.1 ms +23.42%
⚡ CPU Simulation join_full_match 21.8 ms 18.3 ms +19.19%
⚡ CPU Simulation join_french_locale 21.8 ms 18.3 ms +19.14%
⚡ CPU Simulation join_unicode_locale 2.2 ms 1.8 ms +19.05%
⚡ CPU Simulation join_custom_separator 22.1 ms 18.8 ms +17.83%
⚡ CPU Simulation unexpand_large_file[10] 88.5 ms 83.4 ms +6.14%
⚡ CPU Simulation unexpand_many_lines[100000] 42.6 ms 40.2 ms +6.08%
⚡ CPU Simulation split_number_chunks_round_robin 30.3 ms 28.9 ms +4.87%
⚡ CPU Simulation split_bytes 781.9 µs 756 µs +3.43%
🆕 CPU Simulation factor_batch_of_largest_u64s N/A 557.2 ms N/A
🆕 CPU Simulation factor_batch_of_semiprimes N/A 794.1 ms N/A
🆕 CPU Simulation factor_batch_of_small_numbers N/A 138.6 ms N/A
🆕 CPU Simulation factor_batch_of_smallest_u128s N/A 516.3 ms N/A

Tip

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


Comparing emirmrkaljevic:date-jiff-only-specifiers (d1d51b1) with main (fca3de1)2

Open in CodSpeed

Footnotes

  1. 214 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. ↩

  2. No successful run was found on main (35012ad) during the generation of this report, so fca3de1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Comment thread src/uu/date/src/date.rs Outdated
/// them and prints an unknown conversion literally, so `+%Q` must stay `%Q`.
/// Turn the leading `%` of such a conversion into `%%` before jiff sees it.
fn escape_non_gnu_specifiers(fmt: &str) -> String {
if !fmt.contains('%') {

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.

nearly every format has a %, so this always allocates and rescans; could you please guard on f/Q/. like substitute_epoch_seconds does with s? should fix the codspeed regression

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 7befa0a: it returns early unless the format has a % and one of O, f, Q or ., and it returns a Cow, so nothing is allocated when nothing changes.

Comment thread src/uu/date/src/date.rs Outdated
out.push('%');
start = i;
}
i = j.max(i + 1);

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.

j is always > i here, i = j; is enough, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, changed to i = j;.

Comment thread src/uu/date/src/date.rs Outdated
// to the floored epoch second before jiff sees the format string.
let fmt_owned = strip_o_modifier(&substitute_epoch_seconds(fmt, date));
let fmt_owned =
escape_non_gnu_specifiers(&strip_o_modifier(&substitute_epoch_seconds(fmt, date)));

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.

three passes over the format now (epoch, O, this), could this one be merged with strip_o_modifier?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Merged: strip_o_modifier and the escaping are now one function, adapt_format_for_jiff, so it is two passes again (epoch + this one). While at it, %Of and %OQ now stay literal like GNU (before they turned into %f/%Q), added to the test.

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for picking this up!
It can't land yet: codspeed shows a 4.57% slowdown, and the limit is 3%. Each call now makes one more allocation and one more pass over the format (see the inline comments).

@emirmrkaljevic

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed 7befa0a with the guard and the merged pass; waiting for CodSpeed to rerun.

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for the work on this!
It can't land yet: a format whose only jiff-only conversion is at the very start (date +%Q, date +%f) still prints UTC/5, see the inline comment.
CodSpeed also needs to come back under 3% on the new head.

@xtqqczze

Copy link
Copy Markdown
Collaborator

/rerun OpenBSD

Comment thread src/uu/date/src/date.rs Outdated
}
i = j;
}
if start == 0 {

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.

start is still 0 after escaping a conversion at index 0, so date +%Q or date +%f alone still prints UTC/5. Could you please track the change with a flag?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks. Fixed in d1d51b1: it now tracks the change with a changed flag instead of start == 0.

.arg("-d")
.arg("@1.5")
.arg("+%Q %:Q %f %.3f %-f %%Q %Of %OQ %s")
.succeeds()

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 also test +%Q and +%.3f alone: here the later escapes hide the index 0 case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added test_date_non_gnu_specifier_at_start_is_literal with +%Q, +%f and +%.3f on their own.

Comment thread src/uu/date/src/date.rs
}
if bytes.get(i + 1) == Some(&b'O') && bytes.get(i + 2).is_some_and(u8::is_ascii_alphabetic)
{
if matches!(bytes[i + 2], b'f' | b'Q') {

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 the same push / % / start = i as the one below, could they share the code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, both now call a small escape_percent helper.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/env/env0-from is no longer failing!

}

#[test]
fn test_date_non_gnu_specifier_at_start_is_literal() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

please use rstest cases

@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.49 MB (+108 KB, +0.07%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

@xtqqczze

Copy link
Copy Markdown
Collaborator

Duplicate of #14672.

@xtqqczze

Copy link
Copy Markdown
Collaborator

I do not think this should be merged; I think we should document the %f and %Q conversion specifiers instead.

Originally posted by @xtqqczze in #14672 (review)

@xtqqczze

Copy link
Copy Markdown
Collaborator

As per POSIX, the current behavior is not non-compliant. POSIX says the following about strftime:

If a conversion specification does not correspond to any of the above, the behavior is undefined.

So %f and %Q are outside the POSIX-specified behavior, but we are free to provide extensions for them. I don't think we need to remove these conversion specifiers solely because GNU does not implement them.

Originally posted by @xtqqczze in #14672 (comment)

@emirmrkaljevic

Copy link
Copy Markdown
Contributor Author

Thanks, fair point, and I missed that #14672 already covers this. Since the direction there is to keep %f/%Q as extensions and document them, I will close this one. Thanks @sylvestre and @xtqqczze for the reviews.

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: %f, %.f, %Q and %:Q are expanded instead of printed literally like GNU

3 participants