Skip to content

Commit a0fd595

Browse files
Add the AM/PM getters on FixedCalendarDateTimeNames
1 parent 677f392 commit a0fd595

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

components/datetime/src/pattern/names.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,89 @@ impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet> {
27432743
}
27442744
}
27452745

2746+
impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet>
2747+
where
2748+
FSet::DayPeriodNames: NamesContainer<
2749+
DayPeriodNamesV1,
2750+
DayPeriodNameLength,
2751+
Container = DataPayloadWithVariables<DayPeriodNamesV1, DayPeriodNameLength>,
2752+
>,
2753+
{
2754+
/// Gets the "AM" day period symbol for the specified length if the data is loaded.
2755+
///
2756+
/// Returns `Some` if the data for the specified length is loaded, or `None` if not loaded.
2757+
///
2758+
/// # Examples
2759+
///
2760+
/// ```
2761+
/// use icu::calendar::Gregorian;
2762+
/// use icu::datetime::pattern::{DayPeriodNameLength, FixedCalendarDateTimeNames};
2763+
/// use icu::locale::locale;
2764+
///
2765+
/// let mut names =
2766+
/// FixedCalendarDateTimeNames::<Gregorian>::try_new(locale!("en").into())
2767+
/// .unwrap();
2768+
///
2769+
/// // Before loading data, the getter returns None:
2770+
/// assert_eq!(names.get_am(DayPeriodNameLength::Abbreviated), None);
2771+
///
2772+
/// // Load the day period names:
2773+
/// names
2774+
/// .include_day_period_names(DayPeriodNameLength::Abbreviated)
2775+
/// .unwrap();
2776+
///
2777+
/// // Now we can get the AM symbol for the loaded length:
2778+
/// assert_eq!(names.get_am(DayPeriodNameLength::Abbreviated), Some("AM"));
2779+
///
2780+
/// // But other lengths are not loaded:
2781+
/// assert_eq!(names.get_am(DayPeriodNameLength::Wide), None);
2782+
/// ```
2783+
pub fn get_am(&self, length: DayPeriodNameLength) -> Option<&str> {
2784+
let borrowed = self.inner.as_borrowed();
2785+
borrowed
2786+
.dayperiod_names
2787+
.get_with_variables(length)
2788+
.and_then(|names| names.am())
2789+
}
2790+
2791+
/// Gets the "PM" day period symbol for the specified length if the data is loaded.
2792+
///
2793+
/// Returns `Some` if the data for the specified length is loaded, or `None` if not loaded.
2794+
///
2795+
/// # Examples
2796+
///
2797+
/// ```
2798+
/// use icu::calendar::Gregorian;
2799+
/// use icu::datetime::pattern::{DayPeriodNameLength, FixedCalendarDateTimeNames};
2800+
/// use icu::locale::locale;
2801+
///
2802+
/// let mut names =
2803+
/// FixedCalendarDateTimeNames::<Gregorian>::try_new(locale!("en").into())
2804+
/// .unwrap();
2805+
///
2806+
/// // Before loading data, the getter returns None:
2807+
/// assert_eq!(names.get_pm(DayPeriodNameLength::Wide), None);
2808+
///
2809+
/// // Load the day period names:
2810+
/// names
2811+
/// .include_day_period_names(DayPeriodNameLength::Wide)
2812+
/// .unwrap();
2813+
///
2814+
/// // Now we can get the PM symbol for the loaded length:
2815+
/// assert_eq!(names.get_pm(DayPeriodNameLength::Wide), Some("PM"));
2816+
///
2817+
/// // But other lengths are not loaded:
2818+
/// assert_eq!(names.get_pm(DayPeriodNameLength::Abbreviated), None);
2819+
/// ```
2820+
pub fn get_pm(&self, length: DayPeriodNameLength) -> Option<&str> {
2821+
let borrowed = self.inner.as_borrowed();
2822+
borrowed
2823+
.dayperiod_names
2824+
.get_with_variables(length)
2825+
.and_then(|names| names.pm())
2826+
}
2827+
}
2828+
27462829
impl<FSet: DateTimeNamesMarker> DateTimeNames<FSet> {
27472830
/// Maps a [`FixedCalendarDateTimeNames`] of a specific `FSet` to a more general `FSet`.
27482831
///

0 commit comments

Comments
 (0)