arcature-cli 2026.1.1

Developer lifecycle CLI for Arcature applications.
Documentation
//! Bundle timestamps (AP2.1-10).
//!
//! The bundle creation timestamp as an RFC 3339 UTC string. Uses a
//! `chrono`-free std: the `SystemTime` since UNIX epoch, converted to a date
//! via a small civil-from-days algorithm. Deterministic enough for the
//! bundle timestamp (the manifest/checksums/SBOM are deterministic ignoring
//! the timestamp; the timestamp varies with wall clock by design โ€” two
//! builds at different times differ only in the `created_at` field, which is
//! not part of any digest).

/// The current time as an RFC 3339 UTC string. Uses `chrono`-free std: the
/// `SystemTime` since UNIX epoch, converted to a date via a small
/// civil-from-days algorithm. Deterministic enough for the bundle
/// timestamp (the manifest/checksums/SBOM are deterministic ignoring the
/// timestamp; the timestamp varies with wall clock by design โ€” two builds
/// at different times differ only in the `created_at` field, which is not
/// part of any digest).
pub(super) fn rfc3339_now() -> String {
    let dur = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default();
    let secs = dur.as_secs() as i64;
    let (year, month, day, hour, min, sec) = civil_from_unix(secs);
    format!("{year:04}-{month:02}-{day:02}T{hour:02}:{min:02}:{sec:02}Z")
}

/// Convert a UNIX timestamp (seconds since 1970-01-01 UTC) to a (year,
/// month, day, hour, minute, second) tuple. Uses the well-known
/// "civil_from_days" algorithm (Howard Hinnant, public domain). No `chrono`
/// dependency (AGENTS.md ยง8: dependencies enter only through policy; the
/// timestamp is not on a hot path).
pub(super) fn civil_from_unix(secs: i64) -> (i32, u32, u32, u32, u32, u32) {
    let days = secs.div_euclid(86400);
    let rem = secs.rem_euclid(86400);
    let hour = (rem / 3600) as u32;
    let min = ((rem % 3600) / 60) as u32;
    let sec = (rem % 60) as u32;
    // Civil from days (Hinnant's algorithm). Days since 1970-01-01.
    let z = days + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = z - era * 146_097; // [0, 146_096]
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365; // [0, 399]
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
    let mp = (5 * doy + 2) / 153; // [0, 11]
    let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
    let m = if mp < 10 { mp + 3 } else { mp - 9 }; // [1, 12]
    let year = if m <= 2 { y + 1 } else { y };
    (year as i32, m as u32, d as u32, hour, min, sec)
}

#[cfg(test)]
mod tests {
    use super::{civil_from_unix, rfc3339_now};

    #[test]
    fn civil_from_unix_known_epochs() {
        // 1970-01-01T00:00:00Z
        assert_eq!(civil_from_unix(0), (1970, 1, 1, 0, 0, 0));
        // 2026-01-01T00:00:00Z โ€” days since epoch = 20454.
        let secs = 20454 * 86400;
        assert_eq!(civil_from_unix(secs), (2026, 1, 1, 0, 0, 0));
        // 2026-08-17T12:34:56Z
        let secs =
            (20454 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 16) * 86400 + 12 * 3600 + 34 * 60 + 56;
        let (y, m, d, h, mi, s) = civil_from_unix(secs);
        assert_eq!((y, m, d), (2026, 8, 17));
        assert_eq!((h, mi, s), (12, 34, 56));
    }

    #[test]
    fn rfc3339_now_is_well_formed() {
        let ts = rfc3339_now();
        // YYYY-MM-DDThh:mm:ssZ (20 chars).
        assert_eq!(ts.len(), 20);
        assert_eq!(ts.as_bytes()[4], b'-');
        assert_eq!(ts.as_bytes()[7], b'-');
        assert_eq!(ts.as_bytes()[10], b'T');
        assert_eq!(ts.as_bytes()[13], b':');
        assert_eq!(ts.as_bytes()[16], b':');
        assert_eq!(ts.as_bytes()[19], b'Z');
    }

    #[test]
    fn civil_from_unix_handles_negative_secs() {
        // The day before the epoch: 1969-12-31T00:00:00Z
        let (y, m, d, _h, _mi, _s) = civil_from_unix(-86400);
        assert_eq!((y, m, d), (1969, 12, 31));
    }
}