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 1 commit
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
Original file line number Diff line number Diff line change
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
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 @@ -52,6 +52,14 @@ impl TimeZone {
return Self::from_file(&mut file);
}

// attributes are not allowed on if blocks in Rust 1.38
#[cfg(target_os = "android")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this moved up to above line 46? Also what do we do in the case that the tz_string is "localtime" on android?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have yet to find exhaustive documentation on this, but from what I can tell, tzdata doesn't contain a localtime entry, and there is /etc/localtime like on other Unix systems. Moving the tzdata block up sounds good to me.

Copy link

Choose a reason for hiding this comment

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

I've checked an Android device here, and it does not have an /etc/localtime - perhaps they did away with it in later Android releases, or it varies by manufacturer? Android appears to set TZ to the timezone configured in the settings by default, so presumably users would not hit this unless they explicitly overwrote the default value.

{
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")]
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