Skip to content

Commit

Permalink
date: format date to correct date format before parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadabd committed May 14, 2024
1 parent 23d5235 commit 0354861
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// spell-checker:ignore (chrono) Datelike Timelike ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes

use chrono::format::{Item, StrftimeItems};
use chrono::{DateTime, FixedOffset, Local, Offset, TimeDelta, Utc};
use chrono::{DateTime, FixedOffset, Local, NaiveDate, Offset, TimeDelta, Utc};
#[cfg(windows)]
use chrono::{Datelike, Timelike};
use clap::{crate_version, Arg, ArgAction, Command};
Expand Down Expand Up @@ -222,6 +222,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// Iterate over all dates - whether it's a single date or a file.
let dates: Box<dyn Iterator<Item = _>> = match settings.date_source {
DateSource::Custom(ref input) => {

let input = NaiveDate::parse_from_str(input, "%Y-%m-%d")
.expect(format!("invalid date {}", input).as_str())
.format("%Y-%m-%d")
.to_string();

let date = parse_date(input.clone());
let iter = std::iter::once(date);
Box::new(iter)
Expand Down
8 changes: 8 additions & 0 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ fn test_invalid_date_string() {
.stderr_contains("invalid date");
}

#[test]
fn test_date_set_check_valid_format() {
new_ucmd!()
.arg("-d")
.arg("2000-1-4")
.succeeds();
}

#[test]
fn test_date_overflow() {
new_ucmd!()
Expand Down

0 comments on commit 0354861

Please sign in to comment.