Skip to content

Commit

Permalink
Fix touch parse date error
Browse files Browse the repository at this point in the history
Now it can parse `touch -d 2000-01-23 file` and add test for parse date
error.

Related to #2311
  • Loading branch information
deantvv committed May 31, 2021
1 parent 6141fdc commit 9d8e7b9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
24 changes: 1 addition & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/uu/touch/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,14 @@ fn parse_date(str: &str) -> FileTime {
// be any simple specification for what format this parameter allows and I'm
// not about to implement GNU parse_datetime.
// http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y
match time::strptime(str, "%c") {
Ok(tm) => local_tm_to_filetime(to_local(tm)),
Err(e) => panic!("Unable to parse date\n{}", e),
let formats = vec!["%c", "%F"];
for f in formats {
if let Ok(tm) = time::strptime(str, f) {
return local_tm_to_filetime(to_local(tm));
}
}
show_error!("Unable to parse date: {}\n", str);
process::exit(1);
}

fn parse_timestamp(s: &str) -> FileTime {
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,34 @@ fn test_touch_set_date() {
assert_eq!(mtime, start_of_year);
}

#[test]
fn test_touch_set_date2() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_touch_set_date";

ucmd.args(&["-d", "2000-01-23", file])
.succeeds()
.no_stderr();

assert!(at.file_exists(file));

let start_of_year = str_to_filetime("%Y%m%d%H%M", "200001230000");
let (atime, mtime) = get_file_times(&at, file);
assert_eq!(atime, mtime);
assert_eq!(atime, start_of_year);
assert_eq!(mtime, start_of_year);
}

#[test]
fn test_touch_set_date_wrong_format() {
let (_at, mut ucmd) = at_and_ucmd!();
let file = "test_touch_set_date_wrong_format";

ucmd.args(&["-d", "2005-43-21", file])
.fails()
.stderr_contains("Unable to parse date: 2005-43-21");
}

#[test]
fn test_touch_mtime_dst_succeeds() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down

0 comments on commit 9d8e7b9

Please sign in to comment.