Struct icu::datetime::DateFormatter
source · [−]pub struct DateFormatter(_, _);
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.
Examples
use icu::calendar::{any_calendar::AnyCalendar, Date, Gregorian};
use icu::datetime::{options::length, DateFormatter};
use icu::locid::locale;
use std::str::FromStr;
use writeable::assert_writeable_eq;
let length = length::Date::Medium;
let df = DateFormatter::try_new_with_length_unstable(
&icu_testdata::unstable(),
&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
sourceimpl DateFormatter
impl DateFormatter
sourcepub fn try_new_with_length_with_any_provider<P>(
data_provider: &P,
locale: &DataLocale,
length: Date
) -> Result<DateFormatter, DateTimeError> where
P: AnyProvider,
pub fn try_new_with_length_with_any_provider<P>(
data_provider: &P,
locale: &DataLocale,
length: Date
) -> Result<DateFormatter, DateTimeError> where
P: AnyProvider,
Construct a new DateFormatter
from a data provider that implements
AnyProvider
.
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.
The provider must be able to provide data for the following keys: datetime/symbols@1
, datetime/timelengths@1
,
datetime/timelengths@1
, datetime/symbols@1
, datetime/skeletons@1
, datetime/week_data@1
, and plurals/ordinals@1
.
Furthermore, based on the type of calendar used, one of the following data keys may be necessary:
u-ca-japanese
(Japanese calendar):calendar/japanese@1
sourcepub fn try_new_with_length_with_buffer_provider<P>(
data_provider: &P,
locale: &DataLocale,
length: Date
) -> Result<DateFormatter, DateTimeError> where
P: BufferProvider,
pub fn try_new_with_length_with_buffer_provider<P>(
data_provider: &P,
locale: &DataLocale,
length: Date
) -> Result<DateFormatter, DateTimeError> where
P: BufferProvider,
Construct a new DateFormatter
from a data provider that implements
BufferProvider
.
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.
The provider must be able to provide data for the following keys: datetime/symbols@1
, datetime/datelengths@1
,
datetime/timelengths@1
, datetime/symbols@1
, datetime/skeletons@1
, datetime/week_data@1
, and plurals/ordinals@1
.
Furthermore, based on the type of calendar used, one of the following data keys may be necessary:
u-ca-japanese
(Japanese calendar):calendar/japanese@1
Examples
use icu::calendar::{any_calendar::AnyCalendar, Date, Gregorian};
use icu::datetime::{options::length, DateFormatter};
use icu::locid::locale;
use icu_provider::any::DynamicDataProviderAnyMarkerWrap;
use std::str::FromStr;
use writeable::assert_writeable_eq;
let length = length::Date::Medium;
let locale = locale!("en-u-ca-gregory");
let df = DateFormatter::try_new_with_length_with_buffer_provider(
&icu_testdata::buffer(),
&locale.into(),
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_unstable<P>(
data_provider: &P,
locale: &DataLocale,
length: Date
) -> Result<DateFormatter, DateTimeError> where
P: DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<WeekDataV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<GregorianDateLengthsV1Marker> + DataProvider<BuddhistDateLengthsV1Marker> + DataProvider<JapaneseDateLengthsV1Marker> + DataProvider<JapaneseExtendedDateLengthsV1Marker> + DataProvider<CopticDateLengthsV1Marker> + DataProvider<IndianDateLengthsV1Marker> + DataProvider<EthiopianDateLengthsV1Marker> + DataProvider<GregorianDateSymbolsV1Marker> + DataProvider<BuddhistDateSymbolsV1Marker> + DataProvider<JapaneseDateSymbolsV1Marker> + DataProvider<JapaneseExtendedDateSymbolsV1Marker> + DataProvider<CopticDateSymbolsV1Marker> + DataProvider<IndianDateSymbolsV1Marker> + DataProvider<EthiopianDateSymbolsV1Marker> + DataProvider<JapaneseErasV1Marker> + DataProvider<JapaneseExtendedErasV1Marker> + ?Sized,
pub fn try_new_with_length_unstable<P>(
data_provider: &P,
locale: &DataLocale,
length: Date
) -> Result<DateFormatter, DateTimeError> where
P: DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<WeekDataV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<GregorianDateLengthsV1Marker> + DataProvider<BuddhistDateLengthsV1Marker> + DataProvider<JapaneseDateLengthsV1Marker> + DataProvider<JapaneseExtendedDateLengthsV1Marker> + DataProvider<CopticDateLengthsV1Marker> + DataProvider<IndianDateLengthsV1Marker> + DataProvider<EthiopianDateLengthsV1Marker> + DataProvider<GregorianDateSymbolsV1Marker> + DataProvider<BuddhistDateSymbolsV1Marker> + DataProvider<JapaneseDateSymbolsV1Marker> + DataProvider<JapaneseExtendedDateSymbolsV1Marker> + DataProvider<CopticDateSymbolsV1Marker> + DataProvider<IndianDateSymbolsV1Marker> + DataProvider<EthiopianDateSymbolsV1Marker> + DataProvider<JapaneseErasV1Marker> + DataProvider<JapaneseExtendedErasV1Marker> + ?Sized,
Construct a new DateFormatter
from a data provider that can provide all of the requested data.
This method is unstable, more bounds may be added in the future as calendar support is added. It is
preferable to use a provider that implements DataProvider<D>
for all D
, and ensure data is loaded as
appropriate. The Self::try_new_with_length_with_buffer_provider()
, Self::try_new_with_length_with_any_provider()
constructors may also be used if compile stability is desired.
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.
Examples
use icu::calendar::{any_calendar::AnyCalendar, Date, Gregorian};
use icu::datetime::{options::length, DateFormatter};
use icu::locid::locale;
use icu_provider::any::DynamicDataProviderAnyMarkerWrap;
use std::str::FromStr;
use writeable::assert_writeable_eq;
let length = length::Date::Medium;
let locale = locale!("en-u-ca-gregory");
let df = DateFormatter::try_new_with_length_unstable(
&icu_testdata::unstable(),
&locale.into(),
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 format<T>(
&'l self,
value: &T
) -> Result<FormattedDateTime<'l>, DateTimeError> where
T: DateInput<Calendar = AnyCalendar>,
pub fn format<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.
Auto Trait Implementations
impl RefUnwindSafe for DateFormatter
impl Send for DateFormatter
impl Sync for DateFormatter
impl Unpin for DateFormatter
impl UnwindSafe for DateFormatter
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more