Skip to content

Commit 57d9dd7

Browse files
committed
date: fix date -f dates.txt is failing
This commit is a trivial followup for: uutils#4917 and uutils/parse_datetime#12 The functionality to parse the datetime was moved into the parse_datetime crate and the only (tiny) piece left is to call it from `date`. It also adds the test-case from the original issue. I did not include the two tests from PR#4917 because they appear to work even without this change. I am happy to include them of course if prefered. Closes: uutils#4657 Thanks to Ben Schofield
1 parent 28f7d56 commit 57d9dd7

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/uu/date/src/date.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,8 @@ fn make_format_string(settings: &Settings) -> &str {
404404
/// If it fails, return a tuple of the `String` along with its `ParseError`.
405405
fn parse_date<S: AsRef<str> + Clone>(
406406
s: S,
407-
) -> Result<DateTime<FixedOffset>, (String, chrono::format::ParseError)> {
408-
// TODO: The GNU date command can parse a wide variety of inputs.
409-
s.as_ref().parse().map_err(|e| (s.as_ref().into(), e))
407+
) -> Result<DateTime<FixedOffset>, (String, parse_datetime::ParseDateTimeError)> {
408+
parse_datetime::parse_datetime(s.as_ref()).map_err(|e| (s.as_ref().into(), e))
410409
}
411410

412411
#[cfg(not(any(unix, windows)))]

tests/by-util/test_date.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,21 @@ fn test_date_overflow() {
409409
.no_stdout()
410410
.stderr_contains("invalid date");
411411
}
412+
413+
#[test]
414+
fn test_date_parse_from_format() {
415+
let (at, mut ucmd) = at_and_ucmd!();
416+
const FILE: &str = "file-with-dates";
417+
418+
std::fs::write(
419+
at.plus(FILE),
420+
"2023-03-27 08:30:00\n\
421+
2023-04-01 12:00:00\n\
422+
2023-04-15 18:30:00",
423+
)
424+
.unwrap();
425+
ucmd.arg("-f")
426+
.arg(at.plus(FILE))
427+
.arg("+%Y-%m-%d %H:%M:%S")
428+
.succeeds();
429+
}

0 commit comments

Comments
 (0)