pub struct DateTimePattern { /* private fields */ }Expand description
A pattern for formatting a datetime in a calendar.
❗ This type forgoes most internationalization functionality of the datetime crate.
It assumes that the pattern is already localized for the customer’s locale. Most clients
should use DateTimeFormatter instead of directly formatting with patterns.
There are two ways to make one of these:
- From a custom pattern string: 
DateTimePattern::try_from_pattern_str - From a formatted datetime: 
FormattedDateTime::pattern 
Things you can do with one of these:
- Use it to directly format a datetime via 
FixedCalendarDateTimeNames - Convert it to a string pattern via 
Writeable - Get the resolved components
 
📏 This item has a stack size of 32 bytes on the stable toolchain at release date.
§Examples
Create a pattern from a custom string and compare it to one from data, then check the resolved components:
use icu::calendar::Gregorian;
use icu::datetime::fieldsets::YMD;
use icu::datetime::input::Date;
use icu::datetime::pattern::DateTimePattern;
use icu::datetime::provider::fields::components;
use icu::datetime::FixedCalendarDateTimeFormatter;
use icu::locale::locale;
use writeable::assert_writeable_eq;
// Create the pattern from a string:
let pattern_str = "d MMM y";
let custom_pattern =
    DateTimePattern::try_from_pattern_str(pattern_str).unwrap();
assert_writeable_eq!(custom_pattern, pattern_str);
// Load data that resolves to the same pattern:
let data_pattern = FixedCalendarDateTimeFormatter::<Gregorian, _>::try_new(
    locale!("es-MX").into(),
    YMD::medium(),
)
.unwrap()
// The pattern can depend on the datetime being formatted.
.format(&Date::try_new_gregorian(2024, 1, 1).unwrap())
.pattern();
assert_writeable_eq!(data_pattern, pattern_str);
assert_eq!(custom_pattern, data_pattern);
// Check the resolved components:
let mut expected_components_bag = components::Bag::default();
expected_components_bag.year = Some(components::Year::Numeric);
expected_components_bag.month = Some(components::Month::Short);
expected_components_bag.day = Some(components::Day::NumericDayOfMonth);
let actual_components_bag = components::Bag::from(&data_pattern);
assert_eq!(actual_components_bag, expected_components_bag);Check the hour cycle of a resolved pattern:
use icu::datetime::fieldsets::T;
use icu::datetime::input::Time;
use icu::datetime::pattern::DateTimePattern;
use icu::datetime::provider::fields::components;
use icu::datetime::NoCalendarFormatter;
use icu::locale::locale;
use icu::locale::preferences::extensions::unicode::keywords::HourCycle;
use writeable::assert_writeable_eq;
let pattern =
    NoCalendarFormatter::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));Implementations§
Source§impl DateTimePattern
 
impl DateTimePattern
Sourcepub fn try_from_pattern_str(
    pattern_str: &str,
) -> Result<DateTimePattern, PatternError>
 
pub fn try_from_pattern_str( pattern_str: &str, ) -> Result<DateTimePattern, PatternError>
Creates a DateTimePattern from a pattern string.
For more details on the syntax, see UTS 35: https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
Trait Implementations§
Source§impl Debug for DateTimePattern
 
impl Debug for DateTimePattern
Source§impl Display for DateTimePattern
This trait is implemented for compatibility with fmt!.
To create a string, Writeable::write_to_string is usually more efficient.
 
impl Display for DateTimePattern
This trait is implemented for compatibility with fmt!.
To create a string, Writeable::write_to_string is usually more efficient.
Source§impl From<DateTimePattern> for Pattern<'_>
 
impl From<DateTimePattern> for Pattern<'_>
Source§fn from(value: DateTimePattern) -> Pattern<'_>
 
fn from(value: DateTimePattern) -> Pattern<'_>
Source§impl<'a> From<Pattern<'a>> for DateTimePattern
 
impl<'a> From<Pattern<'a>> for DateTimePattern
Source§fn from(pattern: Pattern<'a>) -> DateTimePattern
 
fn from(pattern: Pattern<'a>) -> DateTimePattern
Source§impl<'a> From<PatternBorrowed<'a>> for DateTimePattern
 
impl<'a> From<PatternBorrowed<'a>> for DateTimePattern
Source§fn from(pattern: PatternBorrowed<'a>) -> DateTimePattern
 
fn from(pattern: PatternBorrowed<'a>) -> DateTimePattern
Source§impl FromStr for DateTimePattern
 
impl FromStr for DateTimePattern
Source§type Err = PatternError
 
type Err = PatternError
Source§fn from_str(
    s: &str,
) -> Result<DateTimePattern, <DateTimePattern as FromStr>::Err>
 
fn from_str( s: &str, ) -> Result<DateTimePattern, <DateTimePattern as FromStr>::Err>
s to return a value of this type. Read moreSource§impl PartialEq for DateTimePattern
 
impl PartialEq for DateTimePattern
Source§impl Writeable for DateTimePattern
 
impl Writeable for DateTimePattern
Source§fn write_to<W>(&self, sink: &mut W) -> Result<(), Error>
 
fn write_to<W>(&self, sink: &mut W) -> Result<(), Error>
write_to_parts, and discards any
Part annotations.Source§fn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error>where
    S: PartsWrite + ?Sized,
 
fn write_to_parts<S>(&self, sink: &mut S) -> Result<(), Error>where
    S: PartsWrite + ?Sized,
Part annotations to the given sink. Errors from the
sink are bubbled up. The default implementation delegates to write_to,
and doesn’t produce any Part annotations.Source§fn writeable_length_hint(&self) -> LengthHint
 
fn writeable_length_hint(&self) -> LengthHint
impl Eq for DateTimePattern
Auto Trait Implementations§
impl Freeze for DateTimePattern
impl RefUnwindSafe for DateTimePattern
impl Send for DateTimePattern
impl Sync for DateTimePattern
impl Unpin for DateTimePattern
impl UnwindSafe for DateTimePattern
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
 
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
 
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
 
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more