Skip to main content

rightkit_agent_engine_protocol/
time.rs

1use std::time::{SystemTime, UNIX_EPOCH};
2
3pub fn now_iso() -> String {
4    let secs = SystemTime::now()
5        .duration_since(UNIX_EPOCH)
6        .map(|d| d.as_secs())
7        .unwrap_or(0);
8    epoch_seconds_to_iso(secs)
9}
10
11/// RFC 3339 UTC text (`YYYY-MM-DDTHH:MM:SSZ`) for `secs` since the Unix epoch.
12fn epoch_seconds_to_iso(secs: u64) -> String {
13    i64::try_from(secs)
14        .ok()
15        .and_then(|secs| rightkit_time::DateTime::from_unix(secs, 0))
16        .map(|dt| dt.format_rfc3339())
17        .unwrap_or_default()
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    #[test]
25    fn epoch_seconds_format_as_utc_iso() {
26        assert_eq!(epoch_seconds_to_iso(0), "1970-01-01T00:00:00Z");
27        assert_eq!(epoch_seconds_to_iso(1_704_067_199), "2023-12-31T23:59:59Z");
28        assert_eq!(epoch_seconds_to_iso(1_704_067_200), "2024-01-01T00:00:00Z");
29    }
30}