mirror of
https://github.com/uutils/procps.git
synced 2026-06-10 16:14:00 -07:00
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 5f6e0cbe67.
* ps: Change variables' names for `format_time` and beautify `test_time`
This commit is contained in:
+38
-6
@@ -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<ProcessInformation>) -> String {
|
||||
let cumulative_cpu_time = {
|
||||
let utime = proc_info.borrow_mut().stat()[13].parse::<i64>().unwrap();
|
||||
let stime = proc_info.borrow_mut().stat()[14].parse::<i64>().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<ProcessInformation>) -> String {
|
||||
@@ -70,3 +79,26 @@ fn cmd(proc_info: RefCell<ProcessInformation>) -> String {
|
||||
fn ucmd(proc_info: RefCell<ProcessInformation>) -> 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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user