From e9c9eca2ee580ecb405eec787fbf6f8b98fcacf2 Mon Sep 17 00:00:00 2001 From: MCredbear <40625974+MCredbear@users.noreply.github.com> Date: Mon, 12 Aug 2024 21:39:01 +0800 Subject: [PATCH 1/2] ps: Fix the `time` code (#172) * ps: Fix the `time` code (uutils#160) * ps: Fix the `time_test` code (uutils#160) * ps: Add `[DD]-` format to the `time` and 'time_test' code (uutils#160) * ps: Pass `cargo clippy` lint testing (uutils#160) * ps: Beatify `test_time` code (uutils#160) * ps: Format code (uutils#160) * ps: Move time formatting logic to a new function `format_time` * free: Fix incorrect `CachUse` value with `--line --wide` * Revert "free: Fix incorrect `CachUse` value with `--line --wide`" This reverts commit 5f6e0cbe678f98dded0ab67eaec3d25b96ffb0e0. * ps: Change variables' names for `format_time` and beautify `test_time` --- src/uu/ps/src/picker.rs | 44 +++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/uu/ps/src/picker.rs b/src/uu/ps/src/picker.rs index d2179a9..85f0424 100644 --- a/src/uu/ps/src/picker.rs +++ b/src/uu/ps/src/picker.rs @@ -5,7 +5,6 @@ use std::cell::RefCell; -use chrono::DateTime; use uu_pgrep::process::{ProcessInformation, Teletype}; pub(crate) fn collect_pickers( @@ -54,13 +53,23 @@ fn time(proc_info: RefCell) -> String { let cumulative_cpu_time = { let utime = proc_info.borrow_mut().stat()[13].parse::().unwrap(); let stime = proc_info.borrow_mut().stat()[14].parse::().unwrap(); - utime + stime + (utime + stime) / 100 }; - DateTime::from_timestamp_millis(cumulative_cpu_time) - .unwrap() - .format("%H:%M:%S") - .to_string() + format_time(cumulative_cpu_time) +} + +fn format_time(seconds: i64) -> String { + let day = seconds / (3600 * 24); + let hour = (seconds % (3600 * 24)) / 3600; + let minute = (seconds % 3600) / 60; + let second = seconds % 60; + + if day != 0 { + format!("{:02}-{:02}:{:02}:{:02}", day, hour, minute, second) + } else { + format!("{:02}:{:02}:{:02}", hour, minute, second) + } } fn cmd(proc_info: RefCell) -> String { @@ -70,3 +79,26 @@ fn cmd(proc_info: RefCell) -> String { fn ucmd(proc_info: RefCell) -> String { proc_info.borrow_mut().status().get("Name").unwrap().into() } + +#[test] +fn test_time() { + let formatted = { + let time = { + let utime = 29i64; + let stime = 18439i64; + (utime + stime) / 100 + }; + format_time(time) + }; + assert_eq!(formatted, "00:03:04"); + + let formatted = { + let time = { + let utime = 12345678i64; + let stime = 90i64; + (utime + stime) / 100 + }; + format_time(time) + }; + assert_eq!(formatted, "01-10:17:37"); +} From 763d026d0b0d8bb6dc01beb210a0cccbc613fed0 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 12 Aug 2024 20:44:06 +0000 Subject: [PATCH 2/2] chore(deps): update rust crate clap_complete to v4.5.16 --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e2ecf28..f2492ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -164,9 +164,9 @@ dependencies = [ [[package]] name = "clap_complete" -version = "4.5.14" +version = "4.5.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d11bff0290e9a266fc9b4ce6fa96c2bf2ca3f9724c41c10202ac1daf7a087f8" +checksum = "9c677cd0126f3026d8b093fa29eae5d812fde5c05bc66dbb29d0374eea95113a" dependencies = [ "clap", ]