pub struct ClockHandle { /* private fields */ }Expand description
A handle to a clock for getting time and performing time-based operations.
This is the main interface for time operations. It’s cheap to clone and can be shared across tasks and threads. All clones share the same underlying clock, so they see consistent time.
§Creating a Clock
use es_entity::clock::{ClockHandle, ArtificialClockConfig};
// Real-time clock for production
let clock = ClockHandle::realtime();
// Artificial clock for testing - returns (handle, controller)
let (clock, ctrl) = ClockHandle::artificial(ArtificialClockConfig::manual());§Basic Operations
use es_entity::clock::ClockHandle;
use std::time::Duration;
let clock = ClockHandle::realtime();
// Get current time
let now = clock.now();
// Sleep for a duration
clock.sleep(Duration::from_secs(1)).await;
// Timeout a future
match clock.timeout(Duration::from_secs(5), some_async_operation()).await {
Ok(result) => println!("Completed: {:?}", result),
Err(_) => println!("Timed out"),
}Implementations§
Source§impl ClockHandle
impl ClockHandle
Sourcepub fn realtime() -> Self
pub fn realtime() -> Self
Create a real-time clock that uses the system clock and tokio timers.
Sourcepub fn artificial(config: ArtificialClockConfig) -> (Self, ClockController)
pub fn artificial(config: ArtificialClockConfig) -> (Self, ClockController)
Create an artificial clock with the given configuration.
Returns a tuple of (ClockHandle, ClockController). The handle provides
the common time interface, while the controller provides operations
for advancing time.
§Example
use es_entity::clock::{ClockHandle, ArtificialClockConfig, ArtificialMode};
use chrono::Utc;
// Manual mode - time only advances via controller.advance()
let (clock, ctrl) = ClockHandle::artificial(ArtificialClockConfig::manual());
// Auto mode - time advances 1000x faster than real time
let (clock, ctrl) = ClockHandle::artificial(ArtificialClockConfig::auto(1000.0));
// Start at a specific time
let (clock, ctrl) = ClockHandle::artificial(ArtificialClockConfig {
start_at: Utc::now() - chrono::Duration::days(30),
mode: ArtificialMode::Manual,
});Sourcepub fn now(&self) -> DateTime<Utc>
pub fn now(&self) -> DateTime<Utc>
Get the current time.
This is a fast, synchronous operation regardless of clock type.
For real-time clocks, this returns Utc::now().
For artificial clocks, this returns the current artificial time.
Sourcepub fn sleep(&self, duration: Duration) -> ClockSleep ⓘ
pub fn sleep(&self, duration: Duration) -> ClockSleep ⓘ
Sleep for the given duration.
For real-time clocks, this delegates to tokio::time::sleep.
For artificial clocks in manual mode, this waits until time is advanced.
For artificial clocks in auto mode, this sleeps for a scaled real duration.
Sourcepub fn timeout<F>(&self, duration: Duration, future: F) -> ClockTimeout<F> ⓘwhere
F: Future,
pub fn timeout<F>(&self, duration: Duration, future: F) -> ClockTimeout<F> ⓘwhere
F: Future,
Apply a timeout to a future.
Returns Ok(output) if the future completes before the timeout,
or Err(Elapsed) if the timeout expires first.
Sourcepub fn is_artificial(&self) -> bool
pub fn is_artificial(&self) -> bool
Check if this clock is artificial (as opposed to realtime).
Sourcepub fn artificial_now(&self) -> Option<DateTime<Utc>>
pub fn artificial_now(&self) -> Option<DateTime<Utc>>
Get the current artificial time, if this is an artificial clock that hasn’t transitioned to realtime.
Returns:
Nonefor realtime clocksNonefor artificial clocks that have transitioned to realtimeSome(time)for artificial clocks (manual or auto) that are still artificial
This is useful for code that needs to cache time when running under artificial clocks but use fresh time for realtime clocks.
Trait Implementations§
Source§impl Clone for ClockHandle
impl Clone for ClockHandle
Source§fn clone(&self) -> ClockHandle
fn clone(&self) -> ClockHandle
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ClockHandle
impl !RefUnwindSafe for ClockHandle
impl Send for ClockHandle
impl Sync for ClockHandle
impl Unpin for ClockHandle
impl !UnwindSafe for ClockHandle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more