Skip to content

Commit 26e1757

Browse files
committed
uptime: extract uptime_since fn
1 parent c6b12cf commit 26e1757

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/uu/uptime/src/uptime.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// spell-checker:ignore getloadavg behaviour loadavg uptime upsecs updays upmins uphours boottime nusers utmpxname gettime clockid
77

88
use chrono::{Local, TimeZone, Utc};
9-
use clap::ArgMatches;
109
#[cfg(unix)]
1110
use std::ffi::OsString;
1211
use std::io;
@@ -67,10 +66,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6766
#[cfg(windows)]
6867
let file_path = None;
6968

70-
if let Some(file_path) = file_path {
71-
uptime_with_file(file_path)
69+
if matches.get_flag(options::SINCE) {
70+
uptime_since()
71+
} else if let Some(path) = file_path {
72+
uptime_with_file(path)
7273
} else {
73-
default_uptime(&matches)
74+
default_uptime()
7475
}
7576
}
7677

@@ -191,27 +192,29 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
191192
Ok(())
192193
}
193194

194-
/// Default uptime behaviour i.e. when no file argument is given.
195-
fn default_uptime(matches: &ArgMatches) -> UResult<()> {
196-
if matches.get_flag(options::SINCE) {
197-
#[cfg(unix)]
198-
#[cfg(not(target_os = "openbsd"))]
199-
let (boot_time, _) = process_utmpx(None);
200-
201-
#[cfg(target_os = "openbsd")]
202-
let uptime = get_uptime(None)?;
203-
#[cfg(unix)]
204-
#[cfg(not(target_os = "openbsd"))]
205-
let uptime = get_uptime(boot_time)?;
206-
#[cfg(target_os = "windows")]
207-
let uptime = get_uptime(None)?;
208-
let initial_date = Local
209-
.timestamp_opt(Utc::now().timestamp() - uptime, 0)
210-
.unwrap();
211-
println!("{}", initial_date.format("%Y-%m-%d %H:%M:%S"));
212-
return Ok(());
213-
}
195+
fn uptime_since() -> UResult<()> {
196+
#[cfg(unix)]
197+
#[cfg(not(target_os = "openbsd"))]
198+
let (boot_time, _) = process_utmpx(None);
199+
200+
#[cfg(target_os = "openbsd")]
201+
let uptime = get_uptime(None)?;
202+
#[cfg(unix)]
203+
#[cfg(not(target_os = "openbsd"))]
204+
let uptime = get_uptime(boot_time)?;
205+
#[cfg(target_os = "windows")]
206+
let uptime = get_uptime(None)?;
207+
208+
let initial_date = Local
209+
.timestamp_opt(Utc::now().timestamp() - uptime, 0)
210+
.unwrap();
211+
println!("{}", initial_date.format("%Y-%m-%d %H:%M:%S"));
214212

213+
Ok(())
214+
}
215+
216+
/// Default uptime behaviour i.e. when no file argument is given.
217+
fn default_uptime() -> UResult<()> {
215218
print_time();
216219
print_uptime(None)?;
217220
print_nusers(None);

0 commit comments

Comments
 (0)