Skip to main content

printobserver_core/
clock.rs

1//! The one time source this crate reads.
2//!
3//! Core never calls the system clock. Every instant it stamps, and every
4//! comparison it makes against one — a bounded intervention's expiry, the
5//! agent's minimum interval — is read through a [`Clock`], so that expiry is
6//! driven by time passing and by nothing else and a test drives it by
7//! advancing this one dependency alone.
8//!
9//! A wait that is held only in memory — the detector's resume grace — is
10//! timed on [`Clock::elapsed`] instead, which nothing steps: a host's wall
11//! clock is moved by time synchronisation, and a wait measured across one
12//! such step is cut short or drawn out by the step.
13
14use std::sync::OnceLock;
15use std::time::{Duration, Instant};
16
17use printobserver_types::Timestamp;
18
19/// The instant core reads the current time from.
20///
21/// Shareable across threads, because the expiry driver reads it from a thread
22/// of its own while callers read it from theirs.
23pub trait Clock: Send + Sync {
24    /// The instant now.
25    fn now(&self) -> Timestamp;
26
27    /// How much time has passed since an origin of this clock's own, on a
28    /// clock nothing steps: two readings differ by the time that passed
29    /// between them, whatever the wall clock did meanwhile.
30    ///
31    /// The provided reading is [`Clock::now`]'s, as time since the Unix epoch,
32    /// which a step of the wall clock moves; a clock over a host's own time
33    /// answers its monotonic clock instead, as [`SystemClock`] does.
34    fn elapsed(&self) -> Duration {
35        let micros = self.now().as_utc().timestamp_micros();
36        Duration::from_micros(u64::try_from(micros).unwrap_or_default())
37    }
38}
39
40/// The system's own clock, which is what a running supervisor is given.
41#[derive(Debug, Clone, Copy, Default)]
42pub struct SystemClock;
43
44impl Clock for SystemClock {
45    fn now(&self) -> Timestamp {
46        Timestamp::now()
47    }
48
49    fn elapsed(&self) -> Duration {
50        static ORIGIN: OnceLock<Instant> = OnceLock::new();
51        ORIGIN.get_or_init(Instant::now).elapsed()
52    }
53}
54
55/// The instant `seconds` after `instant`.
56///
57/// Sub-second precision is kept: a bounded change asked to stand for a minute
58/// stands for a minute, rather than for a minute less however far into a second
59/// it happened to be made.
60///
61/// # Errors
62///
63/// Returns the timestamp crate's own error when the sum denotes no
64/// representable instant.
65pub fn plus_seconds(
66    instant: Timestamp,
67    seconds: i64,
68) -> Result<Timestamp, printobserver_types::TimestampError> {
69    instant.plus_seconds(seconds)
70}
71
72/// How many whole seconds `later` is after `earlier`, saturating.
73#[must_use]
74pub fn seconds_between(earlier: Timestamp, later: Timestamp) -> i64 {
75    unix_seconds(later).saturating_sub(unix_seconds(earlier))
76}
77
78/// One instant as whole seconds after the Unix epoch.
79#[must_use]
80pub fn unix_seconds(instant: Timestamp) -> i64 {
81    instant.as_utc().timestamp()
82}