Implement experimental libc time API example using zoneinfo64#7992
Draft
Manishearth wants to merge 1 commit into
Draft
Implement experimental libc time API example using zoneinfo64#7992Manishearth wants to merge 1 commit into
Manishearth wants to merge 1 commit into
Conversation
…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.
fc5bfd5 to
d5e4347
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Create a C-compatible FFI example at
utils/zoneinfo64/examples/bionic_api.rsthat implements standard Android Bionic / POSIX time zone APIs (tzset,localtime,localtime_r,gmtime,gmtime_r, andmktime) backed by the ICU4Xzoneinfo64crate.The actual code here is largely agent generated, with heavy prompting and review.
There's very little actual timezone handling here, since
zoneinfo64does 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
calendrical_calculationsfor converting between dates and days-since-epoch. In Bionic we probably want to use something else, see comments oncivil_from_daysTZ="EST5EDT,M3.2.0,M11.1.0". We can use the preexisting tzparse function for thatThe biggest missing gap here is that libc defines globals
tzname,daylight, andtimezone, which are global mutable statics that get set bytzset(etc) and can be read from C code. This code simulates some of this with an RWLock but it does not handletzname, 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 fromtzname, but more importantly to actual use;strptimeallows parsingPDTas%Zbased on the current set timezone1.zoneinfo64does not contain the data to do this. We would need to build in additional data from somewhere for this.Changelog (N/A)
Footnotes
These abbreviations are non-unique: PDT may mean one of many timezones. So we do not need to ever parse
TZ=PDTasAmerica/Los_Angeles, however if the timezone is already set toAmerica/Los_Angeles,strptimewith%ZandPDTshould correctly parse as daylight time. ↩