From ca670148f2bf1a247e784f732f241b8278d81291 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 27 Apr 2022 08:43:03 +0200 Subject: [PATCH 01/19] build(deps): bump time from 0.1.43 to 0.3.9 Bumps [time](https://github.com/time-rs/time) from 0.1.43 to 0.3.9. - [Release notes](https://github.com/time-rs/time/releases) - [Changelog](https://github.com/time-rs/time/blob/main/CHANGELOG.md) - [Commits](https://github.com/time-rs/time/compare/v0.1.43...v0.3.9) --- updated-dependencies: - dependency-name: time dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- Cargo.lock | 29 +++-- Cargo.toml | 2 +- deny.toml | 3 + src/uu/install/Cargo.toml | 3 + src/uu/pinky/src/pinky.rs | 10 +- src/uu/touch/Cargo.toml | 2 +- src/uu/touch/src/touch.rs | 152 ++++++++++++++++++--------- src/uu/uptime/src/uptime.rs | 6 +- src/uu/who/src/who.rs | 10 +- src/uucore/Cargo.toml | 4 +- src/uucore/src/lib/features/fsext.rs | 12 ++- src/uucore/src/lib/features/utmpx.rs | 14 +-- tests/by-util/test_mv.rs | 6 +- tests/by-util/test_touch.rs | 86 ++++++++------- 14 files changed, 217 insertions(+), 122 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 191ad8923ff..497d7ab4244 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,7 +224,7 @@ dependencies = [ "libc", "num-integer", "num-traits", - "time", + "time 0.1.44", "winapi 0.3.9", ] @@ -335,7 +335,7 @@ dependencies = [ "sha1", "tempfile", "textwrap 0.15.0", - "time", + "time 0.3.9", "unindent", "unix_socket", "users", @@ -1213,6 +1213,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aba1801fb138d8e85e11d0fc70baf4fe1cdfffda7c6cd34a854905df588e5ed0" +dependencies = [ + "libc", +] + [[package]] name = "number_prefix" version = "0.4.0" @@ -1944,16 +1953,17 @@ dependencies = [ [[package]] name = "time" -version = "0.1.43" +version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", + "wasi", "winapi 0.3.9", ] [[package]] -name = "typenum" +< String { thread_local! { - static NOW: time::Tm = time::now() + static NOW: time::OffsetDateTime = time::OffsetDateTime::now_local().unwrap(); } NOW.with(|n| { - let duration = n.to_timespec().sec - when; + let duration = n.unix_timestamp() - when; if duration < 60 { // less than 1min " ".to_owned() @@ -242,7 +242,11 @@ fn idle_string(when: i64) -> String { } fn time_string(ut: &Utmpx) -> String { - time::strftime("%b %e %H:%M", &ut.login_time()).unwrap() // LC_ALL=C + // "%b %e %H:%M" + let time_format: Vec = + time::format_description::parse("[month repr:short] [day padding:space] [hour]:[minute]") + .unwrap(); + ut.login_time().format(&time_format).unwrap() // LC_ALL=C } fn gecos_to_fullname(pw: &Passwd) -> Option { diff --git a/src/uu/touch/Cargo.toml b/src/uu/touch/Cargo.toml index 646b65f50e0..aa747ae7859 100644 --- a/src/uu/touch/Cargo.toml +++ b/src/uu/touch/Cargo.toml @@ -17,7 +17,7 @@ path = "src/touch.rs" [dependencies] filetime = "0.2.1" clap = { version = "3.1", features = ["wrap_help", "cargo"] } -time = "0.1.40" +time = { version = "0.3", features = ["parsing", "formatting", "local-offset", "macros"] } uucore = { version=">=0.0.11", package="uucore", path="../../uucore", features=["libc"] } [target.'cfg(target_os = "windows")'.dependencies] diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index ff08a1b592f..75cd38a7a43 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -17,6 +17,7 @@ use clap::{crate_version, Arg, ArgGroup, Command}; use filetime::*; use std::fs::{self, File}; use std::path::{Path, PathBuf}; +use time::macros::{format_description, time}; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult, USimpleError}; use uucore::format_usage; @@ -41,14 +42,13 @@ pub mod options { static ARG_FILES: &str = "files"; -fn to_local(mut tm: time::Tm) -> time::Tm { - tm.tm_utcoff = time::now().tm_utcoff; - tm +fn to_local(tm: time::PrimitiveDateTime) -> time::OffsetDateTime { + // TODO: handle error getting now + tm.assume_offset(time::OffsetDateTime::now_local().unwrap().offset()) } -fn local_tm_to_filetime(tm: time::Tm) -> FileTime { - let ts = tm.to_timespec(); - FileTime::from_unix_time(ts.sec as i64, ts.nsec as u32) +fn local_dt_to_filetime(dt: time::OffsetDateTime) -> FileTime { + FileTime::from_unix_time(dt.unix_timestamp(), dt.nanosecond()) } #[uucore::main] @@ -62,7 +62,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { Try 'touch --help' for more information."##, ) })?; - let (mut atime, mut mtime) = if let Some(reference) = matches.value_of_os(options::sources::REFERENCE) { stat(Path::new(reference), !matches.is_present(options::NO_DEREF))? @@ -72,7 +71,7 @@ Try 'touch --help' for more information."##, } else if let Some(current) = matches.value_of(options::sources::CURRENT) { parse_timestamp(current)? } else { - local_tm_to_filetime(time::now()) + local_dt_to_filetime(time::OffsetDateTime::now_local().unwrap()) }; (timestamp, timestamp) }; @@ -248,38 +247,80 @@ fn stat(path: &Path, follow: bool) -> UResult<(FileTime, FileTime)> { )) } -fn parse_date(str: &str) -> UResult { +const POSIX_LOCALE_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[weekday repr:short] [month repr:short] [day padding:space] [hour]:[minute]:[second] [year]" +); + +const ISO_8601_FORMAT: &[time::format_description::FormatItem] = + format_description!("[year]-[month]-[day]"); + +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 // not about to implement GNU parse_datetime. // http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y - let formats = vec!["%c", "%F"]; - for f in formats { - if let Ok(tm) = time::strptime(str, f) { - return Ok(local_tm_to_filetime(to_local(tm))); - } + + // TODO: match on char count? + + // "The preferred date and time representation for the current locale." + // "(In the POSIX locale this is equivalent to %a %b %e %H:%M:%S %Y.)" + // time 0.1.43 parsed this as 'a b e T Y' + // which is equivalent to the POSIX locale: %a %b %e %H:%M:%S %Y + // Tue Dec 3 ... + // ("%c", POSIX_LOCALE_FORMAT), + if let Ok(parsed) = time::PrimitiveDateTime::parse(s, &POSIX_LOCALE_FORMAT) { + return Ok(local_dt_to_filetime(to_local(parsed))); } - 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 Ok(local_tm_to_filetime(tm)); + // "Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)" + // ("%F", ISO_8601_FORMAT), + if let Ok(parsed) = time::Date::parse(s, &ISO_8601_FORMAT) { + return Ok(local_dt_to_filetime(to_local( + time::PrimitiveDateTime::new(parsed, time!(00:00)), + ))); } - Err(USimpleError::new( - 1, - format!("Unable to parse date: {}", str), - )) + // "@%s" is "The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ) (Calculated from mktime(tm).)" + if s.bytes().next() == Some(b'@') { + if let Ok(ts) = &s[1..].parse::() { + // Don't convert to local time in this case - seconds since epoch are not time-zone dependent + return Ok(local_dt_to_filetime( + time::OffsetDateTime::from_unix_timestamp(*ts).unwrap(), + )); + } + } + + Err(USimpleError::new(1, format!("Unable to parse date: {}", s))) } +// "%Y%m%d%H%M.%S" 15 chars +const YYYYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:full][month repr:numerical padding:zero][day][hour][minute].[second]" +); + +// "%Y%m%d%H%M" 12 chars +const YYYYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = + format_description!("[year repr:full][month repr:numerical padding:zero][day][hour][minute]"); + +// "%y%m%d%H%M.%S" 13 chars +const YYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = + format_description!("[year repr:last_two padding:none][month][day][hour][minute].[second]"); + +// "%y%m%d%H%M" 10 chars +const YYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = + format_description!("[year repr:last_two padding:none][month padding:zero][day padding:zero][hour repr:24 padding:zero][minute padding:zero]"); + fn parse_timestamp(s: &str) -> UResult { - let now = time::now(); - let (format, ts) = match s.chars().count() { - 15 => ("%Y%m%d%H%M.%S", s.to_owned()), - 12 => ("%Y%m%d%H%M", s.to_owned()), - 13 => ("%y%m%d%H%M.%S", s.to_owned()), - 10 => ("%y%m%d%H%M", s.to_owned()), - 11 => ("%Y%m%d%H%M.%S", format!("{}{}", now.tm_year + 1900, s)), - 8 => ("%Y%m%d%H%M", format!("{}{}", now.tm_year + 1900, s)), + // TODO: handle error + let now = time::OffsetDateTime::now_utc(); + + let (mut format, mut ts) = match s.chars().count() { + 15 => (YYYYMMDDHHMM_DOT_SS_FORMAT, s.to_owned()), + 12 => (YYYYMMDDHHMM_FORMAT, s.to_owned()), + 13 => (YYMMDDHHMM_DOT_SS_FORMAT, s.to_owned()), + 10 => (YYMMDDHHMM_FORMAT, s.to_owned()), + 11 => (YYYYMMDDHHMM_DOT_SS_FORMAT, format!("{}{}", now.year(), s)), + 8 => (YYYYMMDDHHMM_FORMAT, format!("{}{}", now.year(), s)), _ => { return Err(USimpleError::new( 1, @@ -287,30 +328,39 @@ fn parse_timestamp(s: &str) -> UResult { )) } }; - - let tm = time::strptime(&ts, format) - .map_err(|_| USimpleError::new(1, format!("invalid date format {}", s.quote())))?; - - let mut local = to_local(tm); - local.tm_isdst = -1; - let ft = local_tm_to_filetime(local); - - // We have to check that ft is valid time. Due to daylight saving - // time switch, local time can jump from 1:59 AM to 3:00 AM, - // in which case any time between 2:00 AM and 2:59 AM is not valid. - // Convert back to local time and see if we got the same value back. - let ts = time::Timespec { - sec: ft.unix_seconds(), - nsec: 0, - }; - let tm2 = time::at(ts); - if tm.tm_hour != tm2.tm_hour { - return Err(USimpleError::new( - 1, - format!("invalid date format {}", s.quote()), - )); + // workaround time returning Err(TryFromParsed(InsufficientInformation)) for year w/ + // repr:last_two + // https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1ccfac7c07c5d1c7887a11decf0e1996 + if s.chars().count() == 10 { + format = YYYYMMDDHHMM_FORMAT; + ts = "20".to_owned() + &ts; + } else if s.chars().count() == 13 { + format = YYYYMMDDHHMM_DOT_SS_FORMAT; + ts = "20".to_owned() + &ts; } + let tm = time::PrimitiveDateTime::parse(&ts, &format) + .map_err(|_| USimpleError::new(1, format!("invalid date ts format {}", ts.quote())))?; + + let local = to_local(tm); + let ft = local_dt_to_filetime(local); + + // // We have to check that ft is valid time. Due to daylight saving + // // time switch, local time can jump from 1:59 AM to 3:00 AM, + // // in which case any time between 2:00 AM and 2:59 AM is not valid. + // // Convert back to local time and see if we got the same value back. + // let ts = time::Timespec { + // sec: ft.unix_seconds(), + // nsec: 0, + // }; + // let tm2 = time::at(ts); + // if tm.tm_hour != tm2.tm_hour { + // return Err(USimpleError::new( + // 1, + // format!("invalid date format {}", s.quote()), + // )); + // } + Ok(ft) } diff --git a/src/uu/uptime/src/uptime.rs b/src/uu/uptime/src/uptime.rs index a93344dbcd1..5c64cb5af17 100644 --- a/src/uu/uptime/src/uptime.rs +++ b/src/uu/uptime/src/uptime.rs @@ -111,9 +111,9 @@ fn process_utmpx() -> (Option, usize) { match line.record_type() { USER_PROCESS => nusers += 1, BOOT_TIME => { - let t = line.login_time().to_timespec(); - if t.sec > 0 { - boot_time = Some(t.sec as time_t); + let dt = line.login_time(); + if dt.second() > 0 { + boot_time = Some(dt.second() as time_t); } } _ => continue, diff --git a/src/uu/who/src/who.rs b/src/uu/who/src/who.rs index 6e21ac91257..47d96a38189 100644 --- a/src/uu/who/src/who.rs +++ b/src/uu/who/src/who.rs @@ -275,10 +275,10 @@ struct Who { fn idle_string<'a>(when: i64, boottime: i64) -> Cow<'a, str> { thread_local! { - static NOW: time::Tm = time::now() + static NOW: time::OffsetDateTime = time::OffsetDateTime::now_local().unwrap(); } NOW.with(|n| { - let now = n.to_timespec().sec; + let now = n.unix_timestamp(); if boottime < when && now - 24 * 3600 < when && when <= now { let seconds_idle = now - when; if seconds_idle < 60 { @@ -298,7 +298,11 @@ fn idle_string<'a>(when: i64, boottime: i64) -> Cow<'a, str> { } fn time_string(ut: &Utmpx) -> String { - time::strftime("%b %e %H:%M", &ut.login_time()).unwrap() // LC_ALL=C + // "%b %e %H:%M" + let time_format: Vec = + time::format_description::parse("[month repr:short] [day padding:space] [hour]:[minute]") + .unwrap(); + ut.login_time().format(&time_format).unwrap() // LC_ALL=C } #[inline] diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index 6f74b238a04..5b363376d24 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -26,7 +26,7 @@ wild = "2.0" # * optional itertools = { version="0.10.0", optional=true } thiserror = { version="1.0", optional=true } -time = { version="<= 0.1.43", optional=true } +time = { version="<= 0.3.10", optional=true, features = ["formatting", "local-offset", "macros"] } # * "problem" dependencies (pinned) data-encoding = { version="2.1", optional=true } data-encoding-macro = { version="0.1.12", optional=true } @@ -62,6 +62,6 @@ process = ["libc"] ringbuffer = [] signals = [] utf8 = [] -utmpx = ["time", "libc", "dns-lookup"] +utmpx = ["time", "time/macros", "libc", "dns-lookup"] wide = [] pipes = ["nix"] diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index eeaf5406137..838bfd4b5e0 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -12,6 +12,7 @@ // spell-checker:ignore (arch) bitrig ; (fs) cifs smbfs extern crate time; +use time::macros::format_description; pub use crate::*; // import macros from `../../macros.rs` @@ -63,7 +64,6 @@ fn LPWSTR2String(buf: &[u16]) -> String { String::from_utf16(&buf[..len]).unwrap() } -use self::time::Timespec; #[cfg(unix)] use libc::{ mode_t, strerror, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK, @@ -732,11 +732,17 @@ where } } +// match strftime "%Y-%m-%d %H:%M:%S.%f %z" +const PRETTY_DATETIME_FORMAT: &[time::format_description::FormatItem] = format_description!("[year]-[month]-[day padding:zero] [hour]:[minute]:[second].[subsecond] [offset_hour][offset_minute]"); + pub fn pretty_time(sec: i64, nsec: i64) -> String { // sec == seconds since UNIX_EPOCH // nsec == nanoseconds since (UNIX_EPOCH + sec) - let tm = time::at(Timespec::new(sec, nsec as i32)); - let res = time::strftime("%Y-%m-%d %H:%M:%S.%f %z", &tm).unwrap(); + let ts_nanos: i128 = (sec * 1_000_000_000 + nsec).into(); + // TODO: return errors to caller + let tm = time::OffsetDateTime::from_unix_timestamp_nanos(ts_nanos).unwrap(); + + let res = tm.format(&PRETTY_DATETIME_FORMAT).unwrap(); if res.ends_with(" -0000") { res.replace(" -0000", " +0000") } else { diff --git a/src/uucore/src/lib/features/utmpx.rs b/src/uucore/src/lib/features/utmpx.rs index 302d03d71ef..dc66eae097d 100644 --- a/src/uucore/src/lib/features/utmpx.rs +++ b/src/uucore/src/lib/features/utmpx.rs @@ -32,7 +32,6 @@ //! ``` pub extern crate time; -use self::time::{Timespec, Tm}; use std::ffi::CString; use std::io::Result as IOResult; @@ -189,11 +188,14 @@ impl Utmpx { chars2string!(self.inner.ut_line) } /// A.K.A. ut.ut_tv - pub fn login_time(&self) -> Tm { - time::at(Timespec::new( - self.inner.ut_tv.tv_sec as i64, - self.inner.ut_tv.tv_usec as i32, - )) + pub fn login_time(&self) -> time::OffsetDateTime { + let ts_nanos: i128 = (self.inner.ut_tv.tv_sec as i64 * 1_000_000_000 as i64 + + self.inner.ut_tv.tv_usec as i64 * 1_000 as i64) + .into(); + let local_offset = time::OffsetDateTime::now_local().unwrap().offset(); + time::OffsetDateTime::from_unix_timestamp_nanos(ts_nanos) + .unwrap() + .to_offset(local_offset) } /// A.K.A. ut.ut_exit /// diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index c4ec03d95be..06f4d5259a7 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -595,9 +595,9 @@ fn test_mv_update_option() { at.touch(file_a); at.touch(file_b); - let ts = time::now().to_timespec(); - let now = FileTime::from_unix_time(ts.sec as i64, ts.nsec as u32); - let later = FileTime::from_unix_time(ts.sec as i64 + 3600, ts.nsec as u32); + let ts = time::OffsetDateTime::now_local().unwrap(); + let now = FileTime::from_unix_time(ts.unix_timestamp(), ts.nanosecond()); + let later = FileTime::from_unix_time(ts.unix_timestamp() as i64 + 3600, ts.nanosecond() as u32); filetime::set_file_times(at.plus_as_string(file_a), now, now).unwrap(); filetime::set_file_times(at.plus_as_string(file_b), now, later).unwrap(); diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index 6e5d656c403..6de6358318b 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -4,6 +4,7 @@ extern crate touch; use self::touch::filetime::{self, FileTime}; extern crate time; +use time::macros::{datetime, format_description}; use crate::common::util::*; use std::fs::remove_file; @@ -32,11 +33,18 @@ fn set_file_times(at: &AtPath, path: &str, atime: FileTime, mtime: FileTime) { // Adjusts for local timezone fn str_to_filetime(format: &str, s: &str) -> FileTime { - let mut tm = time::strptime(s, format).unwrap(); - tm.tm_utcoff = time::now().tm_utcoff; - tm.tm_isdst = -1; // Unknown flag DST - let ts = tm.to_timespec(); - FileTime::from_unix_time(ts.sec as i64, ts.nsec as u32) + let format_description = match format { + "%y%m%d%H%M" => format_description!("[year repr:last_two][month][day][hour][minute]"), + "%y%m%d%H%M.%S" => { + format_description!("[year repr:last_two][month][day][hour][minute].[second]") + } + "%Y%m%d%H%M" => format_description!("[year][month][day][hour][minute]"), + "%Y%m%d%H%M.%S" => format_description!("[year][month][day][hour][minute].[second]"), + _ => panic!("unexpected dt format"), + }; + let tm = time::PrimitiveDateTime::parse(&s, &format_description).unwrap(); + let offset_dt = tm.assume_offset(time::OffsetDateTime::now_local().unwrap().offset()); + FileTime::from_unix_time(offset_dt.unix_timestamp(), tm.nanosecond()) } #[test] @@ -83,7 +91,10 @@ fn test_touch_set_mdhm_time() { let start_of_year = str_to_filetime( "%Y%m%d%H%M", - &format!("{}01010000", 1900 + time::now().tm_year), + &format!( + "{}01010000", + time::OffsetDateTime::now_local().unwrap().year() + ), ); let (atime, mtime) = get_file_times(&at, file); assert_eq!(atime, mtime); @@ -104,7 +115,7 @@ fn test_touch_set_mdhms_time() { let start_of_year = str_to_filetime( "%Y%m%d%H%M.%S", - &format!("{}01010000.00", 1900 + time::now().tm_year), + &format!("{}01010000.00", time::OffsetDateTime::now_utc().year()), ); let (atime, mtime) = get_file_times(&at, file); assert_eq!(atime, mtime); @@ -123,7 +134,7 @@ fn test_touch_set_ymdhm_time() { assert!(at.file_exists(file)); - let start_of_year = str_to_filetime("%y%m%d%H%M", "1501010000"); + let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000"); let (atime, mtime) = get_file_times(&at, file); assert_eq!(atime, mtime); assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45240); @@ -141,7 +152,7 @@ fn test_touch_set_ymdhms_time() { assert!(at.file_exists(file)); - let start_of_year = str_to_filetime("%y%m%d%H%M.%S", "1501010000.00"); + let start_of_year = str_to_filetime("%Y%m%d%H%M.%S", "201501010000.00"); let (atime, mtime) = get_file_times(&at, file); assert_eq!(atime, mtime); assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45296); @@ -430,18 +441,18 @@ fn test_touch_mtime_dst_succeeds() { assert_eq!(target_time, mtime); } -// is_dst_switch_hour returns true if timespec ts is just before the switch -// to Daylight Saving Time. -// For example, in EST (UTC-5), Timespec { sec: 1583647200, nsec: 0 } -// for March 8 2020 01:00:00 AM -// is just before the switch because on that day clock jumps by 1 hour, -// so 1 minute after 01:59:00 is 03:00:00. -fn is_dst_switch_hour(ts: time::Timespec) -> bool { - let ts_after = ts + time::Duration::hours(1); - let tm = time::at(ts); - let tm_after = time::at(ts_after); - tm_after.tm_hour == tm.tm_hour + 2 -} +// // is_dst_switch_hour returns true if timespec ts is just before the switch +// // to Daylight Saving Time. +// // For example, in EST (UTC-5), Timespec { sec: 1583647200, nsec: 0 } +// // for March 8 2020 01:00:00 AM +// // is just before the switch because on that day clock jumps by 1 hour, +// // so 1 minute after 01:59:00 is 03:00:00. +// fn is_dst_switch_hour(ts: time::Timespec) -> bool { +// let ts_after = ts + time::Duration::hours(1); +// let tm = time::at(ts); +// let tm_after = time::at(ts_after); +// tm_after.tm_hour == tm.tm_hour + 2 +// } // get_dst_switch_hour returns date string for which touch -m -t fails. // For example, in EST (UTC-5), that will be "202003080200" so @@ -450,23 +461,24 @@ fn is_dst_switch_hour(ts: time::Timespec) -> bool { // In other locales it will be a different date/time, and in some locales // it doesn't exist at all, in which case this function will return None. fn get_dst_switch_hour() -> Option { - let now = time::now(); + let now = time::OffsetDateTime::now_local().unwrap(); + // Start from January 1, 2020, 00:00. - let mut tm = time::strptime("20200101-0000", "%Y%m%d-%H%M").unwrap(); - tm.tm_isdst = -1; - tm.tm_utcoff = now.tm_utcoff; - let mut ts = tm.to_timespec(); - // Loop through all hours in year 2020 until we find the hour just - // before the switch to DST. - for _i in 0..(366 * 24) { - if is_dst_switch_hour(ts) { - let mut tm = time::at(ts); - tm.tm_hour += 1; - let s = time::strftime("%Y%m%d%H%M", &tm).unwrap(); - return Some(s); - } - ts = ts + time::Duration::hours(1); - } + let tm = datetime!(2020-01-01 00:00 UTC); + tm.to_offset(now.offset()); + + // let mut ts = tm.to_timespec(); + // // Loop through all hours in year 2020 until we find the hour just + // // before the switch to DST. + // for _i in 0..(366 * 24) { + // // if is_dst_switch_hour(ts) { + // // let mut tm = time::at(ts); + // // tm.tm_hour += 1; + // // let s = time::strftime("%Y%m%d%H%M", &tm).unwrap(); + // // return Some(s); + // // } + // ts = ts + time::Duration::hours(1); + // } None } From f810b55d8660abf98e4d0aad1842a123b3160932 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 23 Apr 2022 22:05:43 +0200 Subject: [PATCH 02/19] build in verbose mode (cfg isn't used) --- .github/workflows/CICD.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index 967e77b60d1..fbe04a1db34 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -384,7 +384,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: test - args: ${{ steps.vars.outputs.CARGO_FEATURES_OPTION }} -p uucore -p coreutils + args: -v ${{ steps.vars.outputs.CARGO_FEATURES_OPTION }} -p uucore -p coreutils env: RUSTFLAGS: "-Awarnings" From 3a576f2441bb29162576d49170f42241dc0e3581 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 23 Apr 2022 21:32:35 +0200 Subject: [PATCH 03/19] time: Various fixes --- .cargo/config | 9 +++++++++ .github/workflows/CICD.yml | 2 +- Cargo.lock | 30 +++++++++++++++++++++++++--- src/uu/touch/src/touch.rs | 3 +-- src/uucore/src/lib/features/fsext.rs | 2 +- src/uucore/src/lib/features/utmpx.rs | 4 ++-- tests/by-util/test_cp.rs | 12 +++++------ tests/by-util/test_touch.rs | 26 ++++++++++++++++++++---- 8 files changed, 69 insertions(+), 19 deletions(-) diff --git a/.cargo/config b/.cargo/config index 0a8fd3d00ce..26008597f0c 100644 --- a/.cargo/config +++ b/.cargo/config @@ -9,3 +9,12 @@ rustflags = [ "-Wclippy::single_char_pattern", "-Wclippy::explicit_iter_loop", ] + +[build] +# See https://github.com/time-rs/time/issues/293#issuecomment-1005002386. The +# unsoundness here is not in the `time` library, but in the Rust stdlib, and as +# such it needs to be fixed there. +rustflags = "--cfg unsound_local_offset" + +[target.'cfg(target_os = "linux")'] +rustflags = ["--cfg", "unsound_local_offset"] diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index fbe04a1db34..04acd4c1825 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -386,7 +386,7 @@ jobs: command: test args: -v ${{ steps.vars.outputs.CARGO_FEATURES_OPTION }} -p uucore -p coreutils env: - RUSTFLAGS: "-Awarnings" + RUSTFLAGS: "-Awarnings --cfg unsound_local_offset" deps: name: Dependencies diff --git a/Cargo.lock b/Cargo.lock index 497d7ab4244..69a83941b38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -871,7 +871,7 @@ checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c" dependencies = [ "cfg-if 1.0.0", "libc", - "wasi 0.10.2+wasi-snapshot-preview1", + "wasi 0.10.0+wasi-snapshot-preview1", ] [[package]] @@ -985,6 +985,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + [[package]] name = "keccak" version = "0.1.0" @@ -1958,12 +1964,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", - "wasi", + "wasi 0.10.0+wasi-snapshot-preview1", "winapi 0.3.9", ] [[package]] -< time::OffsetDateTime { - let ts_nanos: i128 = (self.inner.ut_tv.tv_sec as i64 * 1_000_000_000 as i64 - + self.inner.ut_tv.tv_usec as i64 * 1_000 as i64) + let ts_nanos: i128 = (self.inner.ut_tv.tv_sec as i64 * 1_000_000_000_i64 + + self.inner.ut_tv.tv_usec as i64 * 1_000_i64) .into(); let local_offset = time::OffsetDateTime::now_local().unwrap().offset(); time::OffsetDateTime::from_unix_timestamp_nanos(ts_nanos) diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index 079e966be3b..90e85b76afb 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -1032,8 +1032,8 @@ fn test_cp_no_deref_folder_to_folder() { #[cfg(target_os = "linux")] fn test_cp_archive() { let (at, mut ucmd) = at_and_ucmd!(); - let ts = time::now().to_timespec(); - let previous = FileTime::from_unix_time(ts.sec as i64 - 3600, ts.nsec as u32); + let ts = time::OffsetDateTime::now_local().unwrap(); + let previous = FileTime::from_unix_time(ts.unix_timestamp() - 3600, ts.nanosecond() as u32); // set the file creation/modification an hour ago filetime::set_file_times( at.plus_as_string(TEST_HELLO_WORLD_SOURCE), @@ -1135,8 +1135,8 @@ fn test_cp_archive_recursive() { #[cfg(any(target_os = "linux", target_os = "android"))] fn test_cp_preserve_timestamps() { let (at, mut ucmd) = at_and_ucmd!(); - let ts = time::now().to_timespec(); - let previous = FileTime::from_unix_time(ts.sec as i64 - 3600, ts.nsec as u32); + let ts = time::OffsetDateTime::now_local().unwrap(); + let previous = FileTime::from_unix_time(ts.unix_timestamp() - 3600, ts.nanosecond()); // set the file creation/modification an hour ago filetime::set_file_times( at.plus_as_string(TEST_HELLO_WORLD_SOURCE), @@ -1168,8 +1168,8 @@ fn test_cp_preserve_timestamps() { #[cfg(any(target_os = "linux", target_os = "android"))] fn test_cp_no_preserve_timestamps() { let (at, mut ucmd) = at_and_ucmd!(); - let ts = time::now().to_timespec(); - let previous = FileTime::from_unix_time(ts.sec as i64 - 3600, ts.nsec as u32); + let ts = time::OffsetDateTime::now_local().unwrap(); + let previous = FileTime::from_unix_time(ts.unix_timestamp() - 3600, ts.nanosecond()); // set the file creation/modification an hour ago filetime::set_file_times( at.plus_as_string(TEST_HELLO_WORLD_SOURCE), diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index 6de6358318b..2c538d8b404 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -1,4 +1,10 @@ -// spell-checker:ignore (formats) cymdhm cymdhms mdhm mdhms ymdhm ymdhms +// spell-checker:ignore (formats) cymdhm cymdhms mdhm mdhms ymdhm ymdhms datetime mktime + +// This test relies on +// --cfg unsound_local_offset +// https://github.com/time-rs/time/blob/deb8161b84f355b31e39ce09e40c4d6ce3fea837/src/sys/local_offset_at/unix.rs#L112-L120= +// See https://github.com/time-rs/time/issues/293#issuecomment-946382614= +// Defined in .cargo/config extern crate touch; use self::touch::filetime::{self, FileTime}; @@ -42,8 +48,14 @@ fn str_to_filetime(format: &str, s: &str) -> FileTime { "%Y%m%d%H%M.%S" => format_description!("[year][month][day][hour][minute].[second]"), _ => panic!("unexpected dt format"), }; - let tm = time::PrimitiveDateTime::parse(&s, &format_description).unwrap(); - let offset_dt = tm.assume_offset(time::OffsetDateTime::now_local().unwrap().offset()); + let tm = time::PrimitiveDateTime::parse(s, &format_description).unwrap(); + let d = match time::OffsetDateTime::now_local() { + Ok(now) => now, + Err(e) => { + panic!("Error {} retrieving the OffsetDateTime::now_local", e); + } + }; + let offset_dt = tm.assume_offset(d.offset()); FileTime::from_unix_time(offset_dt.unix_timestamp(), tm.nanosecond()) } @@ -461,7 +473,13 @@ fn test_touch_mtime_dst_succeeds() { // In other locales it will be a different date/time, and in some locales // it doesn't exist at all, in which case this function will return None. fn get_dst_switch_hour() -> Option { - let now = time::OffsetDateTime::now_local().unwrap(); + //let now = time::OffsetDateTime::now_local().unwrap(); + let now = match time::OffsetDateTime::now_local() { + Ok(now) => now, + Err(e) => { + panic!("Error {} retrieving the OffsetDateTime::now_local", e); + } + }; // Start from January 1, 2020, 00:00. let tm = datetime!(2020-01-01 00:00 UTC); From e23dd687155c91e50d1c9f90dbfe7d9ef047657a Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 24 Apr 2022 09:50:39 +0200 Subject: [PATCH 04/19] time: Force the display of the tz sign --- src/uucore/src/lib/features/fsext.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index 75a0c2f9721..0d64661ee1c 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -733,7 +733,7 @@ where } // match strftime "%Y-%m-%d %H:%M:%S.%f %z" -const PRETTY_DATETIME_FORMAT: &[time::format_description::FormatItem] = format_description!("[year]-[month]-[day padding:zero] [hour]:[minute]:[second].[subsecond] [offset_hour][offset_minute]"); +const PRETTY_DATETIME_FORMAT: &[time::format_description::FormatItem] = format_description!("[year]-[month]-[day padding:zero] [hour]:[minute]:[second].[subsecond] [offset_hour sign:mandatory][offset_minute]"); pub fn pretty_time(sec: i64, nsec: i64) -> String { // sec == seconds since UNIX_EPOCH From 326dc5080d74dd79406fa828e1754d50e78e9e8e Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 24 Apr 2022 09:50:58 +0200 Subject: [PATCH 05/19] stat: add a test to verify time easily --- tests/by-util/test_stat.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/by-util/test_stat.rs b/tests/by-util/test_stat.rs index 90ad2d12aaa..6140f50170f 100644 --- a/tests/by-util/test_stat.rs +++ b/tests/by-util/test_stat.rs @@ -283,6 +283,25 @@ fn test_char() { ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); } +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +#[test] +fn test_date() { + // Just test the date for the time 0.3 change + let args = [ + "-c", + #[cfg(any(target_os = "linux", target_os = "android"))] + "%z", + #[cfg(target_os = "linux")] + "/dev/pts/ptmx", + #[cfg(any(target_vendor = "apple"))] + "%z", + #[cfg(any(target_os = "android", target_vendor = "apple"))] + "/dev/ptmx", + ]; + let ts = TestScenario::new(util_name!()); + let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); + ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); +} #[cfg(unix)] #[test] fn test_multi_files() { From 10eaaae2723f61f8588dabcb78df9ec57f7ce80c Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 24 Apr 2022 09:51:15 +0200 Subject: [PATCH 06/19] time: take in account the local tz --- src/uucore/src/lib/features/fsext.rs | 29 ++++++++++++++++++++++++---- tests/by-util/test_stat.rs | 17 +++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index 0d64661ee1c..3f5b2b77cac 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -13,6 +13,7 @@ extern crate time; use time::macros::format_description; +use time::UtcOffset; pub use crate::*; // import macros from `../../macros.rs` @@ -733,16 +734,36 @@ where } // match strftime "%Y-%m-%d %H:%M:%S.%f %z" -const PRETTY_DATETIME_FORMAT: &[time::format_description::FormatItem] = format_description!("[year]-[month]-[day padding:zero] [hour]:[minute]:[second].[subsecond] [offset_hour sign:mandatory][offset_minute]"); +const PRETTY_DATETIME_FORMAT: &[time::format_description::FormatItem] = format_description!("[year]-[month]-[day padding:zero] [hour]:[minute]:[second].[subsecond digits:9] [offset_hour sign:mandatory][offset_minute]"); pub fn pretty_time(sec: i64, nsec: i64) -> String { // sec == seconds since UNIX_EPOCH // nsec == nanoseconds since (UNIX_EPOCH + sec) let ts_nanos: i128 = (sec * 1_000_000_000 + nsec).into(); - // TODO: return errors to caller - let tm = time::OffsetDateTime::from_unix_timestamp_nanos(ts_nanos).unwrap(); - let res = tm.format(&PRETTY_DATETIME_FORMAT).unwrap(); + // Return the date in UTC + let tm = match time::OffsetDateTime::from_unix_timestamp_nanos(ts_nanos) { + Ok(tm) => tm, + Err(e) => { + panic!("error: {}", e); + } + }; + + // Get the offset to convert to local time + // Because of DST (daylight saving), we get the local time back when + // the date was set + let local_offset = match UtcOffset::local_offset_at(tm) { + Ok(lo) => lo, + Err(e) => { + panic!("error: {}", e); + } + }; + + // Include the conversion to local time + let res = tm + .to_offset(local_offset) + .format(&PRETTY_DATETIME_FORMAT) + .unwrap(); if res.ends_with(" -0000") { res.replace(" -0000", " +0000") } else { diff --git a/tests/by-util/test_stat.rs b/tests/by-util/test_stat.rs index 6140f50170f..0871a48fe9f 100644 --- a/tests/by-util/test_stat.rs +++ b/tests/by-util/test_stat.rs @@ -292,7 +292,22 @@ fn test_date() { #[cfg(any(target_os = "linux", target_os = "android"))] "%z", #[cfg(target_os = "linux")] - "/dev/pts/ptmx", + "/bin/sh", + #[cfg(any(target_vendor = "apple"))] + "%z", + #[cfg(any(target_os = "android", target_vendor = "apple"))] + "/bin/sh", + ]; + let ts = TestScenario::new(util_name!()); + let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); + ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); + // Just test the date for the time 0.3 change + let args = [ + "-c", + #[cfg(any(target_os = "linux", target_os = "android"))] + "%z", + #[cfg(target_os = "linux")] + "/dev/ptmx", #[cfg(any(target_vendor = "apple"))] "%z", #[cfg(any(target_os = "android", target_vendor = "apple"))] From 3b3585bbe548a5259944685f3114c84730155929 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 27 Apr 2022 08:43:59 +0200 Subject: [PATCH 07/19] add time 0.1.44 to cargo deny And no longer ignore RUSTSEC-2022-0013 --- deny.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deny.toml b/deny.toml index 1d790456f50..f659910e825 100644 --- a/deny.toml +++ b/deny.toml @@ -12,7 +12,6 @@ yanked = "warn" notice = "warn" ignore = [ "RUSTSEC-2020-0159", - "RUSTSEC-2022-0013", "RUSTSEC-2020-0071", #"RUSTSEC-0000-0000", ] @@ -87,6 +86,8 @@ skip = [ { name = "memchr", version = "=1.0.2" }, { name = "quote", version = "=0.3.15" }, { name = "unicode-xid", version = "=0.0.4" }, + # chrono + { name = "time", version = "=0.1.44" }, ] # spell-checker: enable From c009e1bed8a5b9c5eef667584fcf2b632dcdfaa2 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 28 Apr 2022 00:06:07 +0200 Subject: [PATCH 08/19] workaround the tests/touch/60-seconds test to skip leap second --- src/uu/touch/src/touch.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index a3a344ad41b..e74aaa7001c 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -337,7 +337,12 @@ fn parse_timestamp(s: &str) -> UResult { format = YYYYMMDDHHMM_DOT_SS_FORMAT; ts = "20".to_owned() + &ts; } - + if (format == YYYYMMDDHHMM_DOT_SS_FORMAT || format == YYMMDDHHMM_DOT_SS_FORMAT) + && ts.ends_with(".60") + { + // Work around to disable leap seconds + ts = ts.replace(".60", ".59"); + } let tm = time::PrimitiveDateTime::parse(&ts, &format) .map_err(|_| USimpleError::new(1, format!("invalid date ts format {}", ts.quote())))?; From 2b11d773952fb45de8fa0a4360b67c160f24ed49 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 28 Apr 2022 08:22:48 +0200 Subject: [PATCH 09/19] time: Improve the l&f --- src/uu/touch/src/touch.rs | 24 ++++++++++++++++-------- src/uucore/Cargo.toml | 2 +- src/uucore/src/lib/features/fsext.rs | 7 ++++++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index e74aaa7001c..9a4c0fe5db0 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -247,7 +247,8 @@ fn stat(path: &Path, follow: bool) -> UResult<(FileTime, FileTime)> { } const POSIX_LOCALE_FORMAT: &[time::format_description::FormatItem] = format_description!( - "[weekday repr:short] [month repr:short] [day padding:space] [hour]:[minute]:[second] [year]" + "[weekday repr:short] [month repr:short] [day padding:space] \ + [hour]:[minute]:[second] [year]" ); const ISO_8601_FORMAT: &[time::format_description::FormatItem] = @@ -294,20 +295,27 @@ fn parse_date(s: &str) -> UResult { // "%Y%m%d%H%M.%S" 15 chars const YYYYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = format_description!( - "[year repr:full][month repr:numerical padding:zero][day][hour][minute].[second]" + "[year repr:full][month repr:numerical padding:zero]\ + [day][hour][minute].[second]" ); // "%Y%m%d%H%M" 12 chars -const YYYYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = - format_description!("[year repr:full][month repr:numerical padding:zero][day][hour][minute]"); +const YYYYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:full][month repr:numerical padding:zero]\ + [day][hour][minute]" +); // "%y%m%d%H%M.%S" 13 chars -const YYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = - format_description!("[year repr:last_two padding:none][month][day][hour][minute].[second]"); +const YYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:last_two padding:none][month][day]\ + [hour][minute].[second]" +); // "%y%m%d%H%M" 10 chars -const YYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = - format_description!("[year repr:last_two padding:none][month padding:zero][day padding:zero][hour repr:24 padding:zero][minute padding:zero]"); +const YYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:last_two padding:none][month padding:zero][day padding:zero]\ + [hour repr:24 padding:zero][minute padding:zero]" +); fn parse_timestamp(s: &str) -> UResult { // TODO: handle error diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index 5b363376d24..c86a8cf0773 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -26,7 +26,7 @@ wild = "2.0" # * optional itertools = { version="0.10.0", optional=true } thiserror = { version="1.0", optional=true } -time = { version="<= 0.3.10", optional=true, features = ["formatting", "local-offset", "macros"] } +time = { version="<= 0.3", optional=true, features = ["formatting", "local-offset", "macros"] } # * "problem" dependencies (pinned) data-encoding = { version="2.1", optional=true } data-encoding-macro = { version="0.1.12", optional=true } diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index 3f5b2b77cac..3d7ca1c1f56 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -734,7 +734,12 @@ where } // match strftime "%Y-%m-%d %H:%M:%S.%f %z" -const PRETTY_DATETIME_FORMAT: &[time::format_description::FormatItem] = format_description!("[year]-[month]-[day padding:zero] [hour]:[minute]:[second].[subsecond digits:9] [offset_hour sign:mandatory][offset_minute]"); +const PRETTY_DATETIME_FORMAT: &[time::format_description::FormatItem] = format_description!( + "\ +[year]-[month]-[day padding:zero] \ +[hour]:[minute]:[second].[subsecond digits:9] \ +[offset_hour sign:mandatory][offset_minute]" +); pub fn pretty_time(sec: i64, nsec: i64) -> String { // sec == seconds since UNIX_EPOCH From 31c28eeaa98854797eb432aa6c890002c5133b0b Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 29 Apr 2022 10:28:49 +0200 Subject: [PATCH 10/19] fix gnu/tests/touch/60-seconds --- src/uu/touch/src/touch.rs | 18 ++++++++++++++---- tests/by-util/test_touch.rs | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 9a4c0fe5db0..331867b5f13 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -17,6 +17,7 @@ use filetime::*; use std::fs::{self, File}; use std::path::{Path, PathBuf}; use time::macros::{format_description, time}; +use time::Duration; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult, USimpleError}; use uucore::format_usage; @@ -345,16 +346,25 @@ fn parse_timestamp(s: &str) -> UResult { format = YYYYMMDDHHMM_DOT_SS_FORMAT; ts = "20".to_owned() + &ts; } - if (format == YYYYMMDDHHMM_DOT_SS_FORMAT || format == YYMMDDHHMM_DOT_SS_FORMAT) + + let leap_sec = if (format == YYYYMMDDHHMM_DOT_SS_FORMAT || format == YYMMDDHHMM_DOT_SS_FORMAT) && ts.ends_with(".60") { // Work around to disable leap seconds + // Used in gnu/tests/touch/60-seconds ts = ts.replace(".60", ".59"); - } + true + } else { + false + }; + let tm = time::PrimitiveDateTime::parse(&ts, &format) .map_err(|_| USimpleError::new(1, format!("invalid date ts format {}", ts.quote())))?; - - let local = to_local(tm); + let mut local = to_local(tm); + if leap_sec { + // We are dealing with a leap second, add it + local = local.saturating_add(Duration::SECOND); + } let ft = local_dt_to_filetime(local); // // We have to check that ft is valid time. Due to daylight saving diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index 2c538d8b404..d23fb090e1c 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -603,3 +603,21 @@ fn test_no_dereference_no_file() { .stderr_contains("setting times of 'not-a-file-1': No such file or directory") .stderr_contains("setting times of 'not-a-file-2': No such file or directory"); } + +#[test] +fn test_touch_leap_second() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_leap_sec"; + + ucmd.args(&["-t", "197001010000.60", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let epoch = str_to_filetime("%Y%m%d%H%M", "197001010000"); + let (atime, mtime) = get_file_times(&at, file); + assert_eq!(atime, mtime); + assert_eq!(atime.unix_seconds() - epoch.unix_seconds(), 60); + assert_eq!(mtime.unix_seconds() - epoch.unix_seconds(), 60); +} From 9d81d6fef2729db4765529cf4b3b195a4b513626 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 29 Apr 2022 10:32:28 +0200 Subject: [PATCH 11/19] touch: add support of -d '1970-01-01 18:43:33.023456789' --- deny.toml | 2 +- src/uu/touch/src/touch.rs | 74 +++++++++++++++++++++++-------------- tests/by-util/test_touch.rs | 36 ++++++++++++++++++ 3 files changed, 84 insertions(+), 28 deletions(-) diff --git a/deny.toml b/deny.toml index f659910e825..8154bbf9059 100644 --- a/deny.toml +++ b/deny.toml @@ -64,7 +64,7 @@ highlight = "all" # spell-checker: disable skip = [ # getrandom - { name = "wasi", version="0.10.2+wasi-snapshot-preview1" }, + { name = "wasi", version="0.10.0+wasi-snapshot-preview1" }, # blake2d_simd { name = "arrayvec", version = "=0.7.2" }, # flimit/unix_socket diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 331867b5f13..5264401ef43 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -6,7 +6,7 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. -// spell-checker:ignore (ToDO) filetime strptime utcoff strs datetime MMDDhhmm clapv PWSTR lpszfilepath hresult mktime YYYYMMDDHHMM YYMMDDHHMM DATETIME subsecond +// spell-checker:ignore (ToDO) filetime strptime utcoff strs datetime MMDDhhmm clapv PWSTR lpszfilepath hresult mktime YYYYMMDDHHMM YYMMDDHHMM DATETIME YYYYMMDDHHMMS pub extern crate filetime; #[macro_use] @@ -255,6 +255,41 @@ const POSIX_LOCALE_FORMAT: &[time::format_description::FormatItem] = format_desc const ISO_8601_FORMAT: &[time::format_description::FormatItem] = format_description!("[year]-[month]-[day]"); +// "%Y%m%d%H%M.%S" 15 chars +const YYYYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:full][month repr:numerical padding:zero]\ + [day][hour][minute].[second]" +); + +// "%Y-%m-%d %H:%M:%S.%SS" 12 chars +const YYYYMMDDHHMMSS_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:full]-[month repr:numerical padding:zero]-\ + [day] [hour]:[minute]:[second].[subsecond]" +); +// "%Y-%m-%d %H:%M:%S" 12 chars +const YYYYMMDDHHMMS_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:full]-[month repr:numerical padding:zero]-\ + [day] [hour]:[minute]:[second]" +); + +// "%Y%m%d%H%M" 12 chars +const YYYYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:full][month repr:numerical padding:zero]\ + [day][hour][minute]" +); + +// "%y%m%d%H%M.%S" 13 chars +const YYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:last_two padding:none][month][day]\ + [hour][minute].[second]" +); + +// "%y%m%d%H%M" 10 chars +const YYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:last_two padding:none][month padding:zero][day padding:zero]\ + [hour repr:24 padding:zero][minute padding:zero]" +); + 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 @@ -269,8 +304,17 @@ fn parse_date(s: &str) -> UResult { // which is equivalent to the POSIX locale: %a %b %e %H:%M:%S %Y // Tue Dec 3 ... // ("%c", POSIX_LOCALE_FORMAT), - if let Ok(parsed) = time::PrimitiveDateTime::parse(s, &POSIX_LOCALE_FORMAT) { - return Ok(local_dt_to_filetime(to_local(parsed))); + // + // But also support other format found in the GNU tests like + // in tests/misc/stat-nanoseconds.sh + for fmt in [ + POSIX_LOCALE_FORMAT, + YYYYMMDDHHMMS_FORMAT, + YYYYMMDDHHMMSS_FORMAT, + ] { + if let Ok(parsed) = time::PrimitiveDateTime::parse(s, &fmt) { + return Ok(local_dt_to_filetime(to_local(parsed))); + } } // "Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)" @@ -294,30 +338,6 @@ fn parse_date(s: &str) -> UResult { Err(USimpleError::new(1, format!("Unable to parse date: {}", s))) } -// "%Y%m%d%H%M.%S" 15 chars -const YYYYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = format_description!( - "[year repr:full][month repr:numerical padding:zero]\ - [day][hour][minute].[second]" -); - -// "%Y%m%d%H%M" 12 chars -const YYYYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = format_description!( - "[year repr:full][month repr:numerical padding:zero]\ - [day][hour][minute]" -); - -// "%y%m%d%H%M.%S" 13 chars -const YYMMDDHHMM_DOT_SS_FORMAT: &[time::format_description::FormatItem] = format_description!( - "[year repr:last_two padding:none][month][day]\ - [hour][minute].[second]" -); - -// "%y%m%d%H%M" 10 chars -const YYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = format_description!( - "[year repr:last_two padding:none][month padding:zero][day padding:zero]\ - [hour repr:24 padding:zero][minute padding:zero]" -); - fn parse_timestamp(s: &str) -> UResult { // TODO: handle error let now = time::OffsetDateTime::now_utc(); diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index d23fb090e1c..ed62692f4d8 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -427,6 +427,42 @@ fn test_touch_set_date3() { assert_eq!(mtime, expected); } +#[test] +fn test_touch_set_date4() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_date"; + + ucmd.args(&["-d", "1970-01-01 18:43:33", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let expected = FileTime::from_unix_time(60213, 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_date5() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_date"; + + ucmd.args(&["-d", "1970-01-01 18:43:33.023456789", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let expected = FileTime::from_unix_time(60213, 023456789); + 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!(); From 417b4a22d093c225c78d4fffca17b8926a261912 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 1 May 2022 17:07:29 +0200 Subject: [PATCH 12/19] GNU CI: show the new error --- .github/workflows/GnuTests.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.github/workflows/GnuTests.yml b/.github/workflows/GnuTests.yml index d306092c6f5..fedd6e4d535 100644 --- a/.github/workflows/GnuTests.yml +++ b/.github/workflows/GnuTests.yml @@ -170,6 +170,8 @@ jobs: REPO_DEFAULT_BRANCH='${{ steps.vars.outputs.repo_default_branch }}' if test -f "${REF_LOG_FILE}"; then echo "Reference SHA1/ID: $(sha1sum -- "${REF_SUMMARY_FILE}")" + REF_ERROR=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" "${REF_LOG_FILE}" | sort) + NEW_ERROR=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" '${{ steps.vars.outputs.path_GNU_tests }}/test-suite.log' | sort) REF_FAILING=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" "${REF_LOG_FILE}" | sort) NEW_FAILING=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" '${{ steps.vars.outputs.path_GNU_tests }}/test-suite.log' | sort) for LINE in ${REF_FAILING} @@ -186,6 +188,21 @@ jobs: have_new_failures="true" fi done + for LINE in ${REF_ERROR} + do + if ! grep -Fxq ${LINE}<<<"${NEW_ERROR}"; then + echo "::warning ::Congrats! The gnu test ${LINE} is no longer ERROR!" + fi + done + for LINE in ${NEW_ERROR} + do + if ! grep -Fxq ${LINE}<<<"${REF_ERROR}" + then + echo "::error ::GNU test failed: ${LINE}. ${LINE} is passing on '${{ steps.vars.outputs.repo_default_branch }}'. Maybe you have to rebase?" + have_new_failures="true" + fi + done + else echo "::warning ::Skipping test failure comparison; no prior reference test logs are available." fi From e70b99dad0b01758b71faf76a31094541f23112f Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 29 Apr 2022 10:32:28 +0200 Subject: [PATCH 13/19] touch: add support of -d '1970-01-01 18:43:33.023456789' --- src/uu/touch/src/touch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 5264401ef43..1d90ed4d79d 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -6,7 +6,7 @@ // For the full copyright and license information, please view the LICENSE file // that was distributed with this source code. -// spell-checker:ignore (ToDO) filetime strptime utcoff strs datetime MMDDhhmm clapv PWSTR lpszfilepath hresult mktime YYYYMMDDHHMM YYMMDDHHMM DATETIME YYYYMMDDHHMMS +// spell-checker:ignore (ToDO) filetime strptime utcoff strs datetime MMDDhhmm clapv PWSTR lpszfilepath hresult mktime YYYYMMDDHHMM YYMMDDHHMM DATETIME YYYYMMDDHHMMS subsecond pub extern crate filetime; #[macro_use] From 65d0f5ba9f0ba9a4271d293fd0e7538c6a502783 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 2 May 2022 22:32:37 +0200 Subject: [PATCH 14/19] to_local: manage the error --- src/uu/touch/src/touch.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 1d90ed4d79d..8e8e4c45854 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -43,8 +43,13 @@ pub mod options { static ARG_FILES: &str = "files"; fn to_local(tm: time::PrimitiveDateTime) -> time::OffsetDateTime { - // TODO: handle error getting now - tm.assume_offset(time::OffsetDateTime::now_local().unwrap().offset()) + let offset = match time::OffsetDateTime::now_local() { + Ok(lo) => lo.offset(), + Err(e) => { + panic!("error: {}", e); + } + }; + tm.assume_offset(offset) } fn local_dt_to_filetime(dt: time::OffsetDateTime) -> FileTime { From 06ef89b3d822dccbf0dfff1683486498c6fcfc6f Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 5 May 2022 22:48:37 +0200 Subject: [PATCH 15/19] touch: improve the -d option support of other dates --- src/uu/touch/src/touch.rs | 25 +++++++++++++++++-------- tests/by-util/test_touch.rs | 10 ++++++++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 8e8e4c45854..c082aed23cb 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -16,7 +16,7 @@ use clap::{crate_version, Arg, ArgGroup, Command}; use filetime::*; use std::fs::{self, File}; use std::path::{Path, PathBuf}; -use time::macros::{format_description, time}; +use time::macros::{format_description, offset, time}; use time::Duration; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult, USimpleError}; @@ -42,6 +42,7 @@ pub mod options { static ARG_FILES: &str = "files"; +// Convert a date/time to a date with a TZ offset fn to_local(tm: time::PrimitiveDateTime) -> time::OffsetDateTime { let offset = match time::OffsetDateTime::now_local() { Ok(lo) => lo.offset(), @@ -52,10 +53,18 @@ fn to_local(tm: time::PrimitiveDateTime) -> time::OffsetDateTime { tm.assume_offset(offset) } +// Convert a date/time with a TZ offset into a FileTime fn local_dt_to_filetime(dt: time::OffsetDateTime) -> FileTime { FileTime::from_unix_time(dt.unix_timestamp(), dt.nanosecond()) } +// Convert a date/time, considering that the input is in UTC time +// Used for touch -d 1970-01-01 18:43:33.023456789 for example +fn dt_to_filename(tm: time::PrimitiveDateTime) -> FileTime { + let dt = tm.assume_offset(offset!(UTC)); + local_dt_to_filetime(dt) +} + #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().get_matches_from(args); @@ -310,15 +319,15 @@ fn parse_date(s: &str) -> UResult { // Tue Dec 3 ... // ("%c", POSIX_LOCALE_FORMAT), // - // But also support other format found in the GNU tests like + if let Ok(parsed) = time::PrimitiveDateTime::parse(s, &POSIX_LOCALE_FORMAT) { + return Ok(local_dt_to_filetime(to_local(parsed))); + } + + // Also support other formats found in the GNU tests like // in tests/misc/stat-nanoseconds.sh - for fmt in [ - POSIX_LOCALE_FORMAT, - YYYYMMDDHHMMS_FORMAT, - YYYYMMDDHHMMSS_FORMAT, - ] { + for fmt in [YYYYMMDDHHMMS_FORMAT, YYYYMMDDHHMMSS_FORMAT] { if let Ok(parsed) = time::PrimitiveDateTime::parse(s, &fmt) { - return Ok(local_dt_to_filetime(to_local(parsed))); + return Ok(dt_to_filename(parsed)); } } diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index ed62692f4d8..9b8eebbf046 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -438,7 +438,7 @@ fn test_touch_set_date4() { assert!(at.file_exists(file)); - let expected = FileTime::from_unix_time(60213, 0); + let expected = FileTime::from_unix_time(67413, 0); let (atime, mtime) = get_file_times(&at, file); assert_eq!(atime, mtime); assert_eq!(atime, expected); @@ -456,7 +456,13 @@ fn test_touch_set_date5() { assert!(at.file_exists(file)); - let expected = FileTime::from_unix_time(60213, 023456789); + // Slightly different result on Windows for nano seconds + // TODO: investigate + #[cfg(windows)] + let expected = FileTime::from_unix_time(67413, 23456700); + #[cfg(not(windows))] + let expected = FileTime::from_unix_time(67413, 23456789); + let (atime, mtime) = get_file_times(&at, file); assert_eq!(atime, mtime); assert_eq!(atime, expected); From 39520a84ab7ca2515913b0621d91d60f05775fd7 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 6 May 2022 23:54:12 +0200 Subject: [PATCH 16/19] fix the GNU error detection --- .github/workflows/GnuTests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/GnuTests.yml b/.github/workflows/GnuTests.yml index f2b0ddd4531..3a152be22b6 100644 --- a/.github/workflows/GnuTests.yml +++ b/.github/workflows/GnuTests.yml @@ -170,8 +170,8 @@ jobs: REPO_DEFAULT_BRANCH='${{ steps.vars.outputs.repo_default_branch }}' if test -f "${REF_LOG_FILE}"; then echo "Reference SHA1/ID: $(sha1sum -- "${REF_SUMMARY_FILE}")" - REF_ERROR=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" "${REF_LOG_FILE}" | sort) - NEW_ERROR=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" '${{ steps.vars.outputs.path_GNU_tests }}/test-suite.log' | sort) + REF_ERROR=$(sed -n "s/^ERROR: \([[:print:]]\+\).*/\1/p" "${REF_LOG_FILE}" | sort) + NEW_ERROR=$(sed -n "s/^ERROR: \([[:print:]]\+\).*/\1/p" '${{ steps.vars.outputs.path_GNU_tests }}/test-suite.log' | sort) REF_FAILING=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" "${REF_LOG_FILE}" | sort) NEW_FAILING=$(sed -n "s/^FAIL: \([[:print:]]\+\).*/\1/p" '${{ steps.vars.outputs.path_GNU_tests }}/test-suite.log' | sort) for LINE in ${REF_FAILING} From 087d4b14fc205107d557d3300b63d29849888642 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 8 May 2022 21:27:08 +0200 Subject: [PATCH 17/19] Do not use the Rust/touch for tests/ls/abmon-align.sh --- util/build-gnu.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/util/build-gnu.sh b/util/build-gnu.sh index 83993fde93a..942aa9d879f 100755 --- a/util/build-gnu.sh +++ b/util/build-gnu.sh @@ -137,7 +137,8 @@ sed -i 's|chmod |/usr/bin/chmod |' tests/du/inacc-dir.sh tests/tail-2/tail-n0f.s sed -i 's|sort |/usr/bin/sort |' tests/ls/hyperlink.sh tests/misc/test-N.sh sed -i 's|split |/usr/bin/split |' tests/misc/factor-parallel.sh sed -i 's|id -|/usr/bin/id -|' tests/misc/runcon-no-reorder.sh -sed -i 's|touch |/usr/bin/touch |' tests/cp/preserve-link.sh tests/cp/reflink-perm.sh tests/ls/block-size.sh tests/mv/update.sh tests/misc/ls-time.sh tests/misc/stat-nanoseconds.sh tests/misc/time-style.sh tests/misc/test-N.sh +# tests/ls/abmon-align.sh - https://github.com/uutils/coreutils/issues/3505 +sed -i 's|touch |/usr/bin/touch |' tests/cp/preserve-link.sh tests/cp/reflink-perm.sh tests/ls/block-size.sh tests/mv/update.sh tests/misc/ls-time.sh tests/misc/stat-nanoseconds.sh tests/misc/time-style.sh tests/misc/test-N.sh tests/ls/abmon-align.sh sed -i 's|ln -|/usr/bin/ln -|' tests/cp/link-deref.sh sed -i 's|cp |/usr/bin/cp |' tests/mv/hard-2.sh sed -i 's|paste |/usr/bin/paste |' tests/misc/od-endian.sh From d5569847bd682f0df1eab95316fa9c81afe83676 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 8 May 2022 21:31:42 +0200 Subject: [PATCH 18/19] also support for tests/touch/no-rights.sh format --- src/uu/touch/src/touch.rs | 15 ++++++++++++++- tests/by-util/test_touch.rs | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index c082aed23cb..5444c8c1021 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -280,12 +280,20 @@ const YYYYMMDDHHMMSS_FORMAT: &[time::format_description::FormatItem] = format_de "[year repr:full]-[month repr:numerical padding:zero]-\ [day] [hour]:[minute]:[second].[subsecond]" ); + // "%Y-%m-%d %H:%M:%S" 12 chars const YYYYMMDDHHMMS_FORMAT: &[time::format_description::FormatItem] = format_description!( "[year repr:full]-[month repr:numerical padding:zero]-\ [day] [hour]:[minute]:[second]" ); +// "%Y-%m-%d %H:%M" 12 chars +// Used for example in tests/touch/no-rights.sh +const YYYY_MM_DD_HH_MM_FORMAT: &[time::format_description::FormatItem] = format_description!( + "[year repr:full]-[month repr:numerical padding:zero]-\ + [day] [hour]:[minute]" +); + // "%Y%m%d%H%M" 12 chars const YYYYMMDDHHMM_FORMAT: &[time::format_description::FormatItem] = format_description!( "[year repr:full][month repr:numerical padding:zero]\ @@ -325,7 +333,12 @@ fn parse_date(s: &str) -> UResult { // Also support other formats found in the GNU tests like // in tests/misc/stat-nanoseconds.sh - for fmt in [YYYYMMDDHHMMS_FORMAT, YYYYMMDDHHMMSS_FORMAT] { + // or tests/touch/no-rights.sh + for fmt in [ + YYYYMMDDHHMMS_FORMAT, + YYYYMMDDHHMMSS_FORMAT, + YYYY_MM_DD_HH_MM_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 9b8eebbf046..5417a0dbe29 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -469,6 +469,25 @@ fn test_touch_set_date5() { assert_eq!(mtime, expected); } +#[test] +fn test_touch_set_date6() { + let (at, mut ucmd) = at_and_ucmd!(); + let file = "test_touch_set_date"; + + ucmd.args(&["-d", "2000-01-01 00:00", file]) + .succeeds() + .no_stderr(); + + assert!(at.file_exists(file)); + + let expected = FileTime::from_unix_time(946684800, 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!(); From f65d72e334f73aedb38c6e149480733f433f0e8a Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 8 May 2022 21:52:12 +0200 Subject: [PATCH 19/19] 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 5444c8c1021..f71eb81d01b 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 5417a0dbe29..346e279198a 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!();