pub fn format_iso8601_utc(epoch_secs: i64, millis: u32) -> String {
let days = epoch_secs.div_euclid(86_400);
let secs_of_day = epoch_secs.rem_euclid(86_400);
let (year, month, day) = civil_from_days(days);
let hour = secs_of_day / 3_600;
let minute = (secs_of_day % 3_600) / 60;
let second = secs_of_day % 60;
format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}.{millis:03}Z")
}
fn civil_from_days(z: i64) -> (i64, u32, u32) {
let z = z + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = z - era * 146_097; let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365; let year = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let day = (doy - (153 * mp + 2) / 5 + 1) as u32; let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32; let year = if month <= 2 { year + 1 } else { year };
(year, month, day)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn epoch_zero_is_unix_epoch() {
assert_eq!(format_iso8601_utc(0, 0), "1970-01-01T00:00:00.000Z");
}
#[test]
fn known_instant_matches_js_to_iso_string() {
assert_eq!(
format_iso8601_utc(1_700_000_000, 0),
"2023-11-14T22:13:20.000Z"
);
}
#[test]
fn milliseconds_are_zero_padded() {
assert_eq!(format_iso8601_utc(0, 7), "1970-01-01T00:00:00.007Z");
}
#[test]
fn handles_leap_day() {
assert_eq!(
format_iso8601_utc(1_582_934_400, 0),
"2020-02-29T00:00:00.000Z"
);
}
}