Zoned

Struct Zoned 

Source
pub struct Zoned<C: Clock> { /* private fields */ }
Expand description

A wrapper that adds timezone support to any clock.

Zoned<C> wraps any clock implementing Clock and adds timezone functionality by implementing ZonedClock. It can convert UTC time to local time in the specified timezone.

§Deref Behavior

This type implements Deref to allow direct access to the wrapped clock’s methods. This is particularly useful when wrapping controllable clocks like MockClock.

§Examples

use prism3_clock::{Clock, ZonedClock, SystemClock, Zoned};
use chrono_tz::Asia::Shanghai;

// Wrap a SystemClock with Shanghai timezone
let clock = Zoned::new(SystemClock::new(), Shanghai);
let local = clock.local_time();
println!("Local time in Shanghai: {}", local);

§Using with MockClock

use prism3_clock::{
    Clock, ZonedClock, ControllableClock, MockClock, Zoned
};
use chrono::{DateTime, Utc};
use chrono_tz::Asia::Shanghai;

let mock = MockClock::new();
let clock = Zoned::new(mock, Shanghai);

// Can use ZonedClock methods
let local = clock.local_time();

// Can also use ControllableClock methods via Deref
let time = DateTime::parse_from_rfc3339(
    "2024-01-01T00:00:00Z"
).unwrap().with_timezone(&Utc);
clock.set_time(time);

§Author

Haixing Hu

Implementations§

Source§

impl<C: Clock> Zoned<C>

Source

pub fn new(clock: C, timezone: Tz) -> Self

Creates a new Zoned clock wrapping the given clock with the specified timezone.

§Arguments
  • clock - The clock to wrap.
  • timezone - The timezone to use for local time conversions.
§Returns

A new Zoned<C> instance.

§Examples
use prism3_clock::{SystemClock, Zoned};
use chrono_tz::Asia::Shanghai;

let clock = Zoned::new(SystemClock::new(), Shanghai);
Source

pub fn inner(&self) -> &C

Returns a reference to the inner clock.

§Returns

A reference to the wrapped clock.

§Examples
use prism3_clock::{Clock, SystemClock, Zoned};
use chrono_tz::Asia::Shanghai;

let clock = Zoned::new(SystemClock::new(), Shanghai);
let inner = clock.inner();
let millis = inner.millis();
Source

pub fn into_inner(self) -> C

Consumes the Zoned wrapper and returns the inner clock.

§Returns

The wrapped clock.

§Examples
use prism3_clock::{SystemClock, Zoned};
use chrono_tz::Asia::Shanghai;

let clock = Zoned::new(SystemClock::new(), Shanghai);
let inner = clock.into_inner();

Trait Implementations§

Source§

impl<C: Clock> Clock for Zoned<C>

Source§

fn millis(&self) -> i64

Returns the current time as a Unix timestamp in milliseconds (UTC). Read more
Source§

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

Returns the current time as a DateTime<Utc>. Read more
Source§

impl<C: Clone + Clock> Clone for Zoned<C>

Source§

fn clone(&self) -> Zoned<C>

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<C: Debug + Clock> Debug for Zoned<C>

Source§

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

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

impl<C: Clock> Deref for Zoned<C>

Source§

type Target = C

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<C: Clock> ZonedClock for Zoned<C>

Source§

fn timezone(&self) -> Tz

Returns the timezone of this clock. Read more
Source§

fn local_time(&self) -> DateTime<Tz>

Returns the current local time in this clock’s timezone. Read more

Auto Trait Implementations§

§

impl<C> Freeze for Zoned<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Zoned<C>
where C: RefUnwindSafe,

§

impl<C> Send for Zoned<C>

§

impl<C> Sync for Zoned<C>

§

impl<C> Unpin for Zoned<C>
where C: Unpin,

§

impl<C> UnwindSafe for Zoned<C>
where C: 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.