Struct icu_datetime::neo_pattern::DateTimePattern
source · pub struct DateTimePattern { /* private fields */ }
Expand description
A pattern for formatting a datetime in a calendar.
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::DateTime;
use icu::calendar::Gregorian;
use icu::datetime::neo::TypedNeoFormatter;
use icu::datetime::neo_marker::NeoYearMonthDayMarker;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::datetime::neo_skeleton::NeoSkeletonLength;
use icu::datetime::options::components;
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 =
TypedNeoFormatter::<Gregorian, NeoYearMonthDayMarker>::try_new(
&locale!("es-MX").into(),
NeoSkeletonLength::Medium.into(),
)
.unwrap()
// The pattern can depend on the datetime being formatted.
// For this example, we'll choose the local Unix epoch.
.format(&DateTime::local_unix_epoch().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<Self, PatternError>
pub fn try_from_pattern_str(pattern_str: &str) -> Result<Self, 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) -> Self
fn from(value: &DateTimePattern) -> Self
Converts to this type from the input type.
source§impl FromStr for DateTimePattern
impl FromStr for DateTimePattern
source§impl PartialEq for DateTimePattern
impl PartialEq for DateTimePattern
source§impl Writeable for DateTimePattern
impl Writeable for DateTimePattern
source§fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
fn write_to<W: Write + ?Sized>(&self, sink: &mut W) -> Result
Writes a string to the given sink. Errors from the sink are bubbled up.
The default implementation delegates to
write_to_parts
, and discards any
Part
annotations.§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,
Write bytes and
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.§fn writeable_length_hint(&self) -> LengthHint
fn writeable_length_hint(&self) -> LengthHint
Returns a hint for the number of UTF-8 bytes that will be written to the sink. Read more
§fn write_to_string(&self) -> Cow<'_, str>
fn write_to_string(&self) -> Cow<'_, str>
Creates a new
String
with the data from this Writeable
. Like ToString
,
but smaller and faster. Read more§fn writeable_cmp_bytes(&self, other: &[u8]) -> Ordering
fn writeable_cmp_bytes(&self, other: &[u8]) -> Ordering
Compares the contents of this
Writeable
to the given bytes
without allocating a String to hold the Writeable
contents. Read moreimpl 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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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