-
Notifications
You must be signed in to change notification settings - Fork 264
Teach length::Bag how to switch hour cycles #840
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
cf29e4f
Teach length::Bag how to switch hour cycles
gregtatum 4e891be
De-duplicate the hour cycle in the length data
gregtatum d6d0575
Rename flexible to coarse, and restructure provider data
gregtatum 4529f36
Break out hour cycle into its own module
gregtatum 4c6f00c
Add a feature flag provider_transform_utils, and apply it to the hour…
gregtatum dc73213
Correct H23 as the stored symbol for skeletons
gregtatum b80883a
Fix CI by running `cargo make generate-readmes`
gregtatum e752509
Change the default CoarseHourCycle
gregtatum 1bedace
Rename provider_transform_utils to provider_transform_internals
gregtatum 96b09ee
Refactor Skeleton::from(pattern)
gregtatum 468c200
Rewrite all unwrap() to expect()
gregtatum 2701006
Refactor get_pattern_for_time_length
gregtatum 69fe327
Fix clippy issue with nested match
gregtatum 36fd956
Remove extraneous return
gregtatum 023e612
Merge branch 'main' into hour-cycle
gregtatum 1f127d1
Merge branch 'main' into hour-cycle
gregtatum e7ee7e3
Work around width expansion issue
gregtatum 43ee215
Merge in main
gregtatum 816746c
Update comment to be more general
gregtatum b978ed3
Merge branch 'main' into hour-cycle
gregtatum 8e56e10
Add std as a feature requirement for provider_transform_internals
gregtatum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // This file is part of ICU4X. For terms of use, please see the file | ||
| // called LICENSE at the top level of the ICU4X source tree | ||
| // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
|
||
| //! This hour cycle module represents various utilities for working with hour cycles in order | ||
| //! to apply a user preference. | ||
|
|
||
| #![doc(hidden)] | ||
| #![cfg(feature = "provider_transform_internals")] | ||
|
|
||
| use crate::fields; | ||
| use crate::pattern::{CoarseHourCycle, Pattern, PatternItem}; | ||
| use crate::provider; | ||
| use crate::skeleton; | ||
|
|
||
| /// Figure out the coarse hour cycle given a pattern, which is useful for generating the provider | ||
| /// patterns for `length::Bag`. | ||
| pub fn determine_coarse_hour_cycle(pattern: &Pattern) -> Option<CoarseHourCycle> { | ||
| for item in pattern.items() { | ||
| if let PatternItem::Field(fields::Field { | ||
| symbol: fields::FieldSymbol::Hour(pattern_hour), | ||
| length: _, | ||
| }) = item | ||
| { | ||
| return Some(match pattern_hour { | ||
| fields::Hour::H11 | fields::Hour::H12 => CoarseHourCycle::H11H12, | ||
| fields::Hour::H23 | fields::Hour::H24 => CoarseHourCycle::H23H24, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| /// Invoke the pattern matching machinery to transform the hour cycle of a pattern. This provides | ||
| /// a safe mapping from a h11/h12 to h23/h24 for transforms. | ||
| #[doc(hidden)] | ||
| #[cfg(feature = "provider_transform_internals")] | ||
| pub fn apply_coarse_hour_cycle( | ||
| datetime: &provider::gregory::patterns::DateTimeFormatsV1, | ||
| pattern_str: &str, | ||
| mut pattern: Pattern, | ||
| coarse_hour_cycle: CoarseHourCycle, | ||
| ) -> Option<String> { | ||
| for item in pattern.items_mut() { | ||
| if let PatternItem::Field(fields::Field { symbol, length: _ }) = item { | ||
| if let fields::FieldSymbol::Hour(pattern_hour) = symbol { | ||
| if match coarse_hour_cycle { | ||
| CoarseHourCycle::H11H12 => match pattern_hour { | ||
| fields::Hour::H11 | fields::Hour::H12 => true, | ||
| fields::Hour::H23 | fields::Hour::H24 => false, | ||
| }, | ||
| CoarseHourCycle::H23H24 => match pattern_hour { | ||
| fields::Hour::H11 | fields::Hour::H12 => false, | ||
| fields::Hour::H23 | fields::Hour::H24 => true, | ||
| }, | ||
| } { | ||
| // The preference hour cycle matches the pattern, bail out early and | ||
| // return the current pattern. | ||
| return Some(pattern_str.into()); | ||
| } else { | ||
| // Mutate the pattern with the new symbol, so that it can be matched against. | ||
| *symbol = fields::FieldSymbol::Hour(match coarse_hour_cycle { | ||
| CoarseHourCycle::H11H12 => fields::Hour::H12, | ||
| CoarseHourCycle::H23H24 => fields::Hour::H23, | ||
| }); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let skeleton = skeleton::Skeleton::from(&pattern); | ||
|
|
||
| match skeleton::create_best_pattern_for_fields( | ||
| &datetime.skeletons, | ||
| &datetime.length_patterns, | ||
| &skeleton.as_slice(), | ||
| &None, | ||
| // Prefer using the matched pattern directly, rather than mutating it to match the | ||
| // requested fields. | ||
| true, | ||
| ) { | ||
| skeleton::BestSkeleton::AllFieldsMatch(pattern) | ||
| | skeleton::BestSkeleton::MissingOrExtraFields(pattern) => Some(format!("{}", pattern)), | ||
| skeleton::BestSkeleton::NoMatch => None, | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.