|
6 | 6 | // spell-checker:ignore getloadavg behaviour loadavg uptime upsecs updays upmins uphours boottime nusers utmpxname gettime clockid
|
7 | 7 |
|
8 | 8 | use chrono::{Local, TimeZone, Utc};
|
9 |
| -use clap::ArgMatches; |
10 | 9 | #[cfg(unix)]
|
11 | 10 | use std::ffi::OsString;
|
12 | 11 | use std::io;
|
@@ -67,10 +66,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
67 | 66 | #[cfg(windows)]
|
68 | 67 | let file_path = None;
|
69 | 68 |
|
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) |
72 | 73 | } else {
|
73 |
| - default_uptime(&matches) |
| 74 | + default_uptime() |
74 | 75 | }
|
75 | 76 | }
|
76 | 77 |
|
@@ -191,27 +192,29 @@ fn uptime_with_file(file_path: &OsString) -> UResult<()> {
|
191 | 192 | Ok(())
|
192 | 193 | }
|
193 | 194 |
|
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")); |
214 | 212 |
|
| 213 | + Ok(()) |
| 214 | +} |
| 215 | + |
| 216 | +/// Default uptime behaviour i.e. when no file argument is given. |
| 217 | +fn default_uptime() -> UResult<()> { |
215 | 218 | print_time();
|
216 | 219 | print_uptime(None)?;
|
217 | 220 | print_nusers(None);
|
|
0 commit comments