From 52706372aa23f3433a5fe05e226a2fb6d9f57c01 Mon Sep 17 00:00:00 2001 From: paulotten Date: Wed, 7 Apr 2021 02:41:04 -0400 Subject: [PATCH] Replace outdated time 0.1 dependancy with latest version of chrono (#2044) * Replace outdated time 0.1 dependancy with latest version of chrono I also noticed that times are being miscalculated on linux, so I fixed that. * Add time test for issue #2042 * Cleanup use declarations * Tie time test to `touch` feature - if we compile with the right OS feature flag then we should have it, even on Windows --- Cargo.lock | 2 +- src/uu/du/Cargo.toml | 2 +- src/uu/du/src/du.rs | 17 +++++++++-------- tests/by-util/test_du.rs | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f65ba3dff..41a384ea4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1777,7 +1777,7 @@ dependencies = [ name = "uu_du" version = "0.0.6" dependencies = [ - "time", + "chrono", "uucore", "uucore_procs", "winapi 0.3.9", diff --git a/src/uu/du/Cargo.toml b/src/uu/du/Cargo.toml index eb7b23f8b..3ce9d8361 100644 --- a/src/uu/du/Cargo.toml +++ b/src/uu/du/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" path = "src/du.rs" [dependencies] -time = "0.1.40" +chrono = "0.4" uucore = { version=">=0.0.8", package="uucore", path="../../uucore" } uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" } winapi = { version="0.3", features=[] } diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 9c8bb9794..615b66a4e 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -10,6 +10,8 @@ #[macro_use] extern crate uucore; +use chrono::prelude::DateTime; +use chrono::Local; use std::collections::HashSet; use std::env; use std::fs; @@ -22,7 +24,7 @@ use std::os::windows::fs::MetadataExt; #[cfg(windows)] use std::os::windows::io::AsRawHandle; use std::path::PathBuf; -use time::Timespec; +use std::time::{Duration, UNIX_EPOCH}; #[cfg(windows)] use winapi::shared::minwindef::{DWORD, LPVOID}; #[cfg(windows)] @@ -118,7 +120,7 @@ impl Stat { // https://doc.rust-lang.org/std/os/windows/fs/trait.MetadataExt.html#tymethod.creation_time // "The returned 64-bit value [...] which represents the number of 100-nanosecond intervals since January 1, 1601 (UTC)." fn windows_time_to_unix_time(win_time: u64) -> u64 { - win_time / 10_000 - 11_644_473_600_000 + win_time / 10_000_000 - 11_644_473_600 } #[cfg(windows)] @@ -555,8 +557,8 @@ Try '{} --help' for more information.", }; if matches.opt_present("time") { let tm = { - let (secs, nsecs) = { - let time = match matches.opt_str("time") { + let secs = { + match matches.opt_str("time") { Some(s) => match &s[..] { "accessed" => stat.accessed, "created" => stat.created, @@ -573,13 +575,12 @@ Try '{} --help' for more information.", } }, None => stat.modified, - }; - ((time / 1000) as i64, (time % 1000 * 1_000_000) as i32) + } }; - time::at(Timespec::new(secs, nsecs)) + DateTime::::from(UNIX_EPOCH + Duration::from_secs(secs)) }; if !summarize || index == len - 1 { - let time_str = tm.strftime(time_format_str).unwrap(); + let time_str = tm.format(time_format_str).to_string(); print!( "{}\t{}\t{}{}", convert_size(size), diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index b3b1b3465..30dcd9bb3 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -172,3 +172,21 @@ fn test_du_h_flag_empty_file() { assert_eq!(result.stderr, ""); assert_eq!(result.stdout, "0\tempty.txt\n"); } + +#[cfg(feature = "touch")] +#[test] +fn test_du_time() { + let ts = TestScenario::new("du"); + + let touch = ts.ccmd("touch").arg("-a").arg("-m").arg("-t").arg("201505150000").arg("date_test").run(); + assert!(touch.success); + + let result = ts.ucmd().arg("--time").arg("date_test").run(); + + // cleanup by removing test file + ts.cmd("rm").arg("date_test").run(); + + assert!(result.success); + assert_eq!(result.stderr, ""); + assert_eq!(result.stdout, "0\t2015-05-15 00:00\tdate_test\n"); +}