mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
uptime: extract uptime_since fn
This commit is contained in:
parent
c6b12cfb96
commit
26e175757d
1 changed files with 27 additions and 24 deletions
|
@ -6,7 +6,6 @@
|
||||||
// spell-checker:ignore getloadavg behaviour loadavg uptime upsecs updays upmins uphours boottime nusers utmpxname gettime clockid
|
// spell-checker:ignore getloadavg behaviour loadavg uptime upsecs updays upmins uphours boottime nusers utmpxname gettime clockid
|
||||||
|
|
||||||
use chrono::{Local, TimeZone, Utc};
|
use chrono::{Local, TimeZone, Utc};
|
||||||
use clap::ArgMatches;
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
@ -67,10 +66,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
let file_path = None;
|
let file_path = None;
|
||||||
|
|
||||||
if let Some(file_path) = file_path {
|
if matches.get_flag(options::SINCE) {
|
||||||
uptime_with_file(file_path)
|
uptime_since()
|
||||||
|
} else if let Some(path) = file_path {
|
||||||
|
uptime_with_file(path)
|
||||||
} else {
|
} else {
|
||||||
default_uptime(&matches)
|
default_uptime()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,9 +192,7 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default uptime behaviour i.e. when no file argument is given.
|
fn uptime_since() -> UResult<()> {
|
||||||
fn default_uptime(matches: &ArgMatches) -> UResult<()> {
|
|
||||||
if matches.get_flag(options::SINCE) {
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
#[cfg(not(target_os = "openbsd"))]
|
#[cfg(not(target_os = "openbsd"))]
|
||||||
let (boot_time, _) = process_utmpx(None);
|
let (boot_time, _) = process_utmpx(None);
|
||||||
|
@ -205,13 +204,17 @@ fn default_uptime(matches: &ArgMatches) -> UResult<()> {
|
||||||
let uptime = get_uptime(boot_time)?;
|
let uptime = get_uptime(boot_time)?;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
let uptime = get_uptime(None)?;
|
let uptime = get_uptime(None)?;
|
||||||
|
|
||||||
let initial_date = Local
|
let initial_date = Local
|
||||||
.timestamp_opt(Utc::now().timestamp() - uptime, 0)
|
.timestamp_opt(Utc::now().timestamp() - uptime, 0)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
println!("{}", initial_date.format("%Y-%m-%d %H:%M:%S"));
|
println!("{}", initial_date.format("%Y-%m-%d %H:%M:%S"));
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Default uptime behaviour i.e. when no file argument is given.
|
||||||
|
fn default_uptime() -> UResult<()> {
|
||||||
print_time();
|
print_time();
|
||||||
print_uptime(None)?;
|
print_uptime(None)?;
|
||||||
print_nusers(None);
|
print_nusers(None);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue