Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions components/datetime/src/pattern/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2743,6 +2743,89 @@ impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet> {
}
}

impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet>
where
FSet::DayPeriodNames: NamesContainer<
DayPeriodNamesV1,
DayPeriodNameLength,
Container = DataPayloadWithVariables<DayPeriodNamesV1, DayPeriodNameLength>,
>,
{
/// Gets the "AM" day period symbol for the specified length if the data is loaded.
///
/// Returns `Some` if the data for the specified length is loaded, or `None` if not loaded.
///
/// # Examples
///
/// ```
/// use icu::calendar::Gregorian;
/// use icu::datetime::pattern::{DayPeriodNameLength, FixedCalendarDateTimeNames};
/// use icu::locale::locale;
///
/// let mut names =
/// FixedCalendarDateTimeNames::<Gregorian>::try_new(locale!("en").into())
/// .unwrap();
///
/// // Before loading data, the getter returns None:
/// assert_eq!(names.get_am(DayPeriodNameLength::Abbreviated), None);
///
/// // Load the day period names:
/// names
/// .include_day_period_names(DayPeriodNameLength::Abbreviated)
/// .unwrap();
///
/// // Now we can get the AM symbol for the loaded length:
/// assert_eq!(names.get_am(DayPeriodNameLength::Abbreviated), Some("AM"));
///
/// // But other lengths are not loaded:
/// assert_eq!(names.get_am(DayPeriodNameLength::Wide), None);
/// ```
pub fn get_am(&self, length: DayPeriodNameLength) -> Option<&str> {
let borrowed = self.inner.as_borrowed();
borrowed
.dayperiod_names
.get_with_variables(length)
.and_then(|names| names.am())
}

/// Gets the "PM" day period symbol for the specified length if the data is loaded.
///
/// Returns `Some` if the data for the specified length is loaded, or `None` if not loaded.
///
/// # Examples
///
/// ```
/// use icu::calendar::Gregorian;
/// use icu::datetime::pattern::{DayPeriodNameLength, FixedCalendarDateTimeNames};
/// use icu::locale::locale;
///
/// let mut names =
/// FixedCalendarDateTimeNames::<Gregorian>::try_new(locale!("en").into())
/// .unwrap();
///
/// // Before loading data, the getter returns None:
/// assert_eq!(names.get_pm(DayPeriodNameLength::Wide), None);
///
/// // Load the day period names:
/// names
/// .include_day_period_names(DayPeriodNameLength::Wide)
/// .unwrap();
///
/// // Now we can get the PM symbol for the loaded length:
/// assert_eq!(names.get_pm(DayPeriodNameLength::Wide), Some("PM"));
///
/// // But other lengths are not loaded:
/// assert_eq!(names.get_pm(DayPeriodNameLength::Abbreviated), None);
/// ```
pub fn get_pm(&self, length: DayPeriodNameLength) -> Option<&str> {
let borrowed = self.inner.as_borrowed();
borrowed
.dayperiod_names
.get_with_variables(length)
.and_then(|names| names.pm())
}
}

impl<FSet: DateTimeNamesMarker> DateTimeNames<FSet> {
/// Maps a [`FixedCalendarDateTimeNames`] of a specific `FSet` to a more general `FSet`.
///
Expand Down