Skip to content

Commit

Permalink
Extract timezone info from tzdata file on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
RumovZ committed Mar 7, 2023
1 parent fb2f259 commit f6cc8b6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ winapi = { version = "0.3.0", features = ["std", "minwinbase", "minwindef", "tim
[target.'cfg(unix)'.dependencies]
iana-time-zone = { version = "0.1.45", optional = true, features = ["fallback"] }

[target.'cfg(target_os = "android")'.dependencies]
android-tzdata = "0.1.1"

[dev-dependencies]
serde_json = { version = "1" }
serde_derive = { version = "1", default-features = false }
Expand Down
11 changes: 11 additions & 0 deletions src/offset/local/tz_info/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::{cmp::Ordering, fmt, str};
use super::rule::{AlternateTime, TransitionRule};
use super::{parser, Error, DAYS_PER_WEEK, SECONDS_PER_DAY};

#[cfg(target_os = "android")]
extern crate android_tzdata;

/// Time zone
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct TimeZone {
Expand Down Expand Up @@ -52,6 +55,14 @@ impl TimeZone {
return Self::from_file(&mut file);
}

// attributes are not allowed on if blocks in Rust 1.38
#[cfg(target_os = "android")]
{
if let Ok(bytes) = android_tzdata::find_tz_data(tz_string) {
return Self::from_tz_data(&bytes);
}
}

// TZ string extensions are not allowed
let tz_string = tz_string.trim_matches(|c: char| c.is_ascii_whitespace());
let rule = TransitionRule::from_tz_string(tz_string.as_bytes(), false)?;
Expand Down
7 changes: 3 additions & 4 deletions src/offset/local/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,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()
}

Expand Down

0 comments on commit f6cc8b6

Please sign in to comment.