Skip to main content

vtcode_core/tools/
time_compat.rs

1//! Small compatibility helpers that avoid pulling in heavy dependencies
2//! (e.g. `chrono`) for trivial needs.
3
4use std::time::{SystemTime, UNIX_EPOCH};
5
6/// Return the current wall-clock time as an RFC 3339 / ISO 8601 string
7/// in UTC with second precision. Used for telemetry/health fields that
8/// previously relied on `chrono::Utc::now().to_rfc3339()`.
9pub fn current_timestamp_rfc3339() -> String {
10    let now = match SystemTime::now().duration_since(UNIX_EPOCH) {
11        Ok(d) => d,
12        Err(_) => return "1970-01-01T00:00:00Z".to_string(),
13    };
14    format_rfc3339_utc_seconds(now.as_secs())
15}
16
17fn format_rfc3339_utc_seconds(secs: u64) -> String {
18    let (year, month, day, hour, minute, second) = civil_from_unix(secs);
19    format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z")
20}
21
22/// Convert seconds since the Unix epoch to (year, month, day, hour, minute,
23/// second) in UTC. Uses Howard Hinnant's `days_from_civil` / `civil_from_days`
24/// algorithms, which are well-known and public domain.
25fn civil_from_unix(secs: u64) -> (i32, u32, u32, u32, u32, u32) {
26    let days = (secs / 86_400) as i64;
27    let rem = (secs % 86_400) as u32;
28    let hour = rem / 3600;
29    let minute = (rem % 3600) / 60;
30    let second = rem % 60;
31    let (y, m, d) = civil_from_days(days);
32    #[allow(
33        clippy::cast_sign_loss,
34        reason = "Intentional compatibility, platform, or test-only suppression."
35    )]
36    (y, m as u32, d as u32, hour, minute, second)
37}
38
39#[cfg(test)]
40const fn days_from_civil(y: i32, m: u32, d: u32) -> i64 {
41    let y = if m <= 2 { y - 1 } else { y };
42    let era = if y >= 0 { y } else { y - 399 } / 400;
43    let yoe = (y - era * 400) as i64;
44    let m = m as i64;
45    let d = d as i64;
46    let doy = (153 * (if m > 2 { m - 3 } else { m + 9 }) + 2) / 5 + d - 1;
47    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
48    era as i64 * 146_097 + doe - 719_468
49}
50
51fn civil_from_days(z: i64) -> (i32, i32, i32) {
52    let z = z + 719_468;
53    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
54    let doe = z - era * 146_097;
55    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
56    let y = yoe + era * 400;
57    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
58    let mp = (5 * doy + 2) / 153;
59    let d = doy - (153 * mp + 2) / 5 + 1;
60    let m = if mp < 10 { mp + 3 } else { mp - 9 };
61    let y = if m <= 2 { y + 1 } else { y };
62    (y as i32, m as i32, d as i32)
63}
64
65#[cfg(test)]
66mod tests {
67    use super::{days_from_civil, format_rfc3339_utc_seconds};
68
69    #[test]
70    fn formats_unix_epoch() {
71        assert_eq!(format_rfc3339_utc_seconds(0), "1970-01-01T00:00:00Z");
72    }
73
74    #[test]
75    fn formats_known_timestamp() {
76        // 2024-01-15T12:34:56Z
77        #[allow(
78            clippy::cast_sign_loss,
79            reason = "Intentional compatibility, platform, or test-only suppression."
80        )]
81        let secs = days_from_civil(2024, 1, 15) as u64 * 86_400 + 12 * 3600 + 34 * 60 + 56;
82        assert_eq!(format_rfc3339_utc_seconds(secs), "2024-01-15T12:34:56Z");
83    }
84}