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

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

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

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

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.

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

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.