Clock

Struct Clock 

Source
pub struct Clock<Tz: TimeZone> { /* private fields */ }
Expand description

A testable source of time

By using a Clock as the source of time in your code, you can choose in testing to replace it with a fake. Your test harness can then control how your production code sees time passing.

Implementations§

Source§

impl Clock<Utc>

Source

pub fn new() -> Self

Create a new UTC clock that matches wall-clock time

Examples found in repository?
examples/basic.rs (line 10)
9    pub fn new() -> Self {
10        Self::new_with_clock(Clock::new())
11    }
Source§

impl<Tz: TimeZone> Clock<Tz>
where <Tz as TimeZone>::Offset: Display,

Source

pub fn new_with_timezone(timezone: Tz) -> Self

Create a new clock in this TimeZone

Source

pub fn new_fake(start: DateTime<Tz>) -> Self

Create a new fake clock in this [‘TimeZone’].

Note that the time reported by this clock will not change from start until the clock is advanced using Clock::advance().

Source

pub fn now(&self) -> DateTime<Tz>

Get the current time

Examples found in repository?
examples/basic.rs (line 18)
17    pub fn get_time(&self) -> String {
18        self.clock.now().to_rfc2822()
19    }
Source

pub fn is_fake(&self) -> bool

Check whether the clock is fake

Using this in production code defeats the point, but you may wish to use it in utilities that could be called from either production or test code.

Source

pub fn split(&self) -> Clock<Tz>

Divide this clock

This is equivalent to Clone when using the default, wall clock. The following discussion pertains only to the fake clock case which is intended to be used in testing; in that sense, getting it wrong is likely to be caught by the tests themselves.

When using the fake clock, things become more complicated to enable testing. Clones of the same clock are divided into groups. When you call clone, you get a new clock in the same group as the clock you clone. If you want a Clock in a new group of its own, you must call Clock::split.

For the clock to advance, all groups must either be sleeping or advancing - that means someone must have called either sleep or advance on exactly one instance in each group. So you can think of a group as being the clocks in one execution context (task/thread).

Source

pub async fn advance(&mut self, duration: Duration) -> (DateTime<Tz>, Duration)

Move a fake clock forward by some duration

This will panic if called on a wall clock.

Source

pub fn advance_blocking( &mut self, duration: Duration, ) -> (DateTime<Tz>, Duration)

Move a fake clock forward by some duration

This will panic if called on a wall clock.

Source

pub async fn sleep(&mut self, duration: Duration)

Sleep for some duration

Source

pub fn sleep_blocking(&mut self, duration: Duration)

Sleep for some duration

This should not be called from an async context.

Trait Implementations§

Source§

impl<Tz: Clone + TimeZone> Clone for Clock<Tz>

Source§

fn clone(&self) -> Clock<Tz>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Tz: Debug + TimeZone> Debug for Clock<Tz>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Clock<Utc>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Tz> Freeze for Clock<Tz>
where Tz: Freeze,

§

impl<Tz> RefUnwindSafe for Clock<Tz>
where Tz: RefUnwindSafe,

§

impl<Tz> Send for Clock<Tz>
where Tz: Send, <Tz as TimeZone>::Offset: Send,

§

impl<Tz> Sync for Clock<Tz>
where Tz: Sync, <Tz as TimeZone>::Offset: Send,

§

impl<Tz> Unpin for Clock<Tz>
where Tz: Unpin,

§

impl<Tz> UnwindSafe for Clock<Tz>
where Tz: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V