Skip to main content

weirdnet_chart/
time.rs

1//! Timestamps, fixed-offset time zones and calendar helpers for time axes.
2
3/// Milliseconds since the Unix epoch (1970-01-01T00:00:00Z).
4pub type Timestamp = i64;
5
6/// One second in milliseconds.
7pub const SECOND: i64 = 1_000;
8/// One minute in milliseconds.
9pub const MINUTE: i64 = 60 * SECOND;
10/// One hour in milliseconds.
11pub const HOUR: i64 = 60 * MINUTE;
12/// One day in milliseconds.
13pub const DAY: i64 = 24 * HOUR;
14
15/// The time zone used for axis labels and tick alignment.
16///
17/// Charts don't carry a time-zone database: pass the offset that applies to the
18/// visible range (for example from your platform or a tz crate). Ticks land on
19/// whole local hours, days and months for that offset.
20#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
21pub enum TimeZone {
22    /// Coordinated Universal Time.
23    #[default]
24    Utc,
25    /// A fixed offset from UTC in seconds (east is positive).
26    Fixed(i32),
27}
28
29impl TimeZone {
30    /// The offset from UTC in milliseconds.
31    pub fn offset_ms(self) -> i64 {
32        match self {
33            TimeZone::Utc => 0,
34            TimeZone::Fixed(secs) => secs as i64 * SECOND,
35        }
36    }
37
38    /// The local wall-clock time of `t`, as milliseconds since the local epoch.
39    pub fn to_local(self, t: Timestamp) -> i64 {
40        t + self.offset_ms()
41    }
42
43    /// The UTC timestamp of local wall-clock milliseconds `local`.
44    pub fn from_local(self, local: i64) -> Timestamp {
45        local - self.offset_ms()
46    }
47}
48
49/// A broken-down local date and time.
50#[derive(Clone, Copy, Debug, PartialEq, Eq)]
51pub struct DateTime {
52    /// Year, for example 2026.
53    pub year: i32,
54    /// Month, 1 to 12.
55    pub month: u32,
56    /// Day of the month, 1 to 31.
57    pub day: u32,
58    /// Hour, 0 to 23.
59    pub hour: u32,
60    /// Minute, 0 to 59.
61    pub minute: u32,
62    /// Second, 0 to 59.
63    pub second: u32,
64    /// Millisecond, 0 to 999.
65    pub millis: u32,
66}
67
68impl DateTime {
69    /// Breaks `t` down in time zone `tz`.
70    pub fn from_timestamp(t: Timestamp, tz: TimeZone) -> DateTime {
71        let local = tz.to_local(t);
72        let days = local.div_euclid(DAY);
73        let ms = local.rem_euclid(DAY);
74        let (year, month, day) = civil_from_days(days);
75        DateTime {
76            year,
77            month,
78            day,
79            hour: (ms / HOUR) as u32,
80            minute: (ms % HOUR / MINUTE) as u32,
81            second: (ms % MINUTE / SECOND) as u32,
82            millis: (ms % SECOND) as u32,
83        }
84    }
85
86    /// The UTC timestamp of this local date and time in `tz`.
87    pub fn to_timestamp(self, tz: TimeZone) -> Timestamp {
88        let days = days_from_civil(self.year, self.month, self.day);
89        let local = days * DAY + self.hour as i64 * HOUR + self.minute as i64 * MINUTE + self.second as i64 * SECOND + self.millis as i64;
90        tz.from_local(local)
91    }
92}
93
94/// Days since 1970-01-01 for a proleptic Gregorian date.
95pub fn days_from_civil(year: i32, month: u32, day: u32) -> i64 {
96    let y = year as i64 - i64::from(month <= 2);
97    let era = y.div_euclid(400);
98    let yoe = y - era * 400;
99    let m = month as i64;
100    let doy = (153 * (if m > 2 { m - 3 } else { m + 9 }) + 2) / 5 + day as i64 - 1;
101    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
102    era * 146_097 + doe - 719_468
103}
104
105/// The proleptic Gregorian date `(year, month, day)` of a day count since 1970-01-01.
106pub fn civil_from_days(days: i64) -> (i32, u32, u32) {
107    let z = days + 719_468;
108    let era = z.div_euclid(146_097);
109    let doe = z - era * 146_097;
110    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
111    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
112    let mp = (5 * doy + 2) / 153;
113    let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
114    let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
115    let year = (yoe + era * 400 + i64::from(month <= 2)) as i32;
116    (year, month, day)
117}
118
119pub(crate) const MONTHS: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn civil_round_trip() {
127        for days in [-800_000, -1, 0, 1, 59, 10_957, 20_782, 2_000_000] {
128            let (y, m, d) = civil_from_days(days);
129            assert_eq!(days_from_civil(y, m, d), days);
130        }
131        assert_eq!(civil_from_days(0), (1970, 1, 1));
132        assert_eq!(days_from_civil(2000, 3, 1), 11_017);
133    }
134
135    #[test]
136    fn breaks_down_with_offset() {
137        // 2026-11-25T20:44:05.250Z
138        let t = DateTime { year: 2026, month: 11, day: 25, hour: 20, minute: 44, second: 5, millis: 250 }.to_timestamp(TimeZone::Utc);
139        let local = DateTime::from_timestamp(t, TimeZone::Fixed(-5 * 3600));
140        assert_eq!((local.day, local.hour, local.minute, local.second, local.millis), (25, 15, 44, 5, 250));
141        let east = DateTime::from_timestamp(t, TimeZone::Fixed(9 * 3600));
142        assert_eq!((east.day, east.hour), (26, 5));
143        assert_eq!(east.to_timestamp(TimeZone::Fixed(9 * 3600)), t);
144    }
145}