1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::io::{BufRead};
pub struct Uptic {
pub days : u64,
pub hours : u8,
pub minutes: u8,
pub raw_ss : u64
}
impl Default for Uptic {
fn default() -> Self {
let mut data = Uptic {
days : 0,
hours : 0,
minutes: 0,
raw_ss : 0
}; data.init();
data
}
}
impl Uptic {
fn read_lines<P>(&self, file: &P) -> std::io::Result<
std::io::Lines<std::io::BufReader<std::fs::File>>
> where P: AsRef<std::path::Path>, {
Ok(std::io::BufReader::new(
std::fs::File::open(file)?).lines())
}
#[cfg(not(target_os = "windows"))]
fn init(&mut self) {
if std::path::Path::new("/proc/uptime").exists() {
if let Ok(lines) = self.read_lines(&"/proc/uptime") {
for line in lines {
if let Ok(__line__) = line {
self.raw_ss = __line__.split(' ').next().unwrap().parse::<f64>().unwrap() as u64;
}
}
self.days = (self.raw_ss / 60 / 60 / 24) ^ 0;
self.hours = ((self.raw_ss / 60 / 60 % 24) ^ 0) as u8;
self.minutes = ((self.raw_ss / 60 % 60) ^ 0) as u8;
}
}
}
}
#[cfg(test)]
mod tests {
use crate::{Uptic};
#[test]
fn hmm() {
let uptime = Uptic::default();
println!("d/s({}), h/s({}), m/s({})", uptime.days, uptime.hours, uptime.minutes);
}
}