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