Skip to main content

Clock

Trait Clock 

Source
pub trait Clock: Send + Sync {
    // Required method
    fn now(&self) -> DateTime<Utc>;
}
Expand description

A source of the current time, so domain/application code doesn’t call chrono::Utc::now directly and become untestable.

crate::adapter::clock::SystemClock wraps the real wall clock; crate::mock::clock::FixedClock is a test double that returns a configured time.

§Examples

use ddd_toolkit_core::port::clock::Clock;

struct FixedClock(chrono::DateTime<chrono::Utc>);

impl Clock for FixedClock {
    fn now(&self) -> chrono::DateTime<chrono::Utc> {
        self.0
    }
}

fn is_in_the_future(clock: &dyn Clock, deadline: chrono::DateTime<chrono::Utc>) -> bool {
    clock.now() < deadline
}

let noon = "2026-07-21T12:00:00Z".parse().unwrap();
let clock = FixedClock(noon);

assert!(is_in_the_future(&clock, "2026-07-21T13:00:00Z".parse().unwrap()));
assert!(!is_in_the_future(&clock, "2026-07-21T11:00:00Z".parse().unwrap()));

Required Methods§

Source

fn now(&self) -> DateTime<Utc>

The current time.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§