From c8ece3e9726a7cbc54da8e7ac77bc5c33866c9a2 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Thu, 23 Mar 2023 18:16:57 +0100 Subject: [PATCH] test_touch: test a specific time with DST problems Use a specific time to check whether any DST problems arise. The test is also ignored, because this functionality seems to be broken at the moment. Still, this is an improvement, because it is more reliable and does not need to obtain a local offset, which might lead to a panic in the time library. --- tests/by-util/test_touch.rs | 62 +++++++------------------------------ 1 file changed, 11 insertions(+), 51 deletions(-) diff --git a/tests/by-util/test_touch.rs b/tests/by-util/test_touch.rs index 6267e018e..0c0a0b042 100644 --- a/tests/by-util/test_touch.rs +++ b/tests/by-util/test_touch.rs @@ -10,7 +10,7 @@ extern crate touch; use self::touch::filetime::{self, FileTime}; extern crate time; -use time::macros::{datetime, format_description}; +use time::macros::format_description; use crate::common::util::{AtPath, TestScenario}; use std::fs::remove_file; @@ -625,61 +625,21 @@ 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 -// } - -// get_dst_switch_hour returns date string for which touch -m -t fails. -// For example, in EST (UTC-5), that will be "202003080200" so -// touch -m -t 202003080200 file -// fails (that date/time does not exist). -// 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 = match time::OffsetDateTime::now_local() { - Ok(now) => now, - Err(e) => { - panic!("Error {e} retrieving the OffsetDateTime::now_local"); - } - }; - - // Start from January 1, 2020, 00:00. - 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 -} - #[test] +#[ignore = "not implemented"] fn test_touch_mtime_dst_fails() { let (_at, mut ucmd) = at_and_ucmd!(); let file = "test_touch_set_mtime_dst_fails"; - if let Some(s) = get_dst_switch_hour() { - ucmd.args(&["-m", "-t", &s, file]).fails(); - } + // Some timezones use daylight savings time, this leads to problems if the + // specified time is within the jump forward. In EST (UTC-5), there is a + // jump from 1:59AM to 3:00AM on, March 8 2020, so any thing in-between is + // invalid. + // See https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html + // for information on the TZ variable, which where the string is copied from. + ucmd.env("TZ", "EST+5EDT,M3.2.0/2,M11.1.0/2") + .args(&["-m", "-t", "202003080200", file]) + .fails(); } #[test]