Skip to content

Implement experimental libc time API example using zoneinfo64#7992

Draft
Manishearth wants to merge 1 commit into
unicode-org:mainfrom
Manishearth:bionic-tz-example
Draft

Implement experimental libc time API example using zoneinfo64#7992
Manishearth wants to merge 1 commit into
unicode-org:mainfrom
Manishearth:bionic-tz-example

Conversation

@Manishearth

@Manishearth Manishearth commented May 27, 2026

Copy link
Copy Markdown
Member

Create a C-compatible FFI example at utils/zoneinfo64/examples/bionic_api.rs that implements standard Android Bionic / POSIX time zone APIs (tzset, localtime, localtime_r, gmtime, gmtime_r, and mktime) backed by the ICU4X zoneinfo64 crate.

The actual code here is largely agent generated, with heavy prompting and review.

There's very little actual timezone handling here, since zoneinfo64 does most of the work. This code mostly just translates between the types and behaviors.

The goal of this PR is to demonstrate that this is possible, and give a starting off point for actual integration. It doesn't need to be merged (though I'm not opposed to that).

Couple notes for integration

  • This just embeds zoneinfo64 data into the binary. Bionic will probably wish to do something else.
  • This uses calendrical_calculations for converting between dates and days-since-epoch. In Bionic we probably want to use something else, see comments on civil_from_days
  • This doesn't handle custom POSIX-style timezone strings like TZ="EST5EDT,M3.2.0,M11.1.0". We can use the preexisting tzparse function for that

The biggest missing gap here is that libc defines globals tzname, daylight, and timezone, which are global mutable statics that get set by tzset (etc) and can be read from C code. This code simulates some of this with an RWLock but it does not handle tzname, which is an array that contains the abbreviations for the standard and daylight time (e.g. PST/PDT). This has multiple uses: firstly this is public API and the user may expect to read things from tzname, but more importantly to actual use; strptime allows parsing PDT as %Z based on the current set timezone1.

zoneinfo64 does not contain the data to do this. We would need to build in additional data from somewhere for this.

Changelog (N/A)

Footnotes

  1. These abbreviations are non-unique: PDT may mean one of many timezones. So we do not need to ever parse TZ=PDT as America/Los_Angeles, however if the timezone is already set to America/Los_Angeles, strptime with %Z and PDT should correctly parse as daylight time.

@Manishearth Manishearth added the U-android-bionic Bionic in Android label May 27, 2026
…neinfo64

Create a C-compatible FFI example at `utils/zoneinfo64/examples/bionic_api.rs` that implements standard Android Bionic / POSIX time zone APIs (`tzset`, `localtime`, `localtime_r`, `gmtime`, `gmtime_r`, and `mktime`) backed by the ICU4X `zoneinfo64` crate.

Key features:
- Leverages `calendrical_calculations` for Gregorian date math conversions.
- Replaces risky, thread-unsafe mutable POSIX static globals (`tzname`, `daylight`, `timezone`) with a safe, thread-safe `RwLock<TimeZoneState>` structure.
- Strictly scopes all locks to prevent deadlocks in recursive FFI calls like `mktime` calling `localtime_r_safe`.
- Documents key porting gaps and considerations (abbreviations support, custom POSIX spec parsing fallbacks, and thread-unsafe FFI wrapper handling).
- Includes a full verification test suite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

U-android-bionic Bionic in Android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant