From f65d72e334f73aedb38c6e149480733f433f0e8a Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 8 May 2022 21:52:12 +0200 Subject: [PATCH] also support for tests/touch/relative.sh --- src/uu/touch/src/touch.rs | 8 ++++++++ tests/by-util/test_touch.rs | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 5444c8c10..f71eb81d0 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -312,6 +312,13 @@ const YYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = format_descri [hour repr:24 padding:zero][minute padding:zero]" ); +// "%Y-%m-%d %H:%M +offset" +// Used for example in tests/touch/relative.sh +const YYYYMMDDHHMM_OFFSET_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year]-[month]-[day] [hour repr:24]:[minute] \ + [offset_hour sign:mandatory][offset_minute]" +); + fn parse_date(s: &str) -> UResult { // This isn't actually compatible with GNU touch, but there doesn't seem to // be any simple specification for what format this parameter allows and I'm @@ -338,6 +345,7 @@ fn parse_date(s: &str) -> UResult { YYYYMMDDHHMMS_FORMAT, YYYYMMDDHHMMSS_FORMAT, YYYY_MM_DD_HH_MM_FORMAT, + YYYYMMDDHHMM_OFFSET_FORMAT, ] { if let Ok(parsed) = time::PrimitiveDateTime::parse(s, &fmt) { return Ok(dt_to_filename(parsed)); diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index 5417a0dbe..346e27919 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -488,6 +488,25 @@ fn test_touch_set_date6() { assert_eq!(mtime, expected); } +#[test] +fn test_touch_set_date7() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_date"; + + ucmd.args(&["-d", "2004-01-16 12:00 +0000", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let expected = FileTime::from_unix_time(1074254400, 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!();