From f5218bfa13b2edd92363f498707da788f9635e5e Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 12 Apr 2022 14:11:53 +0200 Subject: [PATCH] sys/unix: cache time zone info --- src/offset/sys/unix.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/offset/sys/unix.rs b/src/offset/sys/unix.rs index e83f9da1..3013797c 100644 --- a/src/offset/sys/unix.rs +++ b/src/offset/sys/unix.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::sync::Once; + use super::{DateTime, FixedOffset, Local, NaiveDateTime}; use crate::offset::tz_info::TimeZone; use crate::Utc; @@ -27,11 +29,17 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> DateTime } fn offset(unix: i64) -> FixedOffset { + let info = unsafe { + INIT.call_once(|| { + INFO = Some(TimeZone::local().expect("unable to parse localtime info")); + }); + INFO.as_ref().unwrap() + }; + FixedOffset::east( - TimeZone::local() - .expect("unable to parse localtime info") - .find_local_time_type(unix) - .expect("unable to select local time type") - .offset(), + info.find_local_time_type(unix).expect("unable to select local time type").offset(), ) } + +static mut INFO: Option = None; +static INIT: Once = Once::new();