Skip to main content

relayburn_cli/util/
time.rs

1//! Tiny time helpers shared by the harness adapter
2//! ([`super::super::harnesses::claude`]).
3//!
4//! Both call sites need the same two operations:
5//!
6//! - [`iso_now`] — current wall-clock in `YYYY-MM-DDTHH:MM:SS.mmmZ` format,
7//!   matching `new Date().toISOString()` in the TS sibling.
8//! - [`civil_from_days`] — Howard Hinnant's days-since-epoch → (Y, M, D)
9//!   conversion. Pulled out so we don't pay a `chrono` / `time` dependency
10//!   for two tiny call sites.
11//!
12//! Until D5 / D6 these were duplicated across `harnesses/claude.rs` and
13//! `commands/run.rs` with a `keep them in sync` comment. CodeRabbit caught
14//! the duplication during PR #318 review; this module is the resolution.
15
16/// Build an ISO-8601 UTC timestamp suitable for `Stamp::ts` / the
17/// `burnSpawnTs` enrichment tag. Mirrors `new Date().toISOString()` in
18/// the TS sibling.
19pub fn iso_now() -> String {
20    use std::time::{SystemTime, UNIX_EPOCH};
21    let total_ms = SystemTime::now()
22        .duration_since(UNIX_EPOCH)
23        .map(|d| d.as_millis() as i64)
24        .unwrap_or(0);
25    iso_from_ms(total_ms)
26}
27
28/// Format an absolute `total_ms` (millis since the Unix epoch) as an
29/// ISO-8601 UTC string. Split out so callers that already captured a
30/// `SystemTime` (e.g. the driver's `spawn_start_ts`) can format it without
31/// re-reading the clock.
32pub fn iso_from_system_time(t: std::time::SystemTime) -> String {
33    use std::time::UNIX_EPOCH;
34    let total_ms = t
35        .duration_since(UNIX_EPOCH)
36        .map(|d| d.as_millis() as i64)
37        .unwrap_or(0);
38    iso_from_ms(total_ms)
39}
40
41fn iso_from_ms(total_ms: i64) -> String {
42    let total_secs = total_ms.div_euclid(1000);
43    let ms = total_ms.rem_euclid(1000) as u32;
44    // Civil-date conversion (Howard Hinnant's algorithm). Sufficient for
45    // the y2038-and-beyond range we care about.
46    let z = total_secs.div_euclid(86_400);
47    let secs_of_day = total_secs.rem_euclid(86_400) as u32;
48    let (y, m, d) = civil_from_days(z);
49    let hh = secs_of_day / 3600;
50    let mm = (secs_of_day % 3600) / 60;
51    let ss = secs_of_day % 60;
52    format!("{y:04}-{m:02}-{d:02}T{hh:02}:{mm:02}:{ss:02}.{ms:03}Z")
53}
54
55/// Days-since-epoch → (year, month, day). Hinnant 2014.
56///
57/// `z` is days since 1970-01-01; negative values are pre-epoch. Returns
58/// the proleptic Gregorian (year, 1-12 month, 1-31 day).
59pub fn civil_from_days(z: i64) -> (i64, u32, u32) {
60    let z = z + 719_468;
61    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
62    let doe = (z - era * 146_097) as u64;
63    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
64    let y = yoe as i64 + era * 400;
65    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
66    let mp = (5 * doy + 2) / 153;
67    let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
68    let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
69    (y + (if m <= 2 { 1 } else { 0 }), m, d)
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn iso_now_is_zulu_iso8601_shape() {
78        let s = iso_now();
79        assert_eq!(s.len(), "1970-01-01T00:00:00.000Z".len());
80        assert!(s.ends_with('Z'));
81        assert_eq!(&s[4..5], "-");
82        assert_eq!(&s[7..8], "-");
83        assert_eq!(&s[10..11], "T");
84        assert_eq!(&s[13..14], ":");
85        assert_eq!(&s[16..17], ":");
86        assert_eq!(&s[19..20], ".");
87    }
88
89    #[test]
90    fn civil_from_days_round_trips_known_dates() {
91        // 1970-01-01 = day 0
92        assert_eq!(civil_from_days(0), (1970, 1, 1));
93        // 2000-01-01 = day 10957
94        assert_eq!(civil_from_days(10_957), (2000, 1, 1));
95        // 2024-02-29 (leap) = day 19782
96        assert_eq!(civil_from_days(19_782), (2024, 2, 29));
97    }
98
99    #[test]
100    fn iso_from_system_time_uses_unix_epoch() {
101        use std::time::{Duration, UNIX_EPOCH};
102        let t = UNIX_EPOCH + Duration::from_millis(0);
103        assert_eq!(iso_from_system_time(t), "1970-01-01T00:00:00.000Z");
104    }
105}