mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
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.
This commit is contained in:
parent
053e6b4d08
commit
25e4410c3b
3 changed files with 9 additions and 12 deletions
|
@ -274,13 +274,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
match date {
|
match date {
|
||||||
Ok(date) => {
|
Ok(date) => {
|
||||||
let format_string = custom_time_format(format_string);
|
let format_string = custom_time_format(format_string);
|
||||||
// Refuse to pass this string to chrono as it is crashing in this crate
|
|
||||||
if format_string.contains("%#z") {
|
|
||||||
return Err(USimpleError::new(
|
|
||||||
1,
|
|
||||||
format!("invalid format {}", format_string.replace("%f", "%N")),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
// Hack to work around panic in chrono,
|
// Hack to work around panic in chrono,
|
||||||
// TODO - remove when a fix for https://github.com/chronotope/chrono/issues/623 is released
|
// TODO - remove when a fix for https://github.com/chronotope/chrono/issues/623 is released
|
||||||
let format_items = StrftimeItems::new(format_string.as_str());
|
let format_items = StrftimeItems::new(format_string.as_str());
|
||||||
|
|
|
@ -35,8 +35,10 @@ fn timezone_abbreviation() -> String {
|
||||||
/// A string that can be used as parameter of the chrono functions that use formats
|
/// A string that can be used as parameter of the chrono functions that use formats
|
||||||
pub fn custom_time_format(fmt: &str) -> String {
|
pub fn custom_time_format(fmt: &str) -> String {
|
||||||
// TODO - Revisit when chrono 0.5 is released. https://github.com/chronotope/chrono/issues/970
|
// TODO - Revisit when chrono 0.5 is released. https://github.com/chronotope/chrono/issues/970
|
||||||
|
// chrono crashes on %#z, but it's the same as %z anyway.
|
||||||
// GNU `date` uses `%N` for nano seconds, however the `chrono` crate uses `%f`.
|
// GNU `date` uses `%N` for nano seconds, however the `chrono` crate uses `%f`.
|
||||||
fmt.replace("%N", "%f")
|
fmt.replace("%#z", "%z")
|
||||||
|
.replace("%N", "%f")
|
||||||
.replace("%Z", timezone_abbreviation().as_ref())
|
.replace("%Z", timezone_abbreviation().as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -381,10 +381,12 @@ fn test_invalid_format_string() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unsupported_format() {
|
fn test_capitalized_numeric_time_zone() {
|
||||||
let result = new_ucmd!().arg("+%#z").fails();
|
// %z +hhmm numeric time zone (e.g., -0400)
|
||||||
result.no_stdout();
|
// # is supposed to capitalize, which makes little sense here, but chrono crashes
|
||||||
assert!(result.stderr_str().starts_with("date: invalid format %#z"));
|
// on such format so it's good to test.
|
||||||
|
let re = Regex::new(r"^[+-]\d{4,4}\n$").unwrap();
|
||||||
|
new_ucmd!().arg("+%#z").succeeds().stdout_matches(&re);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue