From 26e175757dbd08446dcc8f05f4cab5b48c775274 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 6 May 2025 16:59:23 +0200 Subject: [PATCH] uptime: extract uptime_since fn --- src/uu/uptime/src/uptime.rs | 51 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/uu/uptime/src/uptime.rs b/src/uu/uptime/src/uptime.rs index f5b924bc9..e001a64a8 100644 --- a/src/uu/uptime/src/uptime.rs +++ b/src/uu/uptime/src/uptime.rs @@ -6,7 +6,6 @@ // spell-checker:ignore getloadavg behaviour loadavg uptime upsecs updays upmins uphours boottime nusers utmpxname gettime clockid use chrono::{Local, TimeZone, Utc}; -use clap::ArgMatches; #[cfg(unix)] use std::ffi::OsString; use std::io; @@ -67,10 +66,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { #[cfg(windows)] let file_path = None; - if let Some(file_path) = file_path { - uptime_with_file(file_path) + if matches.get_flag(options::SINCE) { + uptime_since() + } else if let Some(path) = file_path { + uptime_with_file(path) } else { - default_uptime(&matches) + default_uptime() } } @@ -191,27 +192,29 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> { 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. -fn default_uptime(matches: &ArgMatches) -> 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(()); - } - +fn default_uptime() -> UResult<()> { print_time(); print_uptime(None)?; print_nusers(None);