Struct icu_datetime::TypedDateTimeFormatter

source ·
pub struct TypedDateTimeFormatter<C>(/* private fields */);
Expand description

TypedDateTimeFormatter is a formatter capable of formatting date/times from a calendar selected at compile time. For the difference between this and DateTimeFormatter, please read the crate root docs.

When constructed, it uses data from the data provider, selected locale and provided options to collect all data necessary to format any dates into that locale.

For that reason, one should think of the process of formatting a date in two steps - first, a computational heavy construction of TypedDateTimeFormatter, and then fast formatting of DateInput data using the instance. 📏 This item has a stack size of 5152 bytes on the stable toolchain at release date.

§Examples

use icu::calendar::{DateTime, Gregorian};
use icu::datetime::{options::length, TypedDateTimeFormatter};
use icu::locale::locale;
use writeable::assert_writeable_eq;

let mut options = length::Bag::from_date_time_style(
    length::Date::Medium,
    length::Time::Short,
);

let dtf = TypedDateTimeFormatter::<Gregorian>::try_new(
    &locale!("en").into(),
    options.into(),
)
.expect("Failed to create TypedDateTimeFormatter instance.");

let datetime = DateTime::try_new_gregorian_datetime(2020, 9, 1, 12, 34, 28)
    .expect("Failed to construct DateTime.");

assert_writeable_eq!(dtf.format(&datetime), "Sep 1, 2020, 12:34 PM");

This model replicates that of ICU and ECMA402.

Implementations§

source§

impl<C: CldrCalendar> TypedDateTimeFormatter<C>

source

pub fn try_from_date_and_time( date: TypedDateFormatter<C>, time: TimeFormatter, ) -> Result<Self, DateTimeError>

Constructor that takes a TimeFormatter and TypedDateFormatter and combines them into a TypedDateTimeFormatter.

§Examples
use icu::calendar::Gregorian;
use icu::datetime::{
    options::length, TimeFormatter, TypedDateFormatter,
    TypedDateTimeFormatter,
};
use icu::locale::locale;

let tf = TimeFormatter::try_new_with_length(
    &locale!("en").into(),
    length::Time::Short,
)
.expect("Failed to create TimeFormatter instance.");
let df = TypedDateFormatter::<Gregorian>::try_new_with_length(
    &locale!("en").into(),
    length::Date::Short,
)
.expect("Failed to create TypedDateFormatter instance.");

TypedDateTimeFormatter::<Gregorian>::try_from_date_and_time(df, tf)
    .unwrap();
source

pub fn try_new( locale: &DataLocale, options: DateTimeFormatterOptions, ) -> Result<Self, DateTimeError>
where Baked: DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker>,

Constructor that takes a selected locale, then collects all compiled data necessary to format date and time values into the given locale.

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::{options::length, TypedDateTimeFormatter};
use icu::locale::locale;
use writeable::assert_writeable_eq;

let options = length::Bag::from_date_time_style(
    length::Date::Medium,
    length::Time::Medium,
);

let dtf = TypedDateTimeFormatter::<Gregorian>::try_new(
    &locale!("en").into(),
    options.into(),
)
.unwrap();

let datetime =
    DateTime::try_new_gregorian_datetime(2022, 8, 31, 1, 2, 3).unwrap();

assert_writeable_eq!(dtf.format(&datetime), "Aug 31, 2022, 1:02:03 AM");
source

pub fn try_new_with_any_provider( provider: &(impl AnyProvider + ?Sized), locale: &DataLocale, options: DateTimeFormatterOptions, ) -> Result<Self, DateTimeError>

A version of Self::try_new that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), locale: &DataLocale, options: DateTimeFormatterOptions, ) -> Result<Self, DateTimeError>

A version of Self::try_new that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_unstable<D>( provider: &D, locale: &DataLocale, options: DateTimeFormatterOptions, ) -> Result<Self, DateTimeError>
where D: DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker> + DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<WeekDataV1Marker> + ?Sized,

A version of Self::try_new that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn try_new_experimental( locale: &DataLocale, options: DateTimeFormatterOptions, ) -> Result<Self, DateTimeError>
where Baked: DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker>,

Constructor that supports experimental options using compiled data.

Enabled with the compiled_data and experimental Cargo features.

📚 Help choosing a constructor

🚧 This code is experimental; it may change at any time, in breaking or non-breaking ways, including in SemVer minor releases. It can be enabled with the "experimental" Cargo feature of the icu meta-crate. Use with caution. #1317
§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::{options::components, TypedDateTimeFormatter};
use icu::locale::locale;
use writeable::assert_writeable_eq;

let mut options = components::Bag::default();
options.year = Some(components::Year::Numeric);
options.month = Some(components::Month::Long);

let dtf = TypedDateTimeFormatter::<Gregorian>::try_new_experimental(
    &locale!("en").into(),
    options.into(),
)
.unwrap();

let datetime =
    DateTime::try_new_gregorian_datetime(2022, 8, 31, 1, 2, 3).unwrap();

assert_writeable_eq!(dtf.format(&datetime), "August 2022");
source

pub fn try_new_experimental_unstable<D>( provider: &D, locale: &DataLocale, options: DateTimeFormatterOptions, ) -> Result<Self, DateTimeError>
where D: DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker> + DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<DateSkeletonPatternsV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<WeekDataV1Marker> + ?Sized,

A version of Self::try_new_experimental that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn format<'l, T>(&'l self, value: &T) -> FormattedDateTime<'l>
where T: DateTimeInput<Calendar = C>,

Takes a DateTimeInput implementer and returns an instance of a FormattedDateTime that contains all information necessary to display a formatted date and operate on it.

§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::TypedDateTimeFormatter;
use writeable::assert_writeable_eq;
use icu::locale::locale;
let dtf = TypedDateTimeFormatter::<Gregorian>::try_new(&locale!("en").into(), options.into())
    .expect("Failed to create TypedDateTimeFormatter instance.");

let datetime = DateTime::try_new_gregorian_datetime(2020, 9, 1, 12, 34, 28)
    .expect("Failed to construct DateTime.");

assert_writeable_eq!(dtf.format(&datetime), "12:34:28 PM");
source

pub fn format_to_string( &self, value: &impl DateTimeInput<Calendar = C>, ) -> String

Takes a DateTimeInput implementer and returns it formatted as a string.

§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::TypedDateTimeFormatter;
use icu::locale::locale;
let dtf = TypedDateTimeFormatter::<Gregorian>::try_new(&locale!("en").into(), options.into())
    .expect("Failed to create TypedDateTimeFormatter instance.");

let datetime = DateTime::try_new_gregorian_datetime(2020, 9, 1, 12, 34, 28)
    .expect("Failed to construct DateTime.");

assert_eq!(dtf.format_to_string(&datetime), "12:34:28 PM");
source

pub fn resolve_components(&self) -> Bag

Returns a components::Bag that represents the resolved components for the options that were provided to the TypedDateTimeFormatter. The developer may request a certain set of options for a TypedDateTimeFormatter but the locale and resolution algorithm may change certain details of what actually gets resolved.

§Examples
use icu::calendar::Gregorian;
use icu::datetime::{
    options::{components, length},
    TypedDateTimeFormatter,
};
use icu::locale::locale;

let options = length::Bag::from_date_style(length::Date::Medium).into();
let dtf = TypedDateTimeFormatter::<Gregorian>::try_new(
    &locale!("en").into(),
    options,
)
.expect("Failed to create TypedDateTimeFormatter instance.");

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);

assert_eq!(dtf.resolve_components(), expected_components_bag);

Trait Implementations§

source§

impl<C: Debug> Debug for TypedDateTimeFormatter<C>

source§

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

Formats the value using the given formatter. Read more

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