Skip to content

Commit

Permalink
handle Android timezones
Browse files Browse the repository at this point in the history
better handle fallback for unsupported os

make paths const
  • Loading branch information
esheppa committed Aug 8, 2022
1 parent 28562f0 commit 058241c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -24,7 +24,7 @@ default = ["clock", "std", "oldtime"]
alloc = []
libc = []
std = []
clock = ["std", "winapi"]
clock = ["std", "winapi", "iana-time-zone"]
oldtime = ["time"]
wasmbind = [] # TODO: empty feature to avoid breaking change in 0.4.20, can be removed later
unstable-locales = ["pure-rust-locales", "alloc"]
Expand All @@ -40,6 +40,7 @@ serde = { version = "1.0.99", default-features = false, optional = true }
pure-rust-locales = { version = "0.5.2", optional = true }
criterion = { version = "0.3", optional = true }
rkyv = {version = "0.7", optional = true}
iana-time-zone = { version = "0.1.41", optional = true }

[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
wasm-bindgen = { version = "0.2" }
Expand Down
2 changes: 1 addition & 1 deletion src/offset/local/tz_info/timezone.rs
Expand Up @@ -89,7 +89,7 @@ impl TimeZone {
/// Construct a time zone from the contents of a time zone file
///
/// Parse TZif data as described in [RFC 8536](https://datatracker.ietf.org/doc/html/rfc8536).
pub(super) fn from_tz_data(bytes: &[u8]) -> Result<Self, Error> {
pub(crate) fn from_tz_data(bytes: &[u8]) -> Result<Self, Error> {
parser::parse(bytes)
}

Expand Down
24 changes: 23 additions & 1 deletion src/offset/local/unix.rs
Expand Up @@ -57,6 +57,7 @@ impl Default for Source {
},
Err(_) => {
// as above, now() should be a better default than some constant
// TODO: see if we can improve caching in the case where the fallback is a valid timezone
Source::LocalTime { mtime: SystemTime::now(), last_checked: SystemTime::now() }
}
},
Expand Down Expand Up @@ -95,11 +96,32 @@ struct Cache {
source: Source,
}

#[cfg(target_os = "android")]
const TZDB_LOCATION: &str = " /system/usr/share/zoneinfo";

#[cfg(not(target_os = "android"))]
const TZDB_LOCATION: &str = "/usr/share/zoneinfo";

#[cfg(any(target_os = "emscripten", target_os = "wasi", target_os = "solaris"))]
fn get_fallback_timezone() -> Option<TimeZone> {
TimeZone::utc()
}

#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "solaris")))]
fn get_fallback_timezone() -> Option<TimeZone> {
let tz_name = iana_time_zone::get_timezone().ok()?;
let bytes = fs::read(format!("{TZDB_LOCATION}/{tz_name}")).ok()?;
TimeZone::from_tz_data(&bytes).ok()
}

impl Default for Cache {
fn default() -> Cache {
// default to UTC if no local timezone can be found
Cache {
zone: TimeZone::local().unwrap_or_else(|_| TimeZone::utc()),
zone: TimeZone::local()
.ok()
.or_else(get_fallback_timezone)
.unwrap_or_else(TimeZone::utc),
source: Source::default(),
}
}
Expand Down

0 comments on commit 058241c

Please sign in to comment.