haematite 0.6.0

Content-addressed, branchable, actor-native storage engine
Documentation
/// Wall-clock timestamp in nanoseconds since the Unix epoch.
pub type Timestamp = u64;

/// Captures the current wall-clock time as a [`Timestamp`]. A reading before the
/// Unix epoch (which a sane host clock never produces) collapses to `0`.
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
pub fn current_timestamp() -> Timestamp {
    use std::time::{SystemTime, UNIX_EPOCH};

    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|elapsed| u64::try_from(elapsed.as_nanos()).unwrap_or(u64::MAX))
        .unwrap_or(0)
}

/// Browser timestamp used only for caller-supplied branch record metadata.
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub fn current_timestamp() -> Timestamp {
    let millis = js_sys::Date::now();
    if !millis.is_finite() || millis <= 0.0 {
        return 0;
    }
    let nanos = millis * 1_000_000.0;
    format!("{nanos:.0}").parse().unwrap_or(u64::MAX)
}