diff --git a/Cargo.lock b/Cargo.lock index 8e256e39..34e260f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,6 +160,27 @@ dependencies = [ "windows-link", ] +[[package]] +name = "chrono-tz" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efdce149c370f133a071ca8ef6ea340b7b88748ab0810097a9e2976eaa34b4f3" +dependencies = [ + "chrono", + "chrono-tz-build", + "phf", +] + +[[package]] +name = "chrono-tz-build" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f10f8c9340e31fc120ff885fcdb54a0b48e474bbd77cab557f0c30a3e569402" +dependencies = [ + "parse-zoneinfo", + "phf_codegen", +] + [[package]] name = "clap" version = "4.5.39" @@ -905,6 +926,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "parse-zoneinfo" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f2a05b18d44e2957b88f96ba460715e295bc1d7510468a2f3d3b44535d26c24" +dependencies = [ + "regex", +] + [[package]] name = "paste" version = "1.0.15" @@ -1770,6 +1800,7 @@ name = "uu_vmstat" version = "0.0.1" dependencies = [ "bytesize", + "chrono", "clap", "terminal_size", "uucore", @@ -1800,10 +1831,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9032bf981784f22fcc5ddc7e74b7cf3bae3d5f44a48d2054138ed38068b9f4e0" dependencies = [ "chrono", + "chrono-tz", "clap", "dns-lookup", "fluent", "fluent-bundle", + "iana-time-zone", "libc", "nix", "number_prefix", diff --git a/src/uu/vmstat/Cargo.toml b/src/uu/vmstat/Cargo.toml index 9669fb0b..6538ed46 100644 --- a/src/uu/vmstat/Cargo.toml +++ b/src/uu/vmstat/Cargo.toml @@ -12,9 +12,10 @@ version.workspace = true [dependencies] bytesize = { workspace = true } +chrono = { workspace = true, default-features = false, features = ["clock"] } clap = { workspace = true } terminal_size = { workspace = true } -uucore = { workspace = true } +uucore = { workspace = true, features = ["custom-tz-fmt"] } [lib] path = "src/vmstat.rs" diff --git a/src/uu/vmstat/src/picker.rs b/src/uu/vmstat/src/picker.rs index 1228ead5..357fc923 100644 --- a/src/uu/vmstat/src/picker.rs +++ b/src/uu/vmstat/src/picker.rs @@ -17,7 +17,7 @@ pub type Picker = ( #[cfg(target_os = "linux")] pub fn get_pickers(matches: &ArgMatches) -> Vec { let wide = matches.get_flag("wide"); - vec![ + let mut pickers = vec![ concat_helper( if wide { ("--procs--".into(), " r b".into()) @@ -62,7 +62,18 @@ pub fn get_pickers(matches: &ArgMatches) -> Vec { }, get_cpu_info, ), - ] + ]; + if matches.get_flag("timestamp") { + pickers.push(concat_helper( + ( + "-----timestamp-----".into(), + format!("{:>19}", uucore::custom_tz_fmt::custom_time_format("%Z")), + ), + get_timestamp, + )); + } + + pickers } #[cfg(target_os = "linux")] @@ -267,3 +278,15 @@ fn get_cpu_info( (len, format!("{:.0}", cpu_load.guest)), ] } + +#[cfg(target_os = "linux")] +fn get_timestamp( + _proc_data: &ProcData, + _proc_data_before: Option<&ProcData>, + _matches: &ArgMatches, +) -> Vec<(usize, String)> { + vec![( + 10, + chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(), + )] +} diff --git a/src/uu/vmstat/src/vmstat.rs b/src/uu/vmstat/src/vmstat.rs index 0fc1c17c..93996afa 100644 --- a/src/uu/vmstat/src/vmstat.rs +++ b/src/uu/vmstat/src/vmstat.rs @@ -133,7 +133,7 @@ pub fn uu_app() -> Command { // arg!(-D --"disk-sum" "Report some summary statistics about disk activity"), // arg!(-p --partition "Detailed statistics about partition"), arg!(-S --unit "Switches outputs between 1000 (k), 1024 (K), 1000000 (m), or 1048576 (M) bytes"), - // arg!(-t --timestamp "Append timestamp to each line"), + arg!(-t --timestamp "Append timestamp to each line"), arg!(-w --wide "Wide output mode"), arg!(-y --"no-first" "Omits first report with statistics since system boot"), ]) diff --git a/tests/by-util/test_vmstat.rs b/tests/by-util/test_vmstat.rs index f26cf969..a37a790d 100644 --- a/tests/by-util/test_vmstat.rs +++ b/tests/by-util/test_vmstat.rs @@ -69,3 +69,15 @@ fn test_active() { .unwrap() .contains("active")); } + +#[test] +#[cfg(target_os = "linux")] +fn test_timestamp() { + let result = new_ucmd!().arg("-t").succeeds(); + assert!(result + .stdout_str() + .lines() + .next() + .unwrap() + .contains("timestamp")); +}