Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract timezone info from tzdata file on Android #978

Merged
merged 2 commits into from May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -99,7 +99,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@v2
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
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
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
Expand Up @@ -68,19 +68,18 @@ struct Cache {
last_checked: SystemTime,
}

#[cfg(target_os = "android")]
RumovZ marked this conversation as resolved.
Show resolved Hide resolved
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()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PR code for src/offset/local/tz_info/timezone.rs (above) the fn from_posix_tz code attempts do things the "Android Way" but if that doesn't succeed it falls back to the current implementation. Specifically, if android_tzdata::find_tz_data(tz_string) fails it will fall through and use the current general implementation.

Should fn fallback_timezone code allow a similar fallback? e.g.

fn fallback_timezone() -> Option<TimeZone> {
    let tz_name = iana_time_zone::get_timezone().ok()?;
    #[cfg(target_os = "android")]
    if let Ok(bytes) = android_tzdata::find_tz_data(&tz_name).ok() {
        return TimeZone::from_tz_data(&bytes).ok();
    }
    let bytes = fs::read(format!("{}/{}", TZDB_LOCATION, tz_name)).ok()?;
    TimeZone::from_tz_data(&bytes).ok()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In from_posix_tz(), we try to parse the tz string as a transition rule as a fallback, which should or shouldn't work regardless of the OS. On the other hand, trying to find a tz file on Android won't ever work, as far as I know, so I don't think it makes sense as a fallback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. 👍

TimeZone::from_tz_data(&bytes).ok()
}

Expand Down