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 920d938
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 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 = TimeZone::local().expect("unable to parse localtime info");
});
&INFO
};

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: TimeZone = TimeZone::empty();
static INIT: Once = Once::new();
11 changes: 10 additions & 1 deletion src/offset/tz_info/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl TimeZone {
}

/// Construct the time zone associated to UTC
fn utc() -> Self {
pub(crate) fn utc() -> Self {
Self {
transitions: Vec::new(),
local_time_types: vec![LocalTimeType::UTC],
Expand All @@ -109,6 +109,15 @@ impl TimeZone {
}
}

pub(crate) const fn empty() -> Self {
Self {
transitions: Vec::new(),
local_time_types: Vec::new(),
leap_seconds: Vec::new(),
extra_rule: None,
}
}

/// Find the local time type associated to the time zone at the specified Unix time in seconds
pub(crate) fn find_local_time_type(&self, unix_time: i64) -> Result<&LocalTimeType, Error> {
self.as_ref().find_local_time_type(unix_time)
Expand Down

0 comments on commit 920d938

Please sign in to comment.