From 96b47f199991657c23865b583155f110500070b4 Mon Sep 17 00:00:00 2001 From: Mustafa Salih <37256744+MustafaSalih1993@users.noreply.github.com> Date: Tue, 11 May 2021 19:07:01 +0300 Subject: [PATCH] Getting uptime programmatically (#55) * added limits 30 chars foreach the title and artist in spotify block, closes: #53 * Getting uptime programmatically --- src/utils/uptime.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/utils/uptime.rs b/src/utils/uptime.rs index 140c4c4..4bb06cc 100644 --- a/src/utils/uptime.rs +++ b/src/utils/uptime.rs @@ -1,17 +1,14 @@ use crate::config::CONFIG; use crate::types::ThreadsData; -use std::fs::read_to_string; +use chrono::Duration; +use nix::sys::sysinfo; pub fn get_uptime() -> ThreadsData { - let buf = match read_to_string("/proc/uptime") { - Ok(data) => data, - _ => return ThreadsData::Uptime("cant find uptime file!".to_string()), - }; + let duration = sysinfo::sysinfo().unwrap().uptime(); + let uptime_sec = Duration::from_std(duration).unwrap().num_seconds(); - let buf: f32 = buf.split(' ').collect::>()[0].parse().unwrap(); - - let hour = buf.round() as u32 / 3600; - let rem = buf as u32 - hour * 3600; + let hour = uptime_sec / 3600; + let rem = uptime_sec - hour * 3600; let minutes = rem / 60; let uptime = if hour > 0 {