mirror of
https://github.com/NotAShelf/microfetch.git
synced 2024-11-01 14:51:14 +00:00
receive uptime from nix crate
This commit is contained in:
parent
2ecf9303fa
commit
0fb8fbaae6
1 changed files with 5 additions and 16 deletions
|
@ -1,21 +1,10 @@
|
||||||
use std::fs::File;
|
use color_eyre::Result;
|
||||||
use std::io::{self, BufRead, BufReader};
|
use nix::sys::sysinfo::sysinfo;
|
||||||
use std::path::Path;
|
use std::io;
|
||||||
|
|
||||||
pub fn get_current() -> Result<String, io::Error> {
|
pub fn get_current() -> Result<String, io::Error> {
|
||||||
let path = Path::new("/proc/uptime");
|
let info = sysinfo().map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
||||||
let file = File::open(path)?;
|
let uptime_seconds = info.uptime().as_secs_f64();
|
||||||
let mut reader = BufReader::new(file);
|
|
||||||
|
|
||||||
let mut line = String::new();
|
|
||||||
reader.read_line(&mut line)?;
|
|
||||||
|
|
||||||
let uptime_seconds: f64 = line
|
|
||||||
.split_whitespace()
|
|
||||||
.next()
|
|
||||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Failed to parse uptime"))?
|
|
||||||
.parse()
|
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
|
|
||||||
|
|
||||||
let total_minutes = (uptime_seconds / 60.0).round() as u64;
|
let total_minutes = (uptime_seconds / 60.0).round() as u64;
|
||||||
let days = total_minutes / (60 * 24);
|
let days = total_minutes / (60 * 24);
|
||||||
|
|
Loading…
Reference in a new issue