1use std::sync::OnceLock;
2
3use time::UtcOffset;
4
5pub trait UtcOffsetExt {
6 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 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
33pub 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}