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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! # secfmt
//!
//! `secfmt` converts seconds into a human readable format (struct) containing years, days, hours, minutes and seconds.

#[derive(Debug)]
pub struct Secfmt {
    pub years: u8,
    pub days: u8,
    pub hours: u8,
    pub minutes: u8,
    pub seconds: u8,
}

/// Converts seconds into a human readable format (struct) containing years, days, hours, minutes and seconds.
///
/// # Examples
///
/// ```
/// let seconds = 31537529;
/// let seconds_human_readable = secfmt::from(seconds);
///
/// assert_eq!(1, secfmt::from(31537529).years);
/// assert_eq!(0, secfmt::from(31537529).days);
/// assert_eq!(0, secfmt::from(31537529).hours);
/// assert_eq!(25, secfmt::from(31537529).minutes);
/// assert_eq!(29, secfmt::from(31537529).seconds);
/// ```
pub fn from(s: u64) -> Secfmt {
    let (mut days, mut hours, mut minutes, mut seconds) = (0, 0, 0, 0);

    let years = s / 31536000;
    let mut remainder = s % 31536000;
    if remainder > 0 {
        days = remainder / 86400;
        remainder = remainder % 86400;
        if remainder > 0 {
            hours = remainder / 3600;
            remainder = remainder % 3600;
            if remainder > 0 {
                minutes = remainder / 60;
                seconds = remainder % 60;
            }
        }
    }

    Secfmt {
        years: years as u8,
        days: days as u8,
        hours: hours as u8,
        minutes: minutes as u8,
        seconds: seconds as u8,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_years() {
        assert_eq!(1, from(31537529).years);
    }

    #[test]
    fn test_days() {
        assert_eq!(0, from(31537529).days);
    }

    #[test]
    fn test_hours() {
        assert_eq!(0, from(31537529).hours);
    }

    #[test]
    fn test_minutes() {
        assert_eq!(25, from(31537529).minutes);
    }

    #[test]
    fn test_seconds() {
        assert_eq!(29, from(31537529).seconds);
    }

    #[test]
    fn test_format() {
        let shr = from(31537529);
        let s = format!("{}y {}d {}h {}m {}s", shr.years, shr.days, shr.hours, shr.minutes, shr.seconds);
        assert_eq!("1y 0d 0h 25m 29s", s);
    }
}