receive uptime from nix crate

This commit is contained in:
raf 2024-08-05 01:37:13 +03:00
parent 2ecf9303fa
commit 0fb8fbaae6
Signed by: NotAShelf
GPG key ID: AF26552424E53993

View file

@ -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);