diff --git a/components/datetime/src/pattern/names.rs b/components/datetime/src/pattern/names.rs index 58d28da55df..6fc738b2759 100644 --- a/components/datetime/src/pattern/names.rs +++ b/components/datetime/src/pattern/names.rs @@ -2743,6 +2743,89 @@ impl FixedCalendarDateTimeNames { } } +impl FixedCalendarDateTimeNames +where + FSet::DayPeriodNames: NamesContainer< + DayPeriodNamesV1, + DayPeriodNameLength, + Container = DataPayloadWithVariables, + >, +{ + /// 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::::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::::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 DateTimeNames { /// Maps a [`FixedCalendarDateTimeNames`] of a specific `FSet` to a more general `FSet`. ///