use std::time::{SystemTime, UNIX_EPOCH};
pub fn unix_millis() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0)
}
#[cfg(not(test))]
fn now_secs() -> u64 {
unix_millis() / 1000
}
#[cfg(test)]
fn now_secs() -> u64 {
TICKING.with(|t| match t.get() {
Some(secs) => {
t.set(Some(secs + 1));
secs
}
None => unix_millis() / 1000,
})
}
#[cfg(test)]
thread_local! {
static TICKING: std::cell::Cell<Option<u64>> = const { std::cell::Cell::new(None) };
}
#[cfg(test)]
pub struct Ticking;
#[cfg(test)]
impl Ticking {
pub fn start(secs: u64) -> Ticking {
TICKING.with(|t| t.set(Some(secs)));
Ticking
}
}
#[cfg(test)]
impl Drop for Ticking {
fn drop(&mut self) {
TICKING.with(|t| t.set(None));
}
}
pub fn now_rfc3339() -> String {
let secs = now_secs();
let (y, m, d) = civil_from_days((secs / 86_400) as i64);
let rem = secs % 86_400;
format!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
y,
m,
d,
rem / 3600,
(rem % 3600) / 60,
rem % 60
)
}
pub fn date_of(ts: &str) -> &str {
if ts.len() >= 10 {
&ts[..10]
} else {
ts
}
}
pub fn days_between(from: &str, to: &str) -> Option<i64> {
Some(days_from_civil(parse_date(to)?) - days_from_civil(parse_date(from)?))
}
fn parse_date(ts: &str) -> Option<(i64, u32, u32)> {
let d = date_of(ts);
let b = d.as_bytes();
if b.len() != 10 || b[4] != b'-' || b[7] != b'-' {
return None;
}
Some((
d[0..4].parse().ok()?,
d[5..7].parse().ok()?,
d[8..10].parse().ok()?,
))
}
pub fn epoch_seconds(ts: &str) -> Option<i64> {
let b = ts.as_bytes();
if b.len() != 20
|| b[4] != b'-'
|| b[7] != b'-'
|| b[10] != b'T'
|| b[13] != b':'
|| b[16] != b':'
|| b[19] != b'Z'
{
return None;
}
let y: i64 = ts[0..4].parse().ok()?;
let m: u32 = ts[5..7].parse().ok()?;
let d: u32 = ts[8..10].parse().ok()?;
let hh: i64 = ts[11..13].parse().ok()?;
let mm: i64 = ts[14..16].parse().ok()?;
let ss: i64 = ts[17..19].parse().ok()?;
Some(days_from_civil((y, m, d)) * 86_400 + hh * 3600 + mm * 60 + ss)
}
fn days_from_civil((y, m, d): (i64, u32, u32)) -> i64 {
let y = if m <= 2 { y - 1 } else { y };
let era = if y >= 0 { y } else { y - 399 } / 400;
let yoe = y - era * 400;
let mp = if m > 2 { m - 3 } else { m + 9 } as i64;
let doy = (153 * mp + 2) / 5 + d as i64 - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
era * 146_097 + doe - 719_468
}
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 / 1460 + doe / 36_524 - doe / 146_096) / 365;
let y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
(if m <= 2 { y + 1 } else { y }, m, d)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_day_apart_is_one_day() {
assert_eq!(
days_between("2026-09-07T23:59:00Z", "2026-09-08T00:01:00Z"),
Some(1)
);
}
#[test]
fn the_same_day_is_zero_however_many_hours_apart() {
assert_eq!(
days_between("2026-09-08T00:01:00Z", "2026-09-08T23:59:00Z"),
Some(0)
);
}
#[test]
fn a_month_boundary_counts_the_real_days() {
assert_eq!(
days_between("2026-02-27T00:00:00Z", "2026-03-01T00:00:00Z"),
Some(2)
);
}
#[test]
fn a_leap_day_counts() {
assert_eq!(
days_between("2024-02-28T00:00:00Z", "2024-03-01T00:00:00Z"),
Some(2)
);
}
#[test]
fn going_backwards_is_negative() {
assert_eq!(
days_between("2026-09-08T00:00:00Z", "2026-09-01T00:00:00Z"),
Some(-7)
);
}
#[test]
fn a_stamp_that_is_not_one_is_none() {
assert_eq!(days_between("yesterday", "2026-09-08T00:00:00Z"), None);
assert_eq!(days_between("2026-09-08T00:00:00Z", "2026-9-8"), None);
}
#[test]
fn epoch_is_1970() {
assert_eq!(civil_from_days(0), (1970, 1, 1));
}
#[test]
fn known_dates() {
assert_eq!(civil_from_days(20_696), (2026, 8, 31));
assert_eq!(civil_from_days(19_782), (2024, 2, 29));
assert_eq!(civil_from_days(-1), (1969, 12, 31));
}
#[test]
fn format_is_stable() {
let s = now_rfc3339();
assert_eq!(s.len(), 20);
assert!(s.ends_with('Z'));
}
#[test]
fn epoch_seconds_reads_the_epoch_itself() {
assert_eq!(epoch_seconds("1970-01-01T00:00:00Z"), Some(0));
}
#[test]
fn epoch_seconds_counts_the_time_of_day_too() {
assert_eq!(
epoch_seconds("1970-01-01T00:01:00Z"),
Some(60),
"a minute past the epoch is 60 seconds, not 0"
);
assert_eq!(
epoch_seconds("2026-09-08T12:00:00Z").unwrap()
- epoch_seconds("2026-09-08T00:00:00Z").unwrap(),
43_200,
"noon is half a day past midnight on the same date"
);
}
#[test]
fn epoch_seconds_of_a_bare_date_is_none() {
assert_eq!(epoch_seconds("2026-09-08"), None);
}
#[test]
fn epoch_seconds_of_garbage_is_none() {
assert_eq!(epoch_seconds("not a timestamp"), None);
}
}