Skip to main content

pacenotes_server/reports/
report_file.rs

1/// The UTC calendar date of a unix timestamp, by Howard Hinnant's
2/// `civil_from_days`, to spare a date crate.
3pub fn utc_date(unix: i64) -> (i64, u32, u32) {
4    let z = unix.div_euclid(86_400) + 719_468;
5    let era = z.div_euclid(146_097);
6    let doe = z.rem_euclid(146_097);
7    let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
8    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
9    let mp = (5 * doy + 2) / 153;
10    let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
11    let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
12    let year = yoe + era * 400 + i64::from(month <= 2);
13    (year, month, day)
14}
15
16/// `conflicts-YYYY-MM-DD.jsonl` for the day `unix` falls on; ISO dates sort as
17/// text, which the overlap summary relies on.
18pub fn file_name(unix: i64) -> String {
19    let (year, month, day) = utc_date(unix);
20    format!("conflicts-{year:04}-{month:02}-{day:02}.jsonl")
21}
22
23#[cfg(test)]
24mod tests {
25    #[test]
26    fn utc_dates_match_known_values() {
27        assert_eq!(super::utc_date(0), (1970, 1, 1));
28        assert_eq!(super::utc_date(951_782_400), (2000, 2, 29));
29        assert_eq!(super::utc_date(1_792_886_400), (2026, 10, 25));
30        assert_eq!(super::utc_date(1_792_886_400 + 86_399), (2026, 10, 25));
31        assert_eq!(super::utc_date(4_107_542_400), (2100, 3, 1));
32        assert_eq!(super::utc_date(-86_400), (1969, 12, 31));
33    }
34
35    #[test]
36    fn a_days_reports_share_one_file() {
37        assert_eq!(
38            super::file_name(1_792_886_400),
39            "conflicts-2026-10-25.jsonl"
40        );
41    }
42}