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
72
73
74
75
/// Get a users idle time.
/// ```rust
/// use user_idle::UserIdle;
/// let idle = UserIdle::get_time().unwrap();
/// let idle_seconds = idle.as_seconds();
/// let idle_minutes = idle.as_minutes();
/// // Check the documentation for more methods
/// ```

mod error;
pub use error::Error;

#[cfg(unix)]
mod unix;

#[cfg(windows)]
mod windows;

pub struct UserIdle {
    seconds: u32
}

impl UserIdle {

    /// Get the idle time
    pub fn get_time() -> Result<Self, Error> {

        #[cfg(unix)]
        let seconds = unix::get_idle_time()?;

        #[cfg(windows)]
        let seconds = windows::get_idle_time()?;

        Ok(UserIdle {
            seconds: seconds
        })

    }

    /// Get time in seconds
    pub fn as_seconds(&self) -> u32 {
        self.seconds
    }

    /// Get time in minutes
    pub fn as_minutes(&self) -> u32 {
        self.as_seconds() / 60
    }

    /// Get time in hours
    pub fn as_hours(&self) -> u32 {
        self.as_minutes() / 60
    }

    /// Get time in days
    pub fn as_days(&self) -> u32 {
        self.as_hours() / 24
    }

    /// Get time in weeks
    pub fn as_weeks(&self) -> u32 {
        self.as_days() / 7
    }

    /// Get time in months
    pub fn as_months(&self) -> u32 {
        self.as_weeks() / 4
    }

    /// Get time in years
    pub fn as_years(&self) -> u32 {
        self.as_months() / 12
    }

}