Struct icu_datetime::datetime::TypedDateTimeFormatter
source · pub struct TypedDateTimeFormatter<C>(pub(crate) DateTimeFormatter, PhantomData<C>);
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::locid::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
.
Tuple Fields§
§0: DateTimeFormatter
§1: PhantomData<C>
Implementations§
source§impl<C: CldrCalendar> TypedDateTimeFormatter<C>
impl<C: CldrCalendar> TypedDateTimeFormatter<C>
sourcepub fn try_from_date_and_time(
date: TypedDateFormatter<C>,
time: TimeFormatter,
) -> Result<Self, DateTimeError>
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::locid::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();
sourcepub fn try_new(
locale: &DataLocale,
options: DateTimeFormatterOptions,
) -> Result<Self, DateTimeError>where
Baked: DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker>,
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.
§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::{options::length, TypedDateTimeFormatter};
use icu::locid::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");
sourcepub fn try_new_with_any_provider(
provider: &(impl AnyProvider + ?Sized),
locale: &DataLocale,
options: DateTimeFormatterOptions,
) -> Result<Self, DateTimeError>
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
.
sourcepub 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,
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
.
sourcepub fn format<'l, T>(&'l self, value: &T) -> FormattedDateTime<'l>where
T: DateTimeInput<Calendar = C>,
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::locid::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");
sourcepub fn format_to_string(
&self,
value: &impl DateTimeInput<Calendar = C>,
) -> String
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::locid::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");
Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for TypedDateTimeFormatter<C>
impl<C> RefUnwindSafe for TypedDateTimeFormatter<C>where
C: RefUnwindSafe,
impl<C> !Send for TypedDateTimeFormatter<C>
impl<C> !Sync for TypedDateTimeFormatter<C>
impl<C> Unpin for TypedDateTimeFormatter<C>where
C: Unpin,
impl<C> UnwindSafe for TypedDateTimeFormatter<C>where
C: UnwindSafe,
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