use std::time::{Duration, SystemTime, UNIX_EPOCH};
const SECS_PER_DAY: i64 = 86_400;
const MIN_SECS: i64 = -62_167_219_200;
const MAX_SECS: i64 = 253_402_300_799;
pub fn rfc3339(time: SystemTime) -> String {
let (secs, nanos) = epoch_parts(time);
let secs = secs.clamp(MIN_SECS, MAX_SECS);
let days = secs.div_euclid(SECS_PER_DAY);
let time_of_day = secs.rem_euclid(SECS_PER_DAY);
let (year, month, day) = civil_from_days(days);
let hour = time_of_day / 3_600;
let minute = (time_of_day % 3_600) / 60;
let second = time_of_day % 60;
format!(
"{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}.{micros:06}Z",
micros = nanos / 1_000
)
}
fn epoch_parts(time: SystemTime) -> (i64, u32) {
match time.duration_since(UNIX_EPOCH) {
Ok(after) => (saturating_secs(after), after.subsec_nanos()),
Err(err) => {
let before = err.duration();
match before.subsec_nanos() {
0 => (-saturating_secs(before), 0),
nanos => (-saturating_secs(before) - 1, 1_000_000_000 - nanos),
}
}
}
}
fn saturating_secs(duration: Duration) -> i64 {
i64::try_from(duration.as_secs()).unwrap_or(i64::MAX)
}
fn civil_from_days(days: i64) -> (i64, u32, u32) {
let shifted = days + 719_468;
let era = shifted.div_euclid(146_097);
let day_of_era = shifted.rem_euclid(146_097);
let year_of_era =
(day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
let month_index = (5 * day_of_year + 2) / 153;
let day = (day_of_year - (153 * month_index + 2) / 5 + 1) as u32;
let month = if month_index < 10 {
month_index + 3
} else {
month_index - 9
} as u32;
let year = year_of_era + era * 400 + i64::from(month <= 2);
(year, month, day)
}
#[cfg(test)]
mod tests {
use super::*;
fn at(secs: u64, nanos: u32) -> SystemTime {
UNIX_EPOCH + Duration::new(secs, nanos)
}
#[test]
fn the_epoch_renders_as_the_epoch() {
assert_eq!(rfc3339(UNIX_EPOCH), "1970-01-01T00:00:00.000000Z");
}
#[test]
fn a_known_instant_matches_a_hand_computed_date() {
assert_eq!(rfc3339(at(1_234_567_890, 0)), "2009-02-13T23:31:30.000000Z");
}
#[test]
fn leap_day_rules_hold_at_every_boundary() {
assert_eq!(rfc3339(at(951_782_400, 0)), "2000-02-29T00:00:00.000000Z");
assert_eq!(rfc3339(at(1_709_164_800, 0)), "2024-02-29T00:00:00.000000Z");
let feb_28_1900 = UNIX_EPOCH - Duration::from_secs(2_203_977_600);
assert_eq!(rfc3339(feb_28_1900), "1900-02-28T00:00:00.000000Z");
assert_eq!(
rfc3339(feb_28_1900 + Duration::from_secs(SECS_PER_DAY as u64)),
"1900-03-01T00:00:00.000000Z"
);
}
#[test]
fn sub_microsecond_precision_is_truncated() {
assert_eq!(rfc3339(at(0, 999)), "1970-01-01T00:00:00.000000Z");
assert_eq!(rfc3339(at(0, 1_999)), "1970-01-01T00:00:00.000001Z");
}
#[test]
fn a_time_before_the_epoch_keeps_its_fraction_pointing_forward() {
let quarter_second_before = UNIX_EPOCH - Duration::new(0, 250_000_000);
assert_eq!(
rfc3339(quarter_second_before),
"1969-12-31T23:59:59.750000Z"
);
let exactly_a_second_before = UNIX_EPOCH - Duration::from_secs(1);
assert_eq!(
rfc3339(exactly_a_second_before),
"1969-12-31T23:59:59.000000Z"
);
}
#[test]
fn the_last_second_of_a_year_does_not_roll_over_early() {
assert_eq!(rfc3339(at(1_767_225_599, 0)), "2025-12-31T23:59:59.000000Z");
assert_eq!(rfc3339(at(1_767_225_600, 0)), "2026-01-01T00:00:00.000000Z");
}
#[test]
fn unrepresentable_times_clamp_to_the_format_bounds() {
let far_future = UNIX_EPOCH + Duration::new(300_000_000_000, 999_999_999);
assert_eq!(rfc3339(far_future), "9999-12-31T23:59:59.999999Z");
let far_past = UNIX_EPOCH - Duration::from_secs(70_000_000_000);
assert_eq!(rfc3339(far_past), "0000-01-01T00:00:00.000000Z");
}
#[test]
fn output_is_fixed_width_so_it_sorts_as_text() {
let early = at(1_000_000, 0);
let late = at(1_700_000_000, 0);
assert_eq!(rfc3339(early).len(), rfc3339(late).len());
assert!(rfc3339(early) < rfc3339(late));
}
}
#[cfg(test)]
mod property_tests {
use super::*;
use proptest::prelude::*;
proptest::proptest! {
#[test]
fn every_time_renders_in_the_documented_shape(secs in 0..4_000_000_000u64, nanos in 0..1_000_000_000u32) {
let rendered = rfc3339(UNIX_EPOCH + Duration::new(secs, nanos));
prop_assert_eq!(rendered.len(), 27);
prop_assert!(rendered.ends_with('Z'));
prop_assert_eq!(rendered.as_bytes()[4], b'-');
prop_assert_eq!(rendered.as_bytes()[7], b'-');
prop_assert_eq!(rendered.as_bytes()[10], b'T');
prop_assert_eq!(rendered.as_bytes()[13], b':');
prop_assert_eq!(rendered.as_bytes()[16], b':');
prop_assert_eq!(rendered.as_bytes()[19], b'.');
}
#[test]
fn later_times_render_as_larger_strings(a in 0..4_000_000_000u64, b in 0..4_000_000_000u64) {
let (earlier, later) = if a <= b { (a, b) } else { (b, a) };
let rendered_earlier = rfc3339(UNIX_EPOCH + Duration::from_secs(earlier));
let rendered_later = rfc3339(UNIX_EPOCH + Duration::from_secs(later));
prop_assert!(rendered_earlier <= rendered_later);
}
#[test]
fn consecutive_days_render_as_distinct_dates(day in 0..40_000i64) {
let secs = day * SECS_PER_DAY;
let today = rfc3339(UNIX_EPOCH + Duration::from_secs(secs as u64));
let tomorrow = rfc3339(UNIX_EPOCH + Duration::from_secs((secs + SECS_PER_DAY) as u64));
prop_assert_ne!(&today[..10], &tomorrow[..10]);
prop_assert!(today < tomorrow);
}
}
}