pub struct DateFormatter(/* private fields */);
Expand description
DateFormatter
is a formatter capable of formatting
dates from any calendar, selected at runtime. For the difference between this and TypedDateFormatter
,
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 DateFormatter
, and then fast formatting of DateTime
data using the instance.
๐ This item has a stack size of 4456 bytes on the stable toolchain at release date.
ยงExamples
use icu::calendar::Date;
use icu::datetime::{options::length, DateFormatter};
use icu::locid::locale;
use writeable::assert_writeable_eq;
let length = length::Date::Medium;
let df = DateFormatter::try_new_with_length(
&locale!("en-u-ca-gregory").into(),
length,
)
.expect("Failed to create TypedDateFormatter instance.");
let date =
Date::try_new_iso_date(2020, 9, 1).expect("Failed to construct Date.");
let any_date = date.to_any();
assert_writeable_eq!(
df.format(&any_date).expect("Calendars should match"),
"Sep 1, 2020"
);
This model replicates that of ICU
and ECMA402
.
Implementationsยง
Sourceยงimpl DateFormatter
impl DateFormatter
Sourcepub fn try_new_with_length(
locale: &DataLocale,
length: Date,
) -> Result<Self, DateTimeError>
pub fn try_new_with_length( locale: &DataLocale, length: Date, ) -> Result<Self, DateTimeError>
Construct a new DateFormatter
from compiled data.
This method will pick the calendar off of the locale; and if unspecified or unknown will fall back to the default
calendar for the locale. See AnyCalendarKind
for a list of supported calendars.
โจ Enabled with the compiled_data
Cargo feature.
๐ Help choosing a constructor
ยงExamples
use icu::calendar::Date;
use icu::datetime::{options::length, DateFormatter};
use icu::locid::locale;
use writeable::assert_writeable_eq;
let length = length::Date::Medium;
let locale = locale!("en-u-ca-gregory").into();
let df = DateFormatter::try_new_with_length(&locale, length)
.expect("Failed to create TypedDateFormatter instance.");
let datetime =
Date::try_new_iso_date(2020, 9, 1).expect("Failed to construct Date.");
let any_datetime = datetime.to_any();
assert_writeable_eq!(
df.format(&any_datetime).expect("Calendars should match"),
"Sep 1, 2020"
);
Sourcepub fn try_new_with_length_with_any_provider(
provider: &impl AnyProvider,
locale: &DataLocale,
length: Date,
) -> Result<Self, DateTimeError>
pub fn try_new_with_length_with_any_provider( provider: &impl AnyProvider, locale: &DataLocale, length: Date, ) -> Result<Self, DateTimeError>
A version of Self::try_new_with_length
that uses custom data provided by an AnyProvider
.
Sourcepub fn try_new_with_length_with_buffer_provider(
provider: &impl BufferProvider,
locale: &DataLocale,
length: Date,
) -> Result<Self, DateTimeError>
pub fn try_new_with_length_with_buffer_provider( provider: &impl BufferProvider, locale: &DataLocale, length: Date, ) -> Result<Self, DateTimeError>
A version of Self::try_new_with_length
that uses custom data provided by a BufferProvider
.
โจ Enabled with the serde
feature.
Sourcepub fn try_new_with_length_unstable<P>(
provider: &P,
locale: &DataLocale,
length: Date,
) -> Result<Self, DateTimeError>where
P: DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<WeekDataV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<BuddhistDateLengthsV1Marker> + DataProvider<BuddhistDateSymbolsV1Marker> + DataProvider<ChineseCacheV1Marker> + DataProvider<ChineseDateLengthsV1Marker> + DataProvider<ChineseDateSymbolsV1Marker> + DataProvider<CopticDateLengthsV1Marker> + DataProvider<CopticDateSymbolsV1Marker> + DataProvider<DangiCacheV1Marker> + DataProvider<DangiDateLengthsV1Marker> + DataProvider<DangiDateSymbolsV1Marker> + DataProvider<EthiopianDateLengthsV1Marker> + DataProvider<EthiopianDateSymbolsV1Marker> + DataProvider<GregorianDateLengthsV1Marker> + DataProvider<GregorianDateSymbolsV1Marker> + DataProvider<HebrewDateLengthsV1Marker> + DataProvider<HebrewDateSymbolsV1Marker> + DataProvider<IndianDateLengthsV1Marker> + DataProvider<IndianDateSymbolsV1Marker> + DataProvider<IslamicDateLengthsV1Marker> + DataProvider<IslamicDateSymbolsV1Marker> + DataProvider<IslamicObservationalCacheV1Marker> + DataProvider<IslamicUmmAlQuraCacheV1Marker> + DataProvider<JapaneseDateLengthsV1Marker> + DataProvider<JapaneseDateSymbolsV1Marker> + DataProvider<JapaneseErasV1Marker> + DataProvider<JapaneseExtendedDateLengthsV1Marker> + DataProvider<JapaneseExtendedDateSymbolsV1Marker> + DataProvider<JapaneseExtendedErasV1Marker> + DataProvider<PersianDateLengthsV1Marker> + DataProvider<PersianDateSymbolsV1Marker> + DataProvider<RocDateLengthsV1Marker> + DataProvider<RocDateSymbolsV1Marker> + ?Sized,
pub fn try_new_with_length_unstable<P>(
provider: &P,
locale: &DataLocale,
length: Date,
) -> Result<Self, DateTimeError>where
P: DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<WeekDataV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<BuddhistDateLengthsV1Marker> + DataProvider<BuddhistDateSymbolsV1Marker> + DataProvider<ChineseCacheV1Marker> + DataProvider<ChineseDateLengthsV1Marker> + DataProvider<ChineseDateSymbolsV1Marker> + DataProvider<CopticDateLengthsV1Marker> + DataProvider<CopticDateSymbolsV1Marker> + DataProvider<DangiCacheV1Marker> + DataProvider<DangiDateLengthsV1Marker> + DataProvider<DangiDateSymbolsV1Marker> + DataProvider<EthiopianDateLengthsV1Marker> + DataProvider<EthiopianDateSymbolsV1Marker> + DataProvider<GregorianDateLengthsV1Marker> + DataProvider<GregorianDateSymbolsV1Marker> + DataProvider<HebrewDateLengthsV1Marker> + DataProvider<HebrewDateSymbolsV1Marker> + DataProvider<IndianDateLengthsV1Marker> + DataProvider<IndianDateSymbolsV1Marker> + DataProvider<IslamicDateLengthsV1Marker> + DataProvider<IslamicDateSymbolsV1Marker> + DataProvider<IslamicObservationalCacheV1Marker> + DataProvider<IslamicUmmAlQuraCacheV1Marker> + DataProvider<JapaneseDateLengthsV1Marker> + DataProvider<JapaneseDateSymbolsV1Marker> + DataProvider<JapaneseErasV1Marker> + DataProvider<JapaneseExtendedDateLengthsV1Marker> + DataProvider<JapaneseExtendedDateSymbolsV1Marker> + DataProvider<JapaneseExtendedErasV1Marker> + DataProvider<PersianDateLengthsV1Marker> + DataProvider<PersianDateSymbolsV1Marker> + DataProvider<RocDateLengthsV1Marker> + DataProvider<RocDateSymbolsV1Marker> + ?Sized,
A version of Self::try_new_with_length
that uses custom data provided by a DataProvider
.
๐ Help choosing a constructor
Sourcepub fn format<'l, T>(
&'l self,
value: &T,
) -> Result<FormattedDateTime<'l>, DateTimeError>where
T: DateInput<Calendar = AnyCalendar>,
pub fn format<'l, T>(
&'l self,
value: &T,
) -> Result<FormattedDateTime<'l>, DateTimeError>where
T: DateInput<Calendar = AnyCalendar>,
Takes a DateInput
implementer and returns an instance of a FormattedDateTime
that contains all information necessary to display a formatted date and operate on it.
This function will fail if the date passed in uses a different calendar than that of the AnyCalendar. Please convert dates before passing them in if necessary. This function will automatically convert and format dates that are associated with the ISO calendar.
Sourcepub fn format_to_string(
&self,
value: &impl DateInput<Calendar = AnyCalendar>,
) -> Result<String, DateTimeError>
pub fn format_to_string( &self, value: &impl DateInput<Calendar = AnyCalendar>, ) -> Result<String, DateTimeError>
Takes a DateInput
implementer and returns it formatted as a string.
This function will fail if the date passed in uses a different calendar than that of the AnyCalendar. Please convert dates before passing them in if necessary. This function will automatically convert and format dates that are associated with the ISO calendar.
Trait Implementationsยง
Auto Trait Implementationsยง
impl Freeze for DateFormatter
impl RefUnwindSafe for DateFormatter
impl !Send for DateFormatter
impl !Sync for DateFormatter
impl Unpin for DateFormatter
impl UnwindSafe for DateFormatter
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