Struct icu::datetime::neo_pattern::DateTimePattern
source · pub struct DateTimePattern { /* private fields */ }
Expand description
A pattern for formatting a datetime in a calendar.
DateTimePattern
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:
FormattedNeoDateTime::pattern
Things you can do with one of these:
- Use it to directly format a datetime via
TypedDateTimeNames
- 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::Date;
use icu::calendar::Gregorian;
use icu::datetime::fieldset::YMD;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::datetime::options::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_iso(2024, 1, 1).unwrap().to_calendar(Gregorian))
.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);
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
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 Bag
impl From<&DateTimePattern> for Bag
source§fn from(value: &DateTimePattern) -> Bag
fn from(value: &DateTimePattern) -> Bag
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