Skip to content

Commit

Permalink
Extract timezone info from tzdata file on Android (#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
RumovZ committed May 26, 2023
1 parent 403b247 commit b4c7fb4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
target: [wasm32-unknown-unknown, wasm32-wasi, wasm32-unknown-emscripten, aarch64-apple-ios, aarch64-linux-android]
target:
[
wasm32-unknown-unknown,
wasm32-wasi,
wasm32-unknown-emscripten,
aarch64-apple-ios,
aarch64-linux-android,
]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
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
8 changes: 8 additions & 0 deletions src/offset/local/tz_info/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ impl TimeZone {
return Self::from_tz_data(&fs::read("/etc/localtime")?);
}

// 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);
}
}

let mut chars = tz_string.chars();
if chars.next() == Some(':') {
return Self::from_file(&mut find_tz_file(chars.as_str())?);
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 b4c7fb4

Please sign in to comment.