Skip to content

Commit 25e4410

Browse files
committed
date: Properly support %#z, instead of rejecting the format
It's easy to just replace %#z with %z as the capitalization makes no sense anyway.
1 parent 053e6b4 commit 25e4410

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

src/uu/date/src/date.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
274274
match date {
275275
Ok(date) => {
276276
let format_string = custom_time_format(format_string);
277-
// Refuse to pass this string to chrono as it is crashing in this crate
278-
if format_string.contains("%#z") {
279-
return Err(USimpleError::new(
280-
1,
281-
format!("invalid format {}", format_string.replace("%f", "%N")),
282-
));
283-
}
284277
// Hack to work around panic in chrono,
285278
// TODO - remove when a fix for https://github.com/chronotope/chrono/issues/623 is released
286279
let format_items = StrftimeItems::new(format_string.as_str());

src/uucore/src/lib/features/custom_tz_fmt.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ fn timezone_abbreviation() -> String {
3535
/// A string that can be used as parameter of the chrono functions that use formats
3636
pub fn custom_time_format(fmt: &str) -> String {
3737
// TODO - Revisit when chrono 0.5 is released. https://github.com/chronotope/chrono/issues/970
38+
// chrono crashes on %#z, but it's the same as %z anyway.
3839
// GNU `date` uses `%N` for nano seconds, however the `chrono` crate uses `%f`.
39-
fmt.replace("%N", "%f")
40+
fmt.replace("%#z", "%z")
41+
.replace("%N", "%f")
4042
.replace("%Z", timezone_abbreviation().as_ref())
4143
}
4244

tests/by-util/test_date.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,12 @@ fn test_invalid_format_string() {
381381
}
382382

383383
#[test]
384-
fn test_unsupported_format() {
385-
let result = new_ucmd!().arg("+%#z").fails();
386-
result.no_stdout();
387-
assert!(result.stderr_str().starts_with("date: invalid format %#z"));
384+
fn test_capitalized_numeric_time_zone() {
385+
// %z +hhmm numeric time zone (e.g., -0400)
386+
// # is supposed to capitalize, which makes little sense here, but chrono crashes
387+
// on such format so it's good to test.
388+
let re = Regex::new(r"^[+-]\d{4,4}\n$").unwrap();
389+
new_ucmd!().arg("+%#z").succeeds().stdout_matches(&re);
388390
}
389391

390392
#[test]

0 commit comments

Comments
 (0)