Struct icu_datetime::TypedDateFormatter
source · [−]pub struct TypedDateFormatter<C>(_, _);
Expand description
TypedDateFormatter
is a formatter capable of formatting
dates from a calendar selected at compile time. For the difference between this
and DateFormatter
, 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 TypedDateFormatter
, and then fast formatting of DateInput
data using the instance.
Examples
use icu::calendar::{Date, Gregorian};
use icu::datetime::{options::length, TypedDateFormatter};
use icu::locid::locale;
let provider = icu_testdata::get_provider();
let df = TypedDateFormatter::<Gregorian>::try_new_with_buffer_provider(&provider, &locale!("en").into(), length::Date::Full)
.expect("Failed to create TypedDateFormatter instance.");
let date = Date::new_gregorian_date(2020, 9, 1)
.expect("Failed to construct Date.");
assert_eq!(df.format_to_string(&date), "Tuesday, September 1, 2020");
This model replicates that of ICU
and ECMA402
.
Implementations
sourceimpl<C: CldrCalendar> TypedDateFormatter<C>
impl<C: CldrCalendar> TypedDateFormatter<C>
sourcepub fn try_new_unstable<D>(
data_provider: &D,
locale: &DataLocale,
length: Date
) -> Result<Self, DateTimeFormatterError> where
D: DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker> + DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<WeekDataV1Marker> + ?Sized,
pub fn try_new_unstable<D>(
data_provider: &D,
locale: &DataLocale,
length: Date
) -> Result<Self, DateTimeFormatterError> where
D: DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker> + DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<WeekDataV1Marker> + ?Sized,
Constructor that takes a selected locale, reference to a data provider and a list of options, then collects all data necessary to format date and time values into the given locale.
Examples
use icu::calendar::Gregorian;
use icu::datetime::{options::length, TypedDateFormatter};
use icu::locid::locale;
let provider = icu_testdata::get_provider();
TypedDateFormatter::<Gregorian>::try_new_with_buffer_provider(&provider, &locale!("en").into(), length::Date::Full)
.unwrap();
sourcepub fn try_new_with_any_provider(
provider: &impl AnyProvider + ?Sized,
locale: &DataLocale,
length: Date
) -> Result<Self, DateTimeFormatterError>
pub fn try_new_with_any_provider(
provider: &impl AnyProvider + ?Sized,
locale: &DataLocale,
length: Date
) -> Result<Self, DateTimeFormatterError>
Create a new instance using an AnyProvider
.
See also: Self::try_new_unstable
sourcepub fn try_new_with_buffer_provider(
provider: &impl BufferProvider + ?Sized,
locale: &DataLocale,
length: Date
) -> Result<Self, DateTimeFormatterError>
pub fn try_new_with_buffer_provider(
provider: &impl BufferProvider + ?Sized,
locale: &DataLocale,
length: Date
) -> Result<Self, DateTimeFormatterError>
Create a new instance using a BufferProvider
. Enabled with the "serde"
feature.
See also: Self::try_new_unstable
sourcepub fn format<'l, T>(&'l self, value: &T) -> FormattedDateTime<'l> where
T: DateInput<Calendar = C>,
pub fn format<'l, T>(&'l self, value: &T) -> FormattedDateTime<'l> where
T: DateInput<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::{Date, Gregorian};
use icu::datetime::{options::length, TypedDateFormatter};
use writeable::assert_writeable_eq;
let df = TypedDateFormatter::<Gregorian>::try_new_with_buffer_provider(&provider, &locale.into(), length::Date::Full)
.expect("Failed to create TypedDateFormatter instance.");
let date = Date::new_gregorian_date(2020, 9, 1)
.expect("Failed to construct Date.");
assert_writeable_eq!(df.format(&date), "Tuesday, September 1, 2020");
At the moment, there’s little value in using that over one of the other format
methods,
but FormattedDateTime
will grow with methods for iterating over fields, extracting information
about formatted date and so on.
sourcepub fn format_to_write(
&self,
w: &mut impl Write,
value: &impl DateInput<Calendar = C>
) -> Result
pub fn format_to_write(
&self,
w: &mut impl Write,
value: &impl DateInput<Calendar = C>
) -> Result
Takes a mutable reference to anything that implements Write
trait
and a DateTimeInput
implementer and populates the buffer with a formatted value.
Examples
use icu::calendar::{Date, Gregorian};
use icu::datetime::{options::length, TypedDateFormatter};
let df = TypedDateFormatter::<Gregorian>::try_new_with_buffer_provider(&provider, &locale.into(), length::Date::Short)
.expect("Failed to create TypedDateFormatter instance.");
let date = Date::new_gregorian_date(2020, 9, 1)
.expect("Failed to construct Date.");
let mut buffer = String::new();
df.format_to_write(&mut buffer, &date)
.expect("Failed to write to a buffer.");
assert_eq!(buffer, "9/1/20");
sourcepub fn format_to_string(&self, value: &impl DateInput<Calendar = C>) -> String
pub fn format_to_string(&self, value: &impl DateInput<Calendar = C>) -> String
Takes a DateTimeInput
implementer and returns it formatted as a string.
Examples
use icu::calendar::{Date, Gregorian};
use icu::datetime::{options::length, TypedDateFormatter};
let df = TypedDateFormatter::<Gregorian>::try_new_with_buffer_provider(&provider, &locale.into(), length::Date::Short)
.expect("Failed to create TypedDateTimeFormatter instance.");
let date = Date::new_gregorian_date(2020, 9, 1)
.expect("Failed to construct Date.");
assert_eq!(df.format_to_string(&date), "9/1/20");
Auto Trait Implementations
impl<C> RefUnwindSafe for TypedDateFormatter<C> where
C: RefUnwindSafe,
impl<C> Send for TypedDateFormatter<C> where
C: Send,
impl<C> Sync for TypedDateFormatter<C> where
C: Sync,
impl<C> Unpin for TypedDateFormatter<C> where
C: Unpin,
impl<C> UnwindSafe for TypedDateFormatter<C> where
C: UnwindSafe,
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