time_local/
lib.rs

1use std::sync::OnceLock;
2
3use time::UtcOffset;
4
5pub trait UtcOffsetExt {
6    /// Cached result of [`time::UtcOffset::current_local_offset`].
7    ///
8    /// # Panics
9    ///
10    /// Panics if [`crate::init`] has not been called with a succesful return value.
11    fn cached_local_offset() -> time::UtcOffset;
12}
13
14static UTC_OFFSET: OnceLock<time::UtcOffset> = OnceLock::new();
15
16impl UtcOffsetExt for time::UtcOffset {
17    fn cached_local_offset() -> time::UtcOffset {
18        *UTC_OFFSET.get().unwrap_or_else(|| utc_offset_init_error())
19    }
20}
21
22pub trait OffsetDateTimeExt {
23    /// Convenience method that calls [`time::OffsetDateTime::to_offset`] with the [`time::UtcOffset`] for `self`.
24    fn to_local(self) -> Result<time::OffsetDateTime, time::error::IndeterminateOffset>;
25}
26
27impl OffsetDateTimeExt for time::OffsetDateTime {
28    fn to_local(self) -> Result<time::OffsetDateTime, time::error::IndeterminateOffset> {
29        Ok(self.to_offset(time::UtcOffset::local_offset_at(self)?))
30    }
31}
32
33/// Call this function before your program spawns threads and before you use [`UtcOffsetExt::cached_local_offset`].
34pub fn init() -> Result<(), time::error::IndeterminateOffset> {
35    let utc_offset = UtcOffset::current_local_offset()?;
36    UTC_OFFSET
37        .set(utc_offset)
38        .unwrap_or_else(|_| utc_offset_init_error());
39    Ok(())
40}
41
42fn utc_offset_init_error() -> ! {
43    panic!(
44        "call `time_local::init()` once during application initialization before spawning threads"
45    )
46}