Skip to content

Commit faa2219

Browse files
authored
Merge pull request #7854 from drinkcat/date-tests
date: add `%q` test, support `%#z` properly
2 parents 053e6b4 + 19e08d5 commit faa2219

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ fn test_date_format_y() {
169169
scene.ucmd().arg("+%y").succeeds().stdout_matches(&re);
170170
}
171171

172+
#[test]
173+
fn test_date_format_q() {
174+
let scene = TestScenario::new(util_name!());
175+
176+
let re = Regex::new(r"^[1-4]\n$").unwrap();
177+
scene.ucmd().arg("+%q").succeeds().stdout_matches(&re);
178+
}
179+
172180
#[test]
173181
fn test_date_format_m() {
174182
let scene = TestScenario::new(util_name!());
@@ -381,10 +389,12 @@ fn test_invalid_format_string() {
381389
}
382390

383391
#[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"));
392+
fn test_capitalized_numeric_time_zone() {
393+
// %z +hhmm numeric time zone (e.g., -0400)
394+
// # is supposed to capitalize, which makes little sense here, but chrono crashes
395+
// on such format so it's good to test.
396+
let re = Regex::new(r"^[+-]\d{4,4}\n$").unwrap();
397+
new_ucmd!().arg("+%#z").succeeds().stdout_matches(&re);
388398
}
389399

390400
#[test]

0 commit comments

Comments
 (0)