kcode-k1-ktool-current-time 0.1.0

Current UTC time Ktool for K1
Documentation
use chrono::{DateTime, TimeDelta, Utc};

use super::{current_time, format_time};

#[test]
fn formatting_is_utc_with_exact_milliseconds() {
    let time = DateTime::parse_from_rfc3339("2026-09-11T00:42:07.205987Z")
        .unwrap()
        .with_timezone(&Utc);

    assert_eq!(format_time(time), "2026-09-11T00:42:07.205Z");
}

#[test]
fn current_time_reads_the_system_clock() {
    let before = Utc::now();
    let text = current_time();
    let after = Utc::now();
    let parsed = DateTime::parse_from_rfc3339(&text)
        .unwrap()
        .with_timezone(&Utc);
    let fraction = text.split_once('.').unwrap().1;

    assert_eq!(fraction.len(), 4);
    assert!(fraction.ends_with('Z'));
    assert!(fraction[..3].bytes().all(|byte| byte.is_ascii_digit()));
    assert!(parsed >= before - TimeDelta::milliseconds(1));
    assert!(parsed <= after);
}