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 53a33d414309e06dd6a20ac09393799944296204 2710 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 seconds(&self) -> i64 {
35
        self.gix.seconds
36
    }
37
38
    pub fn format(&self) -> String {
39
        let dt: DateTime<Utc> = Utc.timestamp_opt(self.gix.seconds, 0).unwrap();
40
        let offset = FixedOffset::east_opt(self.gix.offset).unwrap();
41
        let local = dt.with_timezone(&offset);
42
        local.format("%Y-%m-%d %H:%M:%S %z").to_string()
43
    }
44
45
    pub fn format_rfc(&self, writer: &mut Writer<BytesMut>) {
46
        let dt: DateTime<Utc> = Utc.timestamp_opt(self.gix.seconds, 0).unwrap();
47
        let offset = FixedOffset::east_opt(self.gix.offset).unwrap();
48
        let local = dt.with_timezone(&offset);
49
        writer
50
            .write_all(
51
                local
52
                    .format("%a, %d %b %Y %H:%M:%S %z")
53
                    .to_string()
54
                    .as_bytes(),
55
            )
56
            .unwrap();
57
    }
58
59
    pub fn format_relative(&self) -> (String, Recency) {
60
        format_relative(self.gix.seconds)
61
    }
62
}
63
64
pub fn format_relative(seconds: i64) -> (String, Recency) {
65
    let then = Utc.timestamp_opt(seconds, 0).unwrap();
66
    let now = Utc::now();
67
    let diff = now.signed_duration_since(then);
68
69
    let secs = diff.num_seconds();
70
    let mins = diff.num_minutes();
71
    let hours = diff.num_hours();
72
    let days = diff.num_days();
73
74
    if secs < 120 {
75
        (pluralize(secs.max(0), "second"), Recency::Fresh)
76
    } else if mins < 120 {
77
        (pluralize(mins, "minute"), Recency::Fresh)
78
    } else if hours < 48 {
79
        (pluralize(hours, "hour"), Recency::Fresh)
80
    } else if days < 14 {
81
        (pluralize(days, "day"), Recency::Fresh)
82
    } else if days < 30 {
83
        (pluralize(days / 7, "week"), Recency::Recent)
84
    } else if days < 60 {
85
        (pluralize(days / 30, "month"), Recency::Recent)
86
    } else if days < 365 {
87
        (pluralize(days / 30, "month"), Recency::Stale)
88
    } else {
89
        (pluralize(days / 365, "year"), Recency::Stale)
90
    }
91
}
92
93
fn pluralize(count: i64, unit: &str) -> String {
94
    if count == 1 {
95
        format!("1 {unit}")
96
    } else {
97
        format!("{count} {unit}s")
98
    }
99
}