icu_provider_source/datetime/
patterns.rsuse crate::cldr_serde;
use icu::datetime::provider::calendar::*;
use icu::datetime::provider::pattern;
use icu::datetime::provider::pattern::CoarseHourCycle;
impl From<&cldr_serde::ca::LengthPatterns> for patterns::LengthPatternsV1<'_> {
fn from(other: &cldr_serde::ca::LengthPatterns) -> Self {
Self {
full: other
.full
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
long: other
.long
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
medium: other
.medium
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
short: other
.short
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
}
}
}
impl From<&cldr_serde::ca::DateTimeFormats> for patterns::LengthPatternsV1<'_> {
fn from(other: &cldr_serde::ca::DateTimeFormats) -> Self {
Self {
full: other
.full
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
long: other
.long
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
medium: other
.medium
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
short: other
.short
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
}
}
}
impl From<&cldr_serde::ca::DateTimeFormats> for patterns::GenericLengthPatternsV1<'_> {
fn from(other: &cldr_serde::ca::DateTimeFormats) -> Self {
Self {
full: other
.full
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
long: other
.long
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
medium: other
.medium
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
short: other
.short
.get_pattern()
.parse()
.expect("Failed to parse pattern"),
}
}
}
impl From<&cldr_serde::ca::Dates> for DateLengthsV1<'_> {
fn from(other: &cldr_serde::ca::Dates) -> Self {
let length_combinations_v1 =
patterns::GenericLengthPatternsV1::from(&other.datetime_formats);
Self {
date: (&other.date_formats).into(),
length_combinations: length_combinations_v1,
}
}
}
impl From<&cldr_serde::ca::Dates> for TimeLengthsV1<'_> {
fn from(other: &cldr_serde::ca::Dates) -> Self {
let length_combinations_v1 =
patterns::GenericLengthPatternsV1::from(&other.datetime_formats);
let skeletons_v1 = DateSkeletonPatternsV1::from(&other.datetime_formats.available_formats);
let pattern_str_full = other.time_formats.full.get_pattern();
let pattern_str_long = other.time_formats.long.get_pattern();
let pattern_str_medium = other.time_formats.medium.get_pattern();
let pattern_str_short = other.time_formats.short.get_pattern();
let pattern_full = pattern_str_full
.parse()
.expect("Failed to create a full Pattern from bytes.");
let pattern_long = pattern_str_long
.parse()
.expect("Failed to create a long Pattern from bytes.");
let pattern_medium = pattern_str_medium
.parse()
.expect("Failed to create a medium Pattern from bytes.");
let pattern_short = pattern_str_short
.parse()
.expect("Failed to create a short Pattern from bytes.");
let mut preferred_hour_cycle: Option<CoarseHourCycle> = None;
let arr = [
pattern::CoarseHourCycle::determine(&pattern_full),
pattern::CoarseHourCycle::determine(&pattern_long),
pattern::CoarseHourCycle::determine(&pattern_medium),
pattern::CoarseHourCycle::determine(&pattern_short),
];
let iter = arr.iter().flatten();
for hour_cycle in iter {
if let Some(preferred_hour_cycle) = preferred_hour_cycle {
assert_eq!(
*hour_cycle, preferred_hour_cycle,
"A locale contained a mix of coarse hour cycle types"
);
} else {
preferred_hour_cycle = Some(*hour_cycle);
}
}
let preferred_hour_cycle =
preferred_hour_cycle.expect("Could not find a preferred hour cycle.");
let alt_hour_cycle = if preferred_hour_cycle == CoarseHourCycle::H11H12 {
CoarseHourCycle::H23H24
} else {
CoarseHourCycle::H11H12
};
let (time_h11_h12, time_h23_h24) = {
let time = (&other.time_formats).into();
let alt_time = patterns::LengthPatternsV1 {
full: alt_hour_cycle
.apply_on_pattern(
&length_combinations_v1,
&skeletons_v1,
pattern_str_full,
pattern_full,
)
.as_ref()
.expect("Failed to apply a coarse hour cycle to a full pattern.")
.into(),
long: alt_hour_cycle
.apply_on_pattern(
&length_combinations_v1,
&skeletons_v1,
pattern_str_long,
pattern_long,
)
.as_ref()
.expect("Failed to apply a coarse hour cycle to a long pattern.")
.into(),
medium: alt_hour_cycle
.apply_on_pattern(
&length_combinations_v1,
&skeletons_v1,
pattern_str_medium,
pattern_medium,
)
.as_ref()
.expect("Failed to apply a coarse hour cycle to a medium pattern.")
.into(),
short: alt_hour_cycle
.apply_on_pattern(
&length_combinations_v1,
&skeletons_v1,
pattern_str_short,
pattern_short,
)
.as_ref()
.expect("Failed to apply a coarse hour cycle to a short pattern.")
.into(),
};
match preferred_hour_cycle {
CoarseHourCycle::H11H12 => (time, alt_time),
CoarseHourCycle::H23H24 => (alt_time, time),
}
};
Self {
time_h11_h12,
time_h23_h24,
preferred_hour_cycle,
}
}
}