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
// MIT License
//
// Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
// Distributed under the terms of the MIT License.
//
//
// uptic - easy-to-use uptime crate for linux.
//
// there's Default implemented for Uptic structure, you can use it like this:
//
// let uptime = Uptic::default();

use std::io::{BufRead};

pub struct Uptic {
    pub days   : u64,
    pub hours  : u8,  // < 24
    pub minutes: u8,  // < 60
    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);
    }
}