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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use chrono::Duration;
use std::fmt;

pub trait ChronoHumanDuration {
    type Displayer: fmt::Display;
    fn format_human(&self) -> Self::Displayer;
}

impl ChronoHumanDuration for Duration {
    type Displayer = Displayer;
    fn format_human(&self) -> Self::Displayer {
        Displayer { d: *self }
    }
}

pub struct Displayer {
    d: Duration,
}

impl fmt::Display for Displayer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut wrote = false;
        let d = self.d;

        let months = d.num_weeks() / 4;
        if months > 0 {
            write!(f, "{} month{}", months, if months > 1 { "s" } else { "" })?;
            wrote = true;
        } else {
            let weeks = d.num_weeks();
            if weeks > 0 {
                write!(
                    f,
                    "{}{} week{}",
                    if wrote { ", " } else { "" },
                    weeks,
                    if weeks > 1 { "s" } else { "" }
                )?;
                wrote = true;
            } else {
                let days = d.num_days();
                if days > 0 {
                    write!(
                        f,
                        "{}{} day{}",
                        if wrote { ", " } else { "" },
                        days,
                        if days > 1 { "s" } else { "" }
                    )?;
                    wrote = true;
                } else {
                    let hours = d.num_hours();
                    if hours > 0 {
                        write!(
                            f,
                            "{}{} hour{}",
                            if wrote { ", " } else { "" },
                            hours,
                            if hours > 1 { "s" } else { "" }
                        )?;
                        wrote = true;
                    } else {
                        let minutes = d.num_minutes();
                        if minutes > 0 {
                            write!(
                                f,
                                "{}{} minute{}",
                                if wrote { ", " } else { "" },
                                minutes,
                                if minutes > 1 { "s" } else { "" }
                            )?;
                            wrote = true;
                        }
                    }
                }
            }
        }

        if wrote {
            write!(f, " ago")
        } else {
            write!(f, "just now")
        }
    }
}

#[cfg(test)]
mod tests {
    use super::ChronoHumanDuration;
    use chrono::Duration;

    #[test]
    fn it_works() {
        let d = Duration::weeks(2) + Duration::days(3) + Duration::hours(2) + Duration::minutes(20);
        assert_eq!(d.format_human().to_string(), "2 weeks ago");
        let d = Duration::weeks(1);
        assert_eq!(d.format_human().to_string(), "1 week ago");
        let d = Duration::days(2) + Duration::hours(2);
        assert_eq!(d.format_human().to_string(), "2 days ago");
        let d = Duration::days(1);
        assert_eq!(d.format_human().to_string(), "1 day ago");
        let d = Duration::hours(2);
        assert_eq!(d.format_human().to_string(), "2 hours ago");
        let d = Duration::hours(1);
        assert_eq!(d.format_human().to_string(), "1 hour ago");
        let d = Duration::minutes(2);
        assert_eq!(d.format_human().to_string(), "2 minutes ago");
        let d = Duration::minutes(1);
        assert_eq!(d.format_human().to_string(), "1 minute ago");
        let d = Duration::seconds(60);
        assert_eq!(d.format_human().to_string(), "1 minute ago");
        let d = Duration::seconds(59);
        assert_eq!(d.format_human().to_string(), "just now");
        let d = Duration::seconds(1);
        assert_eq!(d.format_human().to_string(), "just now");
    }
}