katra-core 0.1.0

Katra3D core: shared vocabulary, error model, event model, IDs, and policy types.
Documentation
//! Time helpers.
//!
//! Traces use two clocks:
//! * **monotonic** — nanoseconds relative to profiler start; deterministic
//!   across replays and comparable across runs on the same machine;
//! * **wall** — absolute wall-clock nanoseconds; used to anchor epochs and
//!   to correlate with external logs.

use std::time::{SystemTime, UNIX_EPOCH};

/// Current wall-clock time in nanoseconds since the Unix epoch.
pub fn wall_now_ns() -> u64 {
    SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos() as u64).unwrap_or(0)
}

/// Monotonic time in nanoseconds (process-local, arbitrary origin).
pub fn monotonic_now_ns() -> u64 {
    use std::time::Instant;
    // A process-wide anchor so multiple profilers in one process share an origin.
    static ANCHOR: std::sync::OnceLock<(Instant, u64)> = std::sync::OnceLock::new();
    let (anchor, wall_at_anchor) = ANCHOR.get_or_init(|| (Instant::now(), wall_now_ns()));
    let since = anchor.elapsed().as_nanos() as u64;
    wall_at_anchor.wrapping_add(since)
}