Skip to content

Commit

Permalink
Add support for the Gregorian Calendar availableFormats
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatum committed Feb 8, 2021
1 parent 68af55a commit 988c7e2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
8 changes: 4 additions & 4 deletions components/datetime/src/provider/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ impl DateTimeDates for provider::gregory::DatesV1 {
) -> Result<Pattern> {
let date_time = &self.patterns.date_time;
let s = match style {
style::Date::Full => &date_time.full,
style::Date::Long => &date_time.long,
style::Date::Medium => &date_time.medium,
style::Date::Short => &date_time.short,
style::Date::Full => &date_time.style_patterns.full,
style::Date::Long => &date_time.style_patterns.long,
style::Date::Medium => &date_time.style_patterns.medium,
style::Date::Short => &date_time.style_patterns.short,
};
Ok(Pattern::from_bytes_combination(s, date, time)?)
}
Expand Down
12 changes: 11 additions & 1 deletion components/datetime/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub mod gregory {

pub time: patterns::StylePatternsV1,

pub date_time: patterns::StylePatternsV1,
pub date_time: patterns::DateTimeFormatsV1,
}

macro_rules! symbols {
Expand Down Expand Up @@ -193,5 +193,15 @@ pub mod gregory {
pub medium: Cow<'static, str>,
pub short: Cow<'static, str>,
}

#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct DateTimeFormatsV1 {
pub style_patterns: StylePatternsV1,
pub available_formats: Vec<(Cow<'static, str>, Cow<'static, str>)>,
}
}
}
13 changes: 11 additions & 2 deletions components/datetime/tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use std::{borrow::Cow, fmt::Write};
fn test_fixture(fixture_name: &str) {
let provider = icu_testdata::get_provider();

for fx in fixtures::get_fixture(fixture_name).unwrap().0 {
for fx in fixtures::get_fixture(fixture_name)
.expect("Unable to get fixture.")
.0
{
let langid = fx.input.locale.parse().unwrap();
let options = fixtures::get_options(&fx.input.options);
let dtf = DateTimeFormat::try_new(langid, &provider, &options).unwrap();
Expand Down Expand Up @@ -63,7 +66,13 @@ fn test_dayperiod_patterns() {
.unwrap()
.take_payload()
.unwrap();
*data.to_mut().patterns.date_time.long.to_mut() = String::from("{0}");
*data
.to_mut()
.patterns
.date_time
.style_patterns
.long
.to_mut() = String::from("{0}");
for test_case in &test.test_cases {
for dt_input in &test_case.date_times {
let date_time: MockDateTime = dt_input.parse().unwrap();
Expand Down
37 changes: 36 additions & 1 deletion components/provider_cldr/src/transform/dates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ impl From<&cldr_json::StylePatterns> for gregory::patterns::StylePatternsV1 {
}
}

impl From<&cldr_json::DateTimeFormats> for gregory::patterns::DateTimeFormatsV1 {
fn from(other: &cldr_json::DateTimeFormats) -> Self {
// TODO(#308): Support numbering system variations. We currently throw them away.
Self {
style_patterns: gregory::patterns::StylePatternsV1 {
full: other.full.get_pattern().clone(),
long: other.long.get_pattern().clone(),
medium: other.medium.get_pattern().clone(),
short: other.short.get_pattern().clone(),
},
available_formats: other.available_formats.0.clone(),
}
}
}

impl From<&cldr_json::Dates> for gregory::DatesV1 {
fn from(other: &cldr_json::Dates) -> Self {
Self {
Expand Down Expand Up @@ -389,6 +404,26 @@ pub(self) mod cldr_json {
pub short: StylePattern,
}

#[derive(PartialEq, Debug, Deserialize)]
pub struct DateTimeFormats {
pub full: StylePattern,
pub long: StylePattern,
pub medium: StylePattern,
pub short: StylePattern,
#[serde(rename = "availableFormats")]
pub available_formats: AvailableFormats,
}

#[derive(PartialEq, Clone, Debug, Deserialize)]
pub struct AvailableFormats(
#[serde(with = "tuple_vec_map")] pub(crate) Vec<(Cow<'static, str>, Cow<'static, str>)>,
);

/// This struct represents a 1:1 mapping of the CLDR ca-gregorian.json data at the key
/// "main.LANGID.dates.calendars.gregorian" where "LANGID" is the identifier.
///
/// e.g.
/// https://github.com/unicode-org/cldr-json/blob/master/cldr-json/cldr-dates-full/main/en/ca-gregorian.json
#[derive(PartialEq, Debug, Deserialize)]
pub struct GregoryDates {
pub months: months::Contexts,
Expand All @@ -400,7 +435,7 @@ pub(self) mod cldr_json {
#[serde(rename = "timeFormats")]
pub time_formats: StylePatterns,
#[serde(rename = "dateTimeFormats")]
pub date_time_formats: StylePatterns,
pub date_time_formats: DateTimeFormats,
}

#[derive(PartialEq, Debug, Deserialize)]
Expand Down

0 comments on commit 988c7e2

Please sign in to comment.