time: Various fixes

This commit is contained in:
Sylvestre Ledru 2022-04-23 21:32:35 +02:00
parent f810b55d86
commit 3a576f2441
8 changed files with 69 additions and 19 deletions

View file

@ -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"]

View file

@ -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

30
Cargo.lock generated
View file

@ -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]]
<<name = "typenum"
name = "time"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd"
dependencies = [
"itoa",
"libc",
"num_threads",
"time-macros",
]
[[package]]
name = "time-macros"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792"
[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"

View file

@ -6,8 +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
// spell-checker:ignore (ToDO) filetime strptime utcoff strs datetime MMDDhhmm clapv PWSTR lpszfilepath hresult mktime YYYYMMDDHHMM YYMMDDHHMM DATETIME subsecond
pub extern crate filetime;
#[macro_use]

View file

@ -9,7 +9,7 @@
//! Set of functions to manage file systems
// spell-checker:ignore (arch) bitrig ; (fs) cifs smbfs
// spell-checker:ignore DATETIME subsecond (arch) bitrig ; (fs) cifs smbfs
extern crate time;
use time::macros::format_description;

View file

@ -189,8 +189,8 @@ impl Utmpx {
}
/// A.K.A. ut.ut_tv
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)
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)

View file

@ -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),

View file

@ -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<String> {
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);