BurntSushi / jiff

A date-time library for Rust that encourages you to jump into the pit of success.
The Unlicense
1.72k stars 29 forks source link

Android Platform Support #140

Open daylightwarbler opened 2 days ago

daylightwarbler commented 2 days ago

Getting the current time works as expected, but using the system Time Zone Database and obtaining the system time zone both require additional crates and lower-level Jiff methods. If Android was supported out of the box, then that would also improve ergonomics for cross-platform Android/iOS projects, since Jiff's API could be used the same way on both targets.

On Android, the Time Zone Database is in an Android-specific format. The android-tzdata crate provides TZif data for a time zone name.

use jiff::{civil::date, tz::TimeZone};

let tz_name = "America/New_York";
let tz_data = android_tzdata::find_tz_data(tz_name)?;
let tz = TimeZone::tzif(tz_name, &tz_data)?;
let zdt = date(2023, 12, 31).at(18, 30, 0, 0).to_zoned(tz)?;
assert_eq!(zdt.to_string(), "2023-12-31T18:30:00-05:00[America/New_York]");

The system time zone can be obtained with the iana-time-zone crate.

use jiff::{tz::TimeZone, Timestamp};

let tz_name = iana_time_zone::get_timezone()?;
let tz_data = android_tzdata::find_tz_data(&tz_name)?;
let tz = TimeZone::tzif(&tz_name, &tz_data)?;
let zdt_now = Timestamp::now().to_zoned(tz);
BurntSushi commented 2 days ago

Yeah I do want to support this automatically. But I wanted a concrete thing I could test somehow first. Is there an example Rust program along with perhaps a CI setup that demonstrates the full end to end need here?

Basically, I haven't written Rust programs on Android before. I do have a good idea of what needs to be done based on prior art research, but I don't know exactly how I should go about iterating and testing. If someone can help me with that part, then I can do the rest.

daylightwarbler commented 1 day ago

Ok, here’s an example Android app build with Jiff and a Github actions CI workflow. The app runs some Rust code at startup and then shows some text. The test installs and starts the app in a running emulator and checks if the app crashed or not. If an assert fails then that causes a crash and test failure.

https://github.com/daylightwarbler/android-rust-example