index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
gitrs / src / time.rs
-rw-r--r-- blob 0eb01bcd79118b1b4773e85929a1cd17a222c1c4 2643 bytes (plain)
1
use std::{fmt::Display, io::Write};
2
3
use bytes::{BytesMut, buf::Writer};
4
use gix::date::Time as GixTime;
5
6
use chrono::{DateTime, FixedOffset, TimeZone, Utc};
7
8
#[derive(Clone)]
9
pub struct Time {
10
    gix: GixTime,
11
}
12
13
pub enum Recency {
14
    Fresh,
15
    Recent,
16
    Stale,
17
}
18
19
impl Display for Recency {
20
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21
        match self {
22
            Self::Fresh => write!(f, "fresh"),
23
            Self::Recent => write!(f, "recent"),
24
            Self::Stale => write!(f, "stale"),
25
        }
26
    }
27
}
28
29
impl Time {
30
    pub fn new(gix: GixTime) -> Self {
31
        Self { gix }
32
    }
33
34
    pub fn format(&self) -> String {
35
        let dt: DateTime<Utc> = Utc.timestamp_opt(self.gix.seconds, 0).unwrap();
36
        let offset = FixedOffset::east_opt(self.gix.offset).unwrap();
37
        let local = dt.with_timezone(&offset);
38
        local.format("%Y-%m-%d %H:%M:%S %z").to_string()
39
    }
40
41
    pub fn format_rfc(&self, writer: &mut Writer<BytesMut>) {
42
        let dt: DateTime<Utc> = Utc.timestamp_opt(self.gix.seconds, 0).unwrap();
43
        let offset = FixedOffset::east_opt(self.gix.offset).unwrap();
44
        let local = dt.with_timezone(&offset);
45
        writer
46
            .write_all(
47
                local
48
                    .format("%a, %d %b %Y %H:%M:%S %z")
49
                    .to_string()
50
                    .as_bytes(),
51
            )
52
            .unwrap();
53
    }
54
55
    pub fn format_relative(&self) -> (String, Recency) {
56
        format_relative(self.gix.seconds)
57
    }
58
}
59
60
pub fn format_relative(seconds: i64) -> (String, Recency) {
61
    let then = Utc.timestamp_opt(seconds, 0).unwrap();
62
    let now = Utc::now();
63
    let diff = now.signed_duration_since(then);
64
65
    let secs = diff.num_seconds();
66
    let mins = diff.num_minutes();
67
    let hours = diff.num_hours();
68
    let days = diff.num_days();
69
70
    if secs < 120 {
71
        (pluralize(secs.max(0), "second"), Recency::Fresh)
72
    } else if mins < 120 {
73
        (pluralize(mins, "minute"), Recency::Fresh)
74
    } else if hours < 48 {
75
        (pluralize(hours, "hour"), Recency::Fresh)
76
    } else if days < 14 {
77
        (pluralize(days, "day"), Recency::Fresh)
78
    } else if days < 30 {
79
        (pluralize(days / 7, "week"), Recency::Recent)
80
    } else if days < 60 {
81
        (pluralize(days / 30, "month"), Recency::Recent)
82
    } else if days < 365 {
83
        (pluralize(days / 30, "month"), Recency::Stale)
84
    } else {
85
        (pluralize(days / 365, "year"), Recency::Stale)
86
    }
87
}
88
89
fn pluralize(count: i64, unit: &str) -> String {
90
    if count == 1 {
91
        format!("1 {unit}")
92
    } else {
93
        format!("{count} {unit}s")
94
    }
95
}