Skip to main content

chio_kernel_mobile/
clock.rs

1//! Real-time `Clock` implementation for mobile hosts.
2//!
3//! Both iOS and Android expose `std::time::SystemTime::now()` through
4//! the standard Rust libstd shim, so the mobile adapter can read the
5//! wall clock directly instead of injecting the time through FFI on
6//! every call.
7//!
8//! If `SystemTime::now()` ever falls before `UNIX_EPOCH` (clock set
9//! badly wrong on the device), the impl clamps to `0`. The verdict
10//! path is fail-closed against ancient timestamps, so a capability
11//! issued in the last 60 years will still be rejected as
12//! `NotYetValid` until the device clock corrects.
13
14#![forbid(unsafe_code)]
15
16use std::time::{SystemTime, UNIX_EPOCH};
17
18use chio_kernel_core::Clock;
19
20/// Mobile-suitable `Clock` implementation that reads the device
21/// wall-clock via `SystemTime::now()`.
22#[derive(Debug, Clone, Copy, Default)]
23pub struct MobileClock;
24
25impl MobileClock {
26    /// Construct a new mobile clock.
27    #[must_use]
28    pub const fn new() -> Self {
29        Self
30    }
31}
32
33impl Clock for MobileClock {
34    fn now_unix_secs(&self) -> u64 {
35        SystemTime::now()
36            .duration_since(UNIX_EPOCH)
37            .map(|d| d.as_secs())
38            .unwrap_or(0)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn mobile_clock_returns_recent_timestamp() {
48        let clock = MobileClock::new();
49        let now = clock.now_unix_secs();
50        // Any time after 2020-01-01 is "plausibly current".
51        assert!(now > 1_577_836_800);
52    }
53}