Skip to content

Commit

Permalink
Add example demostrating how to get the hour cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Dec 18, 2024
1 parent c26c10c commit c498ea7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,9 @@ impl_display_with_writeable!(FormattedDateTime<'_>);

impl FormattedDateTime<'_> {
/// Gets the pattern used in this formatted value.
///
/// From the pattern, one can check the properties of the included components, such as
/// the hour cycle being used for formatting. See [`DateTimePattern`].
pub fn pattern(&self) -> DateTimePattern {
self.pattern.to_pattern()
}
Expand Down
28 changes: 28 additions & 0 deletions components/datetime/src/pattern/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ size_test!(DateTimePattern, date_time_pattern_size, 32);
/// assert_eq!(actual_components_bag, expected_components_bag);
/// ```
///
/// Check the hour cycle of a resolved pattern:
///
/// ```
/// use icu::calendar::Time;
/// use icu::datetime::fields::components;
/// use icu::datetime::fieldsets::T;
/// use icu::datetime::pattern::DateTimePattern;
/// use icu::datetime::TimeFormatter;
/// use icu::locale::locale;
/// use icu::locale::preferences::extensions::unicode::keywords::HourCycle;
/// use writeable::assert_writeable_eq;
///
/// let pattern = TimeFormatter::try_new(
/// locale!("es-MX").into(),
/// T::medium(),
/// )
/// .unwrap()
/// // The pattern can depend on the datetime being formatted.
/// .format(&Time::try_new(12, 0, 0, 0).unwrap())
/// .pattern();
///
/// assert_writeable_eq!(pattern, "hh:mm:ss a");
///
/// // Get the hour cycle from the resolved components:
/// let components = components::Bag::from(&pattern);
/// assert_eq!(components.hour_cycle, Some(HourCycle::H12));
/// ```
///
/// [`DateTimeFormatter`]: crate::DateTimeFormatter
/// [`FormattedDateTime::pattern`]: crate::FormattedDateTime::pattern
/// [`TypedDateTimeNames`]: crate::pattern::TypedDateTimeNames
Expand Down

0 comments on commit c498ea7

Please sign in to comment.