pub struct Clock(/* private fields */);Expand description
An injectable source of “now”. Cloning is cheap and, for a test clock, shares the same controllable offset — so a handle handed to a test moves in lockstep with the clock the handler resolves.
Implementations§
Source§impl Clock
impl Clock
Sourcepub fn system() -> Self
pub fn system() -> Self
The real clock: now() reads SystemTime::now() each call. This is
what App::new() provides by default.
Sourcepub fn test() -> Self
pub fn test() -> Self
A controllable test clock. The base is SystemTime::now() at creation
— tests assert on movement (advance/set), not on an absolute base,
so a realistic starting instant is fine and avoids surprising callers
that subtract UNIX_EPOCH.
Sourcepub fn now(&self) -> SystemTime
pub fn now(&self) -> SystemTime
The current instant. For Clock::system this is the live system
clock; for Clock::test it is base + accumulated offset.
Sourcepub fn advance(&self, d: Duration)
pub fn advance(&self, d: Duration)
Move a test clock forward by d. Panics on a system clock — advancing
real time is meaningless, and silently ignoring it would hide a test bug.
Sourcepub fn set(&self, when: SystemTime)
pub fn set(&self, when: SystemTime)
Pin a test clock to an absolute instant. Panics on a system clock for
the same reason as advance. Useful for cron/expiry
tests that need a specific wall-clock time rather than a delta.