use std::time::{SystemTime, UNIX_EPOCH};
pub fn utc_now() -> String {
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
format_utc(secs)
}
pub fn format_utc(secs: u64) -> String {
let days = (secs / 86_400) as i64;
let rem = secs % 86_400;
let (year, month, day) = civil_from_days(days);
format!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
year,
month,
day,
rem / 3600,
(rem % 3600) / 60,
rem % 60
)
}
fn civil_from_days(days: i64) -> (i64, u64, u64) {
let z = days + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = (z - era * 146_097) as u64;
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
let year = yoe as i64 + era * 400 + if month <= 2 { 1 } else { 0 };
(year, month, day)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats_the_epoch() {
assert_eq!(format_utc(0), "1970-01-01T00:00:00Z");
}
#[test]
fn formats_a_leap_day() {
assert_eq!(format_utc(951_782_400), "2000-02-29T00:00:00Z");
}
#[test]
fn formats_seconds_minutes_and_hours() {
assert_eq!(format_utc(1_790_000_000), "2026-09-21T14:13:20Z");
assert_eq!(format_utc(4_102_444_799), "2099-12-31T23:59:59Z");
}
#[test]
fn utc_now_has_the_second_precision_shape() {
let now = utc_now();
assert_eq!(now.len(), 20);
assert!(now.ends_with('Z'));
assert_eq!(&now[10..11], "T");
}
}