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>
impl<C: Clock> Zoned<C>
Sourcepub fn new(clock: C, timezone: Tz) -> Self
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);Sourcepub fn into_inner(self) -> C
pub fn into_inner(self) -> C
Trait Implementations§
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> 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
Mutably borrows from an owned value. Read more