| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use core::str::FromStr; |
| 6 |
|
| 7 |
use writeable::{Writeable, impl_display_with_writeable}; |
| 8 |
|
| 9 |
use crate::provider::pattern::{PatternError, PatternItem, runtime}; |
| 10 |
use crate::size_test_macro::size_test; |
| 11 |
|
| 12 |
size_test!(DateTimePattern, date_time_pattern_size, 32); |
| 13 |
|
| 14 |
/// A pattern for formatting a datetime in a calendar. |
| 15 |
/// |
| 16 |
/// ❗ This type forgoes most internationalization functionality of the datetime crate. |
| 17 |
/// It assumes that the pattern is already localized for the customer's locale. Most clients |
| 18 |
/// should use [`DateTimeFormatter`] instead of directly formatting with patterns. |
| 19 |
/// |
| 20 |
/// This type can be created by parsing a UTS 35 datetime picture string, and it can be |
| 21 |
/// stringified back into that syntax. |
| 22 |
/// |
| 23 |
/// There are two ways to make one of these: |
| 24 |
/// |
| 25 |
/// 1. From a custom pattern string: [`DateTimePattern::try_from_pattern_str`] |
| 26 |
/// 2. From a formatted datetime: [`FormattedDateTime::pattern`] |
| 27 |
/// |
| 28 |
/// Things you can do with one of these: |
| 29 |
/// |
| 30 |
/// 1. Use it to directly format a datetime via [`FixedCalendarDateTimeNames`] |
| 31 |
/// 2. Convert it to a string pattern via [`Writeable`] |
| 32 |
/// 3. Get the resolved components |
| 33 |
#[doc = date_time_pattern_size!()] |
| 34 |
/// |
| 35 |
/// # Examples |
| 36 |
/// |
| 37 |
/// Create a pattern from a custom string and compare it to one from data, |
| 38 |
/// then check the resolved components: |
| 39 |
/// |
| 40 |
/// ``` |
| 41 |
/// use icu::calendar::Gregorian; |
| 42 |
/// use icu::datetime::FixedCalendarDateTimeFormatter; |
| 43 |
/// use icu::datetime::fieldsets::YMD; |
| 44 |
/// use icu::datetime::input::Date; |
| 45 |
/// use icu::datetime::pattern::DateTimePattern; |
| 46 |
/// use icu::datetime::provider::fields::components; |
| 47 |
/// use icu::locale::locale; |
| 48 |
/// use writeable::assert_writeable_eq; |
| 49 |
/// |
| 50 |
/// // Create the pattern from a string: |
| 51 |
/// let pattern_str = "d MMM y"; |
| 52 |
/// let custom_pattern = |
| 53 |
/// DateTimePattern::try_from_pattern_str(pattern_str).unwrap(); |
| 54 |
/// assert_writeable_eq!(custom_pattern, pattern_str); |
| 55 |
/// |
| 56 |
/// // Load data that resolves to the same pattern: |
| 57 |
/// let data_pattern = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new( |
| 58 |
/// locale!("es-MX").into(), |
| 59 |
/// YMD::medium(), |
| 60 |
/// ) |
| 61 |
/// .unwrap() |
| 62 |
/// // The pattern can depend on the datetime being formatted. |
| 63 |
/// .format(&Date::try_new_gregorian(2024, 1, 1).unwrap()) |
| 64 |
/// .pattern(); |
| 65 |
/// assert_writeable_eq!(data_pattern, pattern_str); |
| 66 |
/// assert_eq!(custom_pattern, data_pattern); |
| 67 |
/// |
| 68 |
/// // Check the resolved components: |
| 69 |
/// let mut expected_components_bag = components::Bag::default(); |
| 70 |
/// expected_components_bag.year = Some(components::Year::Numeric); |
| 71 |
/// expected_components_bag.month = Some(components::Month::Short); |
| 72 |
/// expected_components_bag.day = Some(components::Day::NumericDayOfMonth); |
| 73 |
/// let actual_components_bag = components::Bag::from(&data_pattern); |
| 74 |
/// assert_eq!(actual_components_bag, expected_components_bag); |
| 75 |
/// ``` |
| 76 |
/// |
| 77 |
/// Check the hour cycle of a resolved pattern: |
| 78 |
/// |
| 79 |
/// ``` |
| 80 |
/// use icu::datetime::NoCalendarFormatter; |
| 81 |
/// use icu::datetime::fieldsets::T; |
| 82 |
/// use icu::datetime::input::Time; |
| 83 |
/// use icu::datetime::pattern::DateTimePattern; |
| 84 |
/// use icu::datetime::provider::fields::components; |
| 85 |
/// use icu::locale::locale; |
| 86 |
/// use icu::locale::preferences::extensions::unicode::keywords::HourCycle; |
| 87 |
/// use writeable::assert_writeable_eq; |
| 88 |
/// |
| 89 |
/// let pattern = |
| 90 |
/// NoCalendarFormatter::try_new(locale!("es-MX").into(), T::medium()) |
| 91 |
/// .unwrap() |
| 92 |
/// // The pattern can depend on the datetime being formatted. |
| 93 |
/// .format(&Time::try_new(12, 0, 0, 0).unwrap()) |
| 94 |
/// .pattern(); |
| 95 |
/// |
| 96 |
/// assert_writeable_eq!(pattern, "hh:mm:ss a"); |
| 97 |
/// |
| 98 |
/// // Get the hour cycle from the resolved components: |
| 99 |
/// let components = components::Bag::from(&pattern); |
| 100 |
/// assert_eq!(components.hour_cycle, Some(HourCycle::H12)); |
| 101 |
/// ``` |
| 102 |
/// |
| 103 |
/// [`DateTimeFormatter`]: crate::DateTimeFormatter |
| 104 |
/// [`FormattedDateTime::pattern`]: crate::FormattedDateTime::pattern |
| 105 |
/// [`FixedCalendarDateTimeNames`]: crate::pattern::FixedCalendarDateTimeNames |
| 106 |
#[derive(Debug)] |
| 107 |
pub struct DateTimePattern { |
| 108 |
pattern: runtime::Pattern<'static>, |
| 109 |
} |
| 110 |
|
| 111 |
impl DateTimePattern { |
| 112 |
/// Creates a [`DateTimePattern`] from a pattern string. |
| 113 |
/// |
| 114 |
/// For more details on the syntax, see UTS 35: |
| 115 |
/// <https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns> |
| 116 |
pub fn try_from_pattern_str(pattern_str: &str) -> Result<Self, PatternError> { |
| 117 |
let pattern = runtime::Pattern::from_str(pattern_str)?; |
| 118 |
Ok(Self { pattern }) |
| 119 |
} |
| 120 |
|
| 121 |
pub(crate) fn iter_items(&self) -> impl Iterator<Item = PatternItem> + '_ { |
| 122 |
self.pattern.items.iter() |
| 123 |
} |
| 124 |
|
| 125 |
pub(crate) fn as_borrowed(&self) -> DateTimePatternBorrowed<'_> { |
| 126 |
DateTimePatternBorrowed(&self.pattern) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl<'a> From<runtime::Pattern<'a>> for DateTimePattern { |
| 131 |
fn from(pattern: runtime::Pattern<'a>) -> Self { |
| 132 |
Self { |
| 133 |
pattern: pattern.into_owned(), |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
impl<'a> From<runtime::PatternBorrowed<'a>> for DateTimePattern { |
| 139 |
fn from(pattern: runtime::PatternBorrowed<'a>) -> Self { |
| 140 |
Self { |
| 141 |
pattern: pattern.as_pattern().into_owned(), |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl From<DateTimePattern> for runtime::Pattern<'_> { |
| 147 |
fn from(value: DateTimePattern) -> Self { |
| 148 |
value.pattern |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
impl FromStr for DateTimePattern { |
| 153 |
type Err = PatternError; |
| 154 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 155 |
Self::try_from_pattern_str(s) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// Check equality on the borrowed variant since it flattens the difference |
| 160 |
// between the three `Single` pattern variants, which is not public-facing |
| 161 |
impl PartialEq for DateTimePattern { |
| 162 |
fn eq(&self, other: &Self) -> bool { |
| 163 |
self.as_borrowed().eq(&other.as_borrowed()) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
impl Eq for DateTimePattern {} |
| 168 |
|
| 169 |
impl Writeable for DateTimePattern { |
| 170 |
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result { |
| 171 |
self.pattern.write_to(sink) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
impl_display_with_writeable!(DateTimePattern); |
| 176 |
|
| 177 |
// Not clear if this needs to be public. For now, make it private. |
| 178 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 179 |
pub(crate) struct DateTimePatternBorrowed<'a>(pub(crate) &'a runtime::Pattern<'a>); |