Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date: format date to correct date format before parsing #6401

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -219,9 +219,15 @@
now.with_timezone(now.offset())
};

// Iterate over all dates - whether it's a single date or a file.

Check failure on line 222 in src/uu/date/src/date.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/date/src/date.rs', line:222; use `cargo fmt -- "src/uu/date/src/date.rs"`)

Check failure on line 222 in src/uu/date/src/date.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/date/src/date.rs', line:222; use `cargo fmt -- "src/uu/date/src/date.rs"`)
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please run rustfmt

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")

Check warning on line 228 in src/uu/date/src/date.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/date/src/date.rs#L228

Added line #L228 was not covered by tests
.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 @@ -400,7 +400,15 @@
.no_stdout()
.stderr_contains("invalid date");
}

Check failure on line 403 in tests/by-util/test_date.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'tests/by-util/test_date.rs', line:403; use `cargo fmt -- "tests/by-util/test_date.rs"`)

Check failure on line 403 in tests/by-util/test_date.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo fmt`: style violation (file:'tests/by-util/test_date.rs', line:403; use `cargo fmt -- "tests/by-util/test_date.rs"`)
#[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
Loading