Skip to main content

Clock

Trait Clock 

Source
pub trait Clock:
    Debug
    + Send
    + Sync
    + 'static {
    // Required method
    fn now_unix_secs(&self) -> u64;

    // Provided method
    fn utc_day_number(&self) -> u64 { ... }
}
Expand description

Source of wall-clock time, in whole UNIX seconds.

The trait is intentionally minimal: every risk primitive in this crate needs seconds-since-epoch and the derived day number, and nothing else.

Debug is a supertrait so types that store an Arc<dyn Clock> (like CircuitBreaker and SessionPnl) can still #[derive(Debug)].

§Example

A frozen clock — useful when a test wants every primitive to see the exact same time without any drift between calls.

use rustrade_risk::clock::Clock;

#[derive(Debug)]
struct FrozenClock(u64);

impl Clock for FrozenClock {
    fn now_unix_secs(&self) -> u64 { self.0 }
}

let clock = FrozenClock(1_700_000_000);
assert_eq!(clock.now_unix_secs(), 1_700_000_000);
assert_eq!(clock.utc_day_number(), 1_700_000_000 / 86_400);

Required Methods§

Source

fn now_unix_secs(&self) -> u64

Current time in seconds since the UNIX epoch.

Provided Methods§

Source

fn utc_day_number(&self) -> u64

Whole days since the UNIX epoch. Used by SessionPnl to detect 00:00 UTC rollover.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<C> Clock for Arc<C>
where C: Clock + ?Sized,

Arc<ManualClock> implements Clock too — so callers can share a single clock between a test harness and the risk primitive under test without losing the manual-advance methods.

Implementors§