diff options
Diffstat (limited to 'vendor/chrono/src/offset/local/unix.rs')
-rw-r--r-- | vendor/chrono/src/offset/local/unix.rs | 52 |
1 files changed, 19 insertions, 33 deletions
diff --git a/vendor/chrono/src/offset/local/unix.rs b/vendor/chrono/src/offset/local/unix.rs index 32aa31618..ce96a6e3b 100644 --- a/vendor/chrono/src/offset/local/unix.rs +++ b/vendor/chrono/src/offset/local/unix.rs @@ -11,15 +11,18 @@ use std::{cell::RefCell, collections::hash_map, env, fs, hash::Hasher, time::SystemTime}; use super::tz_info::TimeZone; -use super::{DateTime, FixedOffset, Local, NaiveDateTime}; -use crate::{Datelike, LocalResult, Utc}; +use super::{FixedOffset, NaiveDateTime}; +use crate::{Datelike, LocalResult}; -pub(super) fn now() -> DateTime<Local> { - let now = Utc::now().naive_utc(); - naive_to_local(&now, false).unwrap() +pub(super) fn offset_from_utc_datetime(utc: &NaiveDateTime) -> LocalResult<FixedOffset> { + offset(utc, false) } -pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<DateTime<Local>> { +pub(super) fn offset_from_local_datetime(local: &NaiveDateTime) -> LocalResult<FixedOffset> { + offset(local, true) +} + +fn offset(d: &NaiveDateTime, local: bool) -> LocalResult<FixedOffset> { TZ_INFO.with(|maybe_cache| { maybe_cache.borrow_mut().get_or_insert_with(Cache::default).offset(*d, local) }) @@ -68,19 +71,18 @@ struct Cache { last_checked: SystemTime, } -#[cfg(target_os = "android")] -const TZDB_LOCATION: &str = " /system/usr/share/zoneinfo"; - #[cfg(target_os = "aix")] const TZDB_LOCATION: &str = "/usr/share/lib/zoneinfo"; -#[allow(dead_code)] // keeps the cfg simpler #[cfg(not(any(target_os = "android", target_os = "aix")))] const TZDB_LOCATION: &str = "/usr/share/zoneinfo"; fn fallback_timezone() -> Option<TimeZone> { let tz_name = iana_time_zone::get_timezone().ok()?; + #[cfg(not(target_os = "android"))] let bytes = fs::read(format!("{}/{}", TZDB_LOCATION, tz_name)).ok()?; + #[cfg(target_os = "android")] + let bytes = android_tzdata::find_tz_data(&tz_name).ok()?; TimeZone::from_tz_data(&bytes).ok() } @@ -88,7 +90,7 @@ impl Default for Cache { fn default() -> Cache { // default to UTC if no local timezone can be found let env_tz = env::var("TZ").ok(); - let env_ref = env_tz.as_ref().map(|s| s.as_str()); + let env_ref = env_tz.as_deref(); Cache { last_checked: SystemTime::now(), source: Source::new(env_ref), @@ -102,7 +104,7 @@ fn current_zone(var: Option<&str>) -> TimeZone { } impl Cache { - fn offset(&mut self, d: NaiveDateTime, local: bool) -> LocalResult<DateTime<Local>> { + fn offset(&mut self, d: NaiveDateTime, local: bool) -> LocalResult<FixedOffset> { let now = SystemTime::now(); match now.duration_since(self.last_checked) { @@ -114,7 +116,7 @@ impl Cache { Ok(d) if d.as_secs() < 1 => (), Ok(_) | Err(_) => { let env_tz = env::var("TZ").ok(); - let env_ref = env_tz.as_ref().map(|s| s.as_str()); + let env_ref = env_tz.as_deref(); let new_source = Source::new(env_ref); let out_of_date = match (&self.source, &new_source) { @@ -154,32 +156,16 @@ impl Cache { .offset(); return match FixedOffset::east_opt(offset) { - Some(offset) => LocalResult::Single(DateTime::from_utc(d, offset)), + Some(offset) => LocalResult::Single(offset), None => LocalResult::None, }; } // we pass through the year as the year of a local point in time must either be valid in that locale, or - // the entire time was skipped in which case we will return LocalResult::None anywa. - match self - .zone + // the entire time was skipped in which case we will return LocalResult::None anyway. + self.zone .find_local_time_type_from_local(d.timestamp(), d.year()) .expect("unable to select local time type") - { - LocalResult::None => LocalResult::None, - LocalResult::Ambiguous(early, late) => { - let early_offset = FixedOffset::east_opt(early.offset()).unwrap(); - let late_offset = FixedOffset::east_opt(late.offset()).unwrap(); - - LocalResult::Ambiguous( - DateTime::from_utc(d - early_offset, early_offset), - DateTime::from_utc(d - late_offset, late_offset), - ) - } - LocalResult::Single(tt) => { - let offset = FixedOffset::east_opt(tt.offset()).unwrap(); - LocalResult::Single(DateTime::from_utc(d - offset, offset)) - } - } + .map(|o| FixedOffset::east_opt(o.offset()).unwrap()) } } |