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:

  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::Date;
use icu::calendar::Gregorian;
use icu::datetime::fieldset::YMD;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::datetime::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_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

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

source§

impl DateTimePattern

source

pub fn to_string(&self) -> String

Converts the given value to a String.

Under the hood, this uses an efficient [Writeable] implementation. However, in order to avoid allocating a string, it is more efficient to use [Writeable] directly.

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

source§

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

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

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

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
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>,

source§

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>,

source§

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,