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

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

Create a new instance using an AnyProvider.

See also: Self::try_new_unstable

Create a new instance using a BufferProvider. Enabled with the "serde" feature.

See also: Self::try_new_unstable

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.

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

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.