From 84affa2137feecedc997b5b865bb5690705f1599 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Tue, 15 Jun 2021 21:52:47 +0200 Subject: [PATCH] touch: support `@` date format parse `@` as a valid date. --- src/uu/touch/src/touch.rs | 4 ++++ tests/by-util/test_touch.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index efa436c81..2e1c3c8e8 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -268,6 +268,10 @@ fn parse_date(str: &str) -> FileTime { return local_tm_to_filetime(to_local(tm)); } } + if let Ok(tm) = time::strptime(str, "@%s") { + // Don't convert to local time in this case - seconds since epoch are not time-zone dependent + return local_tm_to_filetime(tm); + } show_error!("Unable to parse date: {}\n", str); process::exit(1); } diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index 5e8114092..3ed7f3bb2 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -375,6 +375,24 @@ fn test_touch_set_date2() { assert_eq!(mtime, start_of_year); } +#[test] +fn test_touch_set_date3() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_date"; + + ucmd.args(&["-d", "@1623786360", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let expected = FileTime::from_unix_time(1623786360, 0); + let (atime, mtime) = get_file_times(&at, file); + assert_eq!(atime, mtime); + assert_eq!(atime, expected); + assert_eq!(mtime, expected); +} + #[test] fn test_touch_set_date_wrong_format() { let (_at, mut ucmd) = at_and_ucmd!();