1
Fork 0
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:
Daniel Hofstetter 2025-05-06 16:59:23 +02:00
parent c6b12cfb96
commit 26e175757d

View file

@ -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,27 +192,29 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
Ok(()) Ok(())
} }
fn uptime_since() -> UResult<()> {
#[cfg(unix)]
#[cfg(not(target_os = "openbsd"))]
let (boot_time, _) = process_utmpx(None);
#[cfg(target_os = "openbsd")]
let uptime = get_uptime(None)?;
#[cfg(unix)]
#[cfg(not(target_os = "openbsd"))]
let uptime = get_uptime(boot_time)?;
#[cfg(target_os = "windows")]
let uptime = get_uptime(None)?;
let initial_date = Local
.timestamp_opt(Utc::now().timestamp() - uptime, 0)
.unwrap();
println!("{}", initial_date.format("%Y-%m-%d %H:%M:%S"));
Ok(())
}
/// Default uptime behaviour i.e. when no file argument is given. /// Default uptime behaviour i.e. when no file argument is given.
fn default_uptime(matches: &ArgMatches) -> UResult<()> { fn default_uptime() -> UResult<()> {
if matches.get_flag(options::SINCE) {
#[cfg(unix)]
#[cfg(not(target_os = "openbsd"))]
let (boot_time, _) = process_utmpx(None);
#[cfg(target_os = "openbsd")]
let uptime = get_uptime(None)?;
#[cfg(unix)]
#[cfg(not(target_os = "openbsd"))]
let uptime = get_uptime(boot_time)?;
#[cfg(target_os = "windows")]
let uptime = get_uptime(None)?;
let initial_date = Local
.timestamp_opt(Utc::now().timestamp() - uptime, 0)
.unwrap();
println!("{}", initial_date.format("%Y-%m-%d %H:%M:%S"));
return Ok(());
}
print_time(); print_time();
print_uptime(None)?; print_uptime(None)?;
print_nusers(None); print_nusers(None);