migrate jiff-icu to ICU4X 2.0 (beta 2), add Custom trait for hooking localization into Jiff's fmt::strtime APIs, optimize strftime#338
Merged
BurntSushi merged 8 commits intomasterfrom Apr 28, 2025
Conversation
This basically does what is necessary to get everything compiled and tests passing. We'll add on time zone and offset stuff in a subsequent commit. Note that we now depend on `icu_calendar` and `icu_time` directly, with the latter being optional (but enabled by default). In particular, one can do useful things with just conversions to dates with `icu_calendar`. But I expect most folks will want both.
That API was added, so this is really no longer needed.
I've finally relented and exposes a very restricted way to build an `Error` value. I was motivated to do this because there are some traits in Jiff that use `jiff::Error` in their return types. Without a constructor like this, it isn't possible for implementors of that trait to provide their own error values.
Custom trait for hooking localization into Jiff's fmt::strtime APIs
Specifically, this permits customizing the %c, %r, %X and %x conversion specifiers. (Which are also newly added in this commit.) We also include a default behavior for these specifiers (meant to match Unicode's `und` locale) as well as an opt-in POSIX behavior (meant to match POSIX's `C` locale). Some minor refactoring is also included here. It was too annoying to split into its own commit.
Custom trait for hooking localization into Jiff's fmt::strtime APIsjiff-icu to ICU4X 2.0 (beta 2), add Custom trait for hooking localization into Jiff's fmt::strtime APIs
This actually never got any optimization attention, and it looks like its perf matters to coreutils. So let's take a look at it! Ref uutils/coreutils#7852
jiff-icu to ICU4X 2.0 (beta 2), add Custom trait for hooking localization into Jiff's fmt::strtime APIsjiff-icu to ICU4X 2.0 (beta 2), add Custom trait for hooking localization into Jiff's fmt::strtime APIs, optimize strftime
This is mostly just using `#[inline]` in variouos spots and specializing common code paths. The most interesting change is that a `BrokenDownTime` now contains a `Option<TimeZone>` instead of a time zone abbreviation. It turns out that getting the time zone abbreviation for a fixed offset time zone does some work up-front to format the string. By just storing the time zone, we can defer that work until we know we actually need it.
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.
The ICU4X project seems to be slowly working toward a 2.0 release. It
is currently on
beta.2. There are some nice improvements here, inparticular, the ability to localize zoned datetimes.
This PR migrates to ICU4X 2.0 (first commit) and then adds support for
conversions between time zones, offsets and zoned datetimes (second
commit). I've also added a basic example demonstrating how to format a
jiff::Zonedvalue.I've also added support for
%c,%r,%Xand%xtojiff::fmt::strtime.Since I have no plans for Jiff to do localization, and since these specifiers
are commonly implemented via localization, I've added some
infrastructure to
jiff::fmt::strtimethat enables overriding the behaviorof these specifiers. Specifically, in Biff (not yet released), I've used this
trait with ICU4X to provide localized datetime formatting.
I'm somewhat conflicted about adding a hook for localization into
strtime-style APIs. In particular, the ideal way to do localization isjust by using ICU4X directly. By doing it through
strftime, you'resomewhat limiting yourself quite a bit. But it can be quite
convenient, and it can be important if one is trying to build or
integrate with legacy APIs like POSIX. To mitigate this, I've added
documentation warning folks against using this functionality, and
instead pointing them toward ICU4X.
I also did some work optimizing
strftimein this PR, spurred byuutils/coreutils#7852.
Here's the improvement from this PR for Jiff:
And relative to
chronoandtime(a little tricky to read this output since we distinguish betweenprebuiltandoneshot, and it is appropriate to compare Jiff'soneshotwith Chrono'sprebuilt, since Jiff doesn't have aprebuiltAPI):