use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UtcDateTime {
pub year: i64,
pub month: u32,
pub day: u32,
pub hour: u32,
pub minute: u32,
pub second: u32,
}
impl UtcDateTime {
pub fn now() -> Self {
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
Self::from_unix_seconds(secs)
}
pub fn from_unix_seconds(secs: u64) -> Self {
let days = (secs / 86_400) as i64;
let rem = (secs % 86_400) as u32;
let (year, month, day) = civil_from_days(days);
UtcDateTime {
year,
month,
day,
hour: rem / 3_600,
minute: (rem % 3_600) / 60,
second: rem % 60,
}
}
pub fn to_iso8601(&self) -> String {
format!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
self.year, self.month, self.day, self.hour, self.minute, self.second
)
}
pub fn to_pretty(&self) -> String {
format!(
"{:04}/{:02}/{:02} {:02}:{:02}:{:02} UTC",
self.year, self.month, self.day, self.hour, self.minute, self.second
)
}
}
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) as u64; let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365; let year = yoe as i64 + 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_is_1970() {
assert_eq!(civil_from_days(0), (1970, 1, 1));
}
#[test]
fn known_dates_convert_correctly() {
assert_eq!(civil_from_days(19_782), (2024, 2, 29)); assert_eq!(civil_from_days(10_957), (2000, 1, 1));
}
#[test]
fn decodes_a_full_timestamp() {
let dt = UtcDateTime::from_unix_seconds(1_609_459_200);
assert_eq!(
dt,
UtcDateTime {
year: 2021,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0
}
);
let dt = UtcDateTime::from_unix_seconds(1_609_459_200 + 3_661);
assert_eq!((dt.hour, dt.minute, dt.second), (1, 1, 1));
}
#[test]
fn iso_format_shape() {
let ts = UtcDateTime::now().to_iso8601();
assert!(ts.ends_with('Z'), "expected Z suffix: {ts}");
assert!(ts.contains('T'), "expected T separator: {ts}");
assert_eq!(ts.len(), 20, "expected length 20: {ts}");
}
#[test]
fn pretty_format_shape() {
let ts = UtcDateTime::now().to_pretty();
assert!(ts.ends_with(" UTC"), "expected UTC suffix: {ts}");
assert_eq!(ts.len(), 23, "expected length 23: {ts}");
}
}