Skip to content

Commit

Permalink
sys/unix: cache time zone info
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Apr 12, 2022
1 parent 16d50c1 commit a5c43fd
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/offset/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,11 +29,17 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> DateTime<Local>
}

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<TimeZone> = None;
static INIT: Once = Once::new();

0 comments on commit a5c43fd

Please sign in to comment.