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;
// Real-time clock for production
let clock = ClockHandle::realtime();
// Manual clock for testing - returns (handle, controller)
let (clock, ctrl) = ClockHandle::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 manual() -> (Self, ClockController)
pub fn manual() -> (Self, ClockController)
Create a manual clock starting at the current time.
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;
let (clock, ctrl) = ClockHandle::manual();Sourcepub fn manual_at(start_at: DateTime<Utc>) -> (Self, ClockController)
pub fn manual_at(start_at: DateTime<Utc>) -> (Self, ClockController)
Create a manual clock starting at a specific time.
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;
use chrono::Utc;
let (clock, ctrl) = ClockHandle::manual_at(Utc::now() - chrono::Duration::days(30));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 manual clocks, this returns the current manual 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 manual clocks, this waits until time is advanced.
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 today(&self) -> NaiveDate
pub fn today(&self) -> NaiveDate
Get the current date (without time component).
This is equivalent to clock.now().date_naive().
Sourcepub fn manual_now(&self) -> Option<DateTime<Utc>>
pub fn manual_now(&self) -> Option<DateTime<Utc>>
Get the current manual time, if this is a manual clock.
Returns:
Nonefor realtime clocksSome(time)for manual clocks
This is useful for code that needs to cache time when running under manual 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 UnsafeUnpin 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