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:

  1. From a custom pattern string: DateTimePattern::try_from_pattern_str
  2. From a formatted datetime: FormattedNeoDateTime::pattern

Things you can do with one of these:

  1. Use it to directly format a datetime via TypedDateTimeNames
  2. Convert it to a string pattern via [Writeable]
  3. 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

source

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

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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.

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<&DateTimePattern> for Bag

source§

fn from(value: &DateTimePattern) -> Self

Converts to this type from the input type.
source§

impl FromStr for DateTimePattern

§

type Err = PatternError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl PartialEq for DateTimePattern

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Writeable for DateTimePattern

source§

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,

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

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>

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

Compares the contents of this Writeable to the given bytes without allocating a String to hold the Writeable contents. Read more
source§

impl Eq for DateTimePattern

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,