Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/uucore/src/lib/features/i18n/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ pub enum CalendarType {

/// Transform a strftime format string to use locale-specific calendar values
pub fn localize_format_string(format: &str, date: &JiffDate) -> String {
const PERCENT_PLACEHOLDER: &str = "\x00\x00";

let (locale, _) = get_time_locale();
let iso_date = Date::<Iso>::convert_from(*date);

let mut fmt = format.to_string();
let mut fmt = format.replace("%%", PERCENT_PLACEHOLDER);

// For non-Gregorian calendars, replace date components with converted values
let calendar_type = get_locale_calendar_type(locale);
Expand Down Expand Up @@ -126,7 +128,7 @@ pub fn localize_format_string(format: &str, date: &JiffDate) -> String {
}
}

fmt
fmt.replace(PERCENT_PLACEHOLDER, "%%")
}

#[cfg(test)]
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_date.rs
Comment thread
cakebaker marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -1967,3 +1967,32 @@ fn test_locale_day_names() {
check_date(loc, "2026-01-24", "+%A", sat);
}
}

#[test]
fn test_percent_percent_not_replaced() {
let cases = [
// Time conversion specifiers
(
"+%%H%%I%%k%%l%%M%%N%%p%%P%%r%%R%%s%%S%%T%%X%%z%%Z",
"%H%I%k%l%M%N%p%P%r%R%s%S%T%X%z%Z\n",
),
// Date conversion specifiers
(
"+%%a%%A%%b%%B%%c%%C%%d%%D%%e%%F%%g%%G%%h%%j%%m%%u%%U%%V%%w%%W%%x%%y%%Y",
"%a%A%b%B%c%C%d%D%e%F%g%G%h%j%m%u%U%V%w%W%x%y%Y\n",
),
];
for (format, expected) in cases {
new_ucmd!()
.env("TZ", "UTC")
.arg(format)
.succeeds()
.stdout_is(expected);
new_ucmd!()
.env("TZ", "UTC")
.env("LC_ALL", "fr_FR.UTF-8")
.arg(format)
.succeeds()
.stdout_is(expected);
}
}
Loading