Skip to content

Commit 83b643e

Browse files
Add the AM/PM getters on FixedCalendarDateTimeNames
1 parent 401c436 commit 83b643e

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

components/datetime/src/pattern/names.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,86 @@ impl<C: CldrCalendar, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, F
26652665
}
26662666

26672667
impl<C, FSet: DateTimeNamesMarker> FixedCalendarDateTimeNames<C, FSet> {
2668+
/// Gets the "AM" day period symbol if the data is loaded.
2669+
///
2670+
/// Returns `Some` if the data is loaded or `None` if not loaded.
2671+
///
2672+
/// # Examples
2673+
///
2674+
/// ```
2675+
/// use icu::calendar::Gregorian;
2676+
/// use icu::datetime::pattern::{DayPeriodNameLength, FixedCalendarDateTimeNames};
2677+
/// use icu::locale::locale;
2678+
///
2679+
/// let mut names =
2680+
/// FixedCalendarDateTimeNames::<Gregorian>::try_new(locale!("en").into())
2681+
/// .unwrap();
2682+
///
2683+
/// // Before loading data, the getter returns None:
2684+
/// assert_eq!(names.get_am(), None);
2685+
///
2686+
/// // Load the day period names:
2687+
/// names
2688+
/// .include_day_period_names(DayPeriodNameLength::Abbreviated)
2689+
/// .unwrap();
2690+
///
2691+
/// // Now we can get the AM symbol:
2692+
/// assert_eq!(names.get_am(), Some("AM"));
2693+
/// ```
2694+
pub fn get_am(&self) -> Option<&str> {
2695+
let borrowed = self.inner.as_borrowed();
2696+
for length in [
2697+
DayPeriodNameLength::Abbreviated,
2698+
DayPeriodNameLength::Wide,
2699+
DayPeriodNameLength::Narrow,
2700+
] {
2701+
if let Some(names) = borrowed.dayperiod_names.get_with_variables(length) {
2702+
return names.am();
2703+
}
2704+
}
2705+
None
2706+
}
2707+
2708+
/// Gets the "PM" day period symbol if the data is loaded.
2709+
///
2710+
/// Returns `Some` if the data is loaded or `None` if not loaded.
2711+
///
2712+
/// # Examples
2713+
///
2714+
/// ```
2715+
/// use icu::calendar::Gregorian;
2716+
/// use icu::datetime::pattern::{DayPeriodNameLength, FixedCalendarDateTimeNames};
2717+
/// use icu::locale::locale;
2718+
///
2719+
/// let mut names =
2720+
/// FixedCalendarDateTimeNames::<Gregorian>::try_new(locale!("en").into())
2721+
/// .unwrap();
2722+
///
2723+
/// // Before loading data, the getter returns None:
2724+
/// assert_eq!(names.get_pm(), None);
2725+
///
2726+
/// // Load the day period names:
2727+
/// names
2728+
/// .include_day_period_names(DayPeriodNameLength::Wide)
2729+
/// .unwrap();
2730+
///
2731+
/// // Now we can get the PM symbol:
2732+
/// assert_eq!(names.get_pm(), Some("PM"));
2733+
/// ```
2734+
pub fn get_pm(&self) -> Option<&str> {
2735+
let borrowed = self.inner.as_borrowed();
2736+
for length in [
2737+
DayPeriodNameLength::Abbreviated,
2738+
DayPeriodNameLength::Wide,
2739+
DayPeriodNameLength::Narrow,
2740+
] {
2741+
if let Some(names) = borrowed.dayperiod_names.get_with_variables(length) {
2742+
return names.pm();
2743+
}
2744+
}
2745+
None
2746+
}
2747+
26682748
/// Maps a [`FixedCalendarDateTimeNames`] of a specific `FSet` to a more general `FSet`.
26692749
///
26702750
/// For example, this can transform a formatter for [`DateFieldSet`] to one for

0 commit comments

Comments
 (0)