Skip to main content

gem_audit/
util.rs

1/// Format a Unix timestamp as a human-readable UTC date string.
2pub fn format_timestamp(seconds: i64) -> String {
3    let days_since_epoch = seconds / 86400;
4    let time_of_day = seconds % 86400;
5    let hours = time_of_day / 3600;
6    let minutes = (time_of_day % 3600) / 60;
7    let secs = time_of_day % 60;
8
9    let (year, month, day) = days_to_date(days_since_epoch);
10
11    format!(
12        "{:04}-{:02}-{:02} {:02}:{:02}:{:02} UTC",
13        year, month, day, hours, minutes, secs
14    )
15}
16
17/// Convert days since Unix epoch (1970-01-01) to a (year, month, day) tuple.
18///
19/// Algorithm from <https://howardhinnant.github.io/date_algorithms.html>.
20pub fn days_to_date(days: i64) -> (i64, u32, u32) {
21    let z = days + 719468;
22    let era = (if z >= 0 { z } else { z - 146096 }) / 146097;
23    let doe = (z - era * 146097) as u32;
24    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
25    let y = yoe as i64 + era * 400;
26    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
27    let mp = (5 * doy + 2) / 153;
28    let d = doy - (153 * mp + 2) / 5 + 1;
29    let m = if mp < 10 { mp + 3 } else { mp - 9 };
30    let y = if m <= 2 { y + 1 } else { y };
31    (y, m, d)
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn format_timestamp_epoch() {
40        assert_eq!(format_timestamp(0), "1970-01-01 00:00:00 UTC");
41    }
42
43    #[test]
44    fn format_timestamp_known_date() {
45        // 2023-11-14 22:13:20 UTC
46        assert_eq!(format_timestamp(1700000000), "2023-11-14 22:13:20 UTC");
47    }
48
49    #[test]
50    fn format_timestamp_with_time_components() {
51        // 1970-01-01 01:01:01 UTC = 3661 seconds
52        assert_eq!(format_timestamp(3661), "1970-01-01 01:01:01 UTC");
53    }
54
55    #[test]
56    fn format_timestamp_end_of_day() {
57        // 1970-01-01 23:59:59 UTC = 86399 seconds
58        assert_eq!(format_timestamp(86399), "1970-01-01 23:59:59 UTC");
59    }
60
61    #[test]
62    fn days_to_date_epoch() {
63        assert_eq!(days_to_date(0), (1970, 1, 1));
64    }
65
66    #[test]
67    fn days_to_date_leap_year() {
68        // 2000-02-29 is day 11016 from epoch
69        assert_eq!(days_to_date(11016), (2000, 2, 29));
70    }
71
72    #[test]
73    fn days_to_date_end_of_month() {
74        // 1970-01-31 is day 30
75        assert_eq!(days_to_date(30), (1970, 1, 31));
76    }
77
78    #[test]
79    fn days_to_date_pre_epoch() {
80        // 1969-12-31 is day -1
81        assert_eq!(days_to_date(-1), (1969, 12, 31));
82    }
83
84    #[test]
85    fn days_to_date_year_2024() {
86        // 2024-01-01 is day 19723
87        assert_eq!(days_to_date(19723), (2024, 1, 1));
88    }
89}