-
-
Notifications
You must be signed in to change notification settings - Fork 2k
date: drop pad flags before composite strftime specifiers #14179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MadeNavaneeth
wants to merge
1
commit into
uutils:main
Choose a base branch
from
MadeNavaneeth:date-composite-modifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -771,6 +771,60 @@ fn substitute_epoch_seconds(fmt: &str, date: &Zoned) -> String { | |
| out | ||
| } | ||
|
|
||
| /// 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: | ||
| /// Strip `-` and `_` pad flags before composite strftime specifiers | ||
| /// (`%D`, `%F`, `%T`, `%r`, `%R`, `%c`, `%x`, `%X`). GNU treats these | ||
| /// composites as atomic so the flag must not leak into inner fields. | ||
| /// Other modifiers (width, `0`/`^`/`#`/`+`) are left untouched. | ||
| fn strip_modifiers_on_composite(fmt: &str) -> String { | ||
| const COMPOSITES: &[char] = &['D', 'F', 'T', 'r', 'R', 'c', 'x', 'X']; | ||
| if !fmt.contains('%') { | ||
| return fmt.to_string(); | ||
| } | ||
|
|
||
| let mut out = String::with_capacity(fmt.len()); | ||
| let mut chars = fmt.chars().peekable(); | ||
| while let Some(c) = chars.next() { | ||
| if c != '%' { | ||
| out.push(c); | ||
| continue; | ||
| } | ||
| // Skip `%%` literally. | ||
| if chars.peek() == Some(&'%') { | ||
| chars.next(); | ||
| out.push_str("%%"); | ||
| continue; | ||
| } | ||
| // Look ahead: optional pad flags (`-`/`_`), other modifiers, then a | ||
| // composite letter? | ||
| let mut ahead = chars.clone(); | ||
| while ahead | ||
| .peek() | ||
| .is_some_and(|&m| m == '-' || m == '_' || "_0^#+".contains(m) || m.is_ascii_digit()) | ||
| { | ||
| ahead.next(); | ||
| } | ||
| if ahead.peek().is_some_and(|&l| COMPOSITES.contains(&l)) { | ||
| // Consume only the pad flags (`-`/`_`); keep the rest so the | ||
| // downstream width/flag handling (and its huge-width guard) still | ||
| // applies to the whole composite. | ||
| while chars.peek().is_some_and(|&m| m == '-' || m == '_') { | ||
| chars.next(); | ||
| } | ||
| out.push('%'); | ||
| out.push(chars.next().unwrap()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove the unwrap() |
||
| } else { | ||
| out.push('%'); | ||
| } | ||
| } | ||
| out | ||
| } | ||
|
|
||
| /// Remove the `O` strftime modifier from `fmt`. | ||
| /// | ||
| /// In the C locale `%O` requests alternative numeric symbols that do not | ||
|
|
@@ -908,7 +962,8 @@ fn format_date_with_locale_aware_months( | |
| // negative infinity (e.g. `@-1.5` → `-2`, not `-1`). Every other field jiff | ||
| // produces already agrees with GNU, so only `%s` needs correcting; rewrite it | ||
| // 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 = | ||
| strip_modifiers_on_composite(&strip_o_modifier(&substitute_epoch_seconds(fmt, date))); | ||
| let fmt = fmt_owned.as_str(); | ||
|
|
||
| // Check if format string has GNU modifiers (width/flags) and format if present | ||
|
|
@@ -1295,6 +1350,31 @@ fn set_system_datetime(date: Zoned) -> UResult<()> { | |
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move that into test_date.rs instead |
||
| fn test_strip_modifiers_on_composite() { | ||
| // Pad flags before a composite are dropped (GNU treats the | ||
| // composite as atomic, so the flag must not leak into inner fields). | ||
| assert_eq!(strip_modifiers_on_composite("%-D"), "%D"); | ||
| assert_eq!(strip_modifiers_on_composite("%_x"), "%x"); | ||
| assert_eq!(strip_modifiers_on_composite("%-F"), "%F"); | ||
| assert_eq!(strip_modifiers_on_composite("%_T"), "%T"); | ||
|
|
||
| // Non-composite specs keep their modifiers untouched. | ||
| assert_eq!(strip_modifiers_on_composite("%-d"), "%-d"); | ||
| assert_eq!(strip_modifiers_on_composite("%_m"), "%_m"); | ||
| assert_eq!(strip_modifiers_on_composite("%10Y"), "%10Y"); | ||
|
|
||
| // Width is preserved so the huge-width guard still fires. | ||
| assert_eq!( | ||
| strip_modifiers_on_composite("%18446744073709551615c"), | ||
| "%18446744073709551615c" | ||
| ); | ||
|
|
||
| // `%%` literals are left alone; only the composite after a real `%` is affected. | ||
| assert_eq!(strip_modifiers_on_composite("%%-D"), "%%-D"); | ||
| assert_eq!(strip_modifiers_on_composite("a%-Db"), "a%Db"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_parse_military_timezone_with_offset() { | ||
| // Valid cases: letter only, letter + digit, uppercase | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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