icu_datetime::pattern

Struct DateTimeNames

Source
pub struct DateTimeNames<FSet: DateTimeNamesMarker> { /* private fields */ }
Expand description

A low-level type that formats datetime patterns with localized names. The calendar is chosen in the constructor at runtime.

Currently this only supports loading of non-calendar-specific names, but additional functions may be added in the future. If you need this, see https://github.com/unicode-org/icu4x/issues/6107

Implementations§

Source§

impl<FSet: DateTimeNamesMarker> DateTimeNames<FSet>

Source

pub fn new_without_number_formatting( prefs: DateTimeFormatterPreferences, calendar: AnyCalendar, ) -> Self

Creates a completely empty instance, not even with number formatting.

Source

pub fn from_formatter( prefs: DateTimeFormatterPreferences, formatter: DateTimeFormatter<FSet>, ) -> Self

Creates an instance with the names and calendar loaded in a DateTimeFormatter.

This function requires passing in the DateTimeFormatterPreferences because it is not retained in the formatter. Pass the same value or else unexpected behavior may occur.

§Examples
use icu::datetime::input::Date;
use icu::datetime::input::{DateTime, Time};
use icu::datetime::DateTimeFormatter;
use icu::datetime::fieldsets::{YMD, YMDT};
use icu::datetime::pattern::{DateTimeNames, DayPeriodNameLength};
use icu::locale::locale;
use writeable::assert_writeable_eq;

let prefs = locale!("es-MX").into();

let formatter =
    DateTimeFormatter::try_new(
        prefs,
        YMD::long(),
    )
    .unwrap();

assert_writeable_eq!(
    formatter.format(&Date::try_new_iso(2025, 2, 13).unwrap()),
    "13 de febrero de 2025"
);

// Change the YMD formatter to a YMDT formatter, after loading day period names.
// This assumes that the locale uses Abbreviated names for the given semantic skeleton!
let mut names = DateTimeNames::from_formatter(prefs, formatter).cast_into_fset::<YMDT>();
names.as_mut().include_day_period_names(DayPeriodNameLength::Abbreviated).unwrap();
let formatter = names.try_into_formatter(YMDT::long().hm()).unwrap();

assert_writeable_eq!(
    formatter.format(&DateTime {
        date: Date::try_new_iso(2025, 2, 13).unwrap(),
        time: Time::midnight(),
    }),
    "13 de febrero de 2025, 12:00 a.m."
);
Source§

impl<FSet> DateTimeNames<FSet>

Source

pub fn try_into_formatter( self, field_set: FSet, ) -> Result<DateTimeFormatter<FSet>, (DateTimeFormatterLoadError, Self)>

Loads a pattern for the given field set and returns a DateTimeFormatter.

The names in the current DateTimeNames must be sufficient for the field set. If not, the input object will be returned with an error.

§Examples
use icu::calendar::AnyCalendar;
use icu::datetime::fieldsets::T;
use icu::datetime::input::Time;
use icu::datetime::pattern::{DateTimeNames, DayPeriodNameLength};
use icu::locale::locale;
use writeable::assert_writeable_eq;

let names = DateTimeNames::new_without_number_formatting(
    locale!("es-MX").into(),
    AnyCalendar::try_new(locale!("es-MX").into()).unwrap(),
);

let field_set = T::long().hm();

// Cannot convert yet: no names are loaded
let mut names = names.try_into_formatter(field_set).unwrap_err().1;

// Load the data we need:
names
    .as_mut()
    .include_day_period_names(DayPeriodNameLength::Abbreviated)
    .unwrap();
names.as_mut().include_decimal_formatter().unwrap();

// Now the conversion is successful:
let formatter = names.try_into_formatter(field_set).unwrap();

assert_writeable_eq!(formatter.format(&Time::midnight()), "12:00 a.m.");
Source

pub fn try_into_formatter_unstable<P>( self, provider: &P, field_set: FSet, ) -> Result<DateTimeFormatter<FSet>, (DateTimeFormatterLoadError, Self)>

A version of Self::try_into_formatter that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
Source

pub fn try_into_formatter_with_buffer_provider<P>( self, provider: &P, field_set: FSet, ) -> Result<DateTimeFormatter<FSet>, (DateTimeFormatterLoadError, Self)>
where P: BufferProvider + ?Sized,

A version of Self::try_into_formatter that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

Source§

impl<FSet: DateTimeNamesMarker> DateTimeNames<FSet>

Source

pub fn cast_into_fset<FSet2: DateTimeNamesFrom<FSet>>( self, ) -> DateTimeNames<FSet2>

Maps a FixedCalendarDateTimeNames of a specific FSet to a more general FSet.

For example, this can transform a formatter for DateFieldSet to one for CompositeDateTimeFieldSet.

Trait Implementations§

Source§

impl<FSet: DateTimeNamesMarker> AsMut<FixedCalendarDateTimeNames<(), FSet>> for DateTimeNames<FSet>

Source§

fn as_mut(&mut self) -> &mut FixedCalendarDateTimeNames<(), FSet>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<FSet: DateTimeNamesMarker> AsRef<FixedCalendarDateTimeNames<(), FSet>> for DateTimeNames<FSet>

Source§

fn as_ref(&self) -> &FixedCalendarDateTimeNames<(), FSet>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<FSet: Clone + DateTimeNamesMarker> Clone for DateTimeNames<FSet>

Source§

fn clone(&self) -> DateTimeNames<FSet>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<FSet: Debug + DateTimeNamesMarker> Debug for DateTimeNames<FSet>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<FSet> Freeze for DateTimeNames<FSet>
where <<FSet as DateTimeNamesMarker>::YearNames as NamesContainer<YearNamesV1, YearNameLength>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::MonthNames as NamesContainer<MonthNamesV1, MonthNameLength>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::WeekdayNames as NamesContainer<WeekdayNamesV1, WeekdayNameLength>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DayPeriodNamesV1, DayPeriodNameLength>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimeZoneEssentialsV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<LocationsRootV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<LocationsV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<ExemplarCitiesRootV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<ExemplarCitiesV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<MetazoneGenericNamesLongV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<MetazoneGenericNamesShortV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<MetazoneStandardNamesLongV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<MetazoneSpecificNamesLongV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<MetazoneSpecificNamesShortV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<MetazonePeriodV1, ()>>::Container: Freeze,

§

impl<FSet> RefUnwindSafe for DateTimeNames<FSet>
where <<FSet as DateTimeNamesMarker>::YearNames as NamesContainer<YearNamesV1, YearNameLength>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::MonthNames as NamesContainer<MonthNamesV1, MonthNameLength>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::WeekdayNames as NamesContainer<WeekdayNamesV1, WeekdayNameLength>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DayPeriodNamesV1, DayPeriodNameLength>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimeZoneEssentialsV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<LocationsRootV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<LocationsV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<ExemplarCitiesRootV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<ExemplarCitiesV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<MetazoneGenericNamesLongV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<MetazoneGenericNamesShortV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<MetazoneStandardNamesLongV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<MetazoneSpecificNamesLongV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<MetazoneSpecificNamesShortV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<MetazonePeriodV1, ()>>::Container: RefUnwindSafe, FSet: RefUnwindSafe,

§

impl<FSet> Send for DateTimeNames<FSet>
where <<FSet as DateTimeNamesMarker>::YearNames as NamesContainer<YearNamesV1, YearNameLength>>::Container: Send, <<FSet as DateTimeNamesMarker>::MonthNames as NamesContainer<MonthNamesV1, MonthNameLength>>::Container: Send, <<FSet as DateTimeNamesMarker>::WeekdayNames as NamesContainer<WeekdayNamesV1, WeekdayNameLength>>::Container: Send, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DayPeriodNamesV1, DayPeriodNameLength>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimeZoneEssentialsV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<LocationsRootV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<LocationsV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<ExemplarCitiesRootV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<ExemplarCitiesV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<MetazoneGenericNamesLongV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<MetazoneGenericNamesShortV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<MetazoneStandardNamesLongV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<MetazoneSpecificNamesLongV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<MetazoneSpecificNamesShortV1, ()>>::Container: Send, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<MetazonePeriodV1, ()>>::Container: Send, FSet: Send,

§

impl<FSet> Sync for DateTimeNames<FSet>
where <<FSet as DateTimeNamesMarker>::YearNames as NamesContainer<YearNamesV1, YearNameLength>>::Container: Sync, <<FSet as DateTimeNamesMarker>::MonthNames as NamesContainer<MonthNamesV1, MonthNameLength>>::Container: Sync, <<FSet as DateTimeNamesMarker>::WeekdayNames as NamesContainer<WeekdayNamesV1, WeekdayNameLength>>::Container: Sync, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DayPeriodNamesV1, DayPeriodNameLength>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimeZoneEssentialsV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<LocationsRootV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<LocationsV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<ExemplarCitiesRootV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<ExemplarCitiesV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<MetazoneGenericNamesLongV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<MetazoneGenericNamesShortV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<MetazoneStandardNamesLongV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<MetazoneSpecificNamesLongV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<MetazoneSpecificNamesShortV1, ()>>::Container: Sync, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<MetazonePeriodV1, ()>>::Container: Sync, FSet: Sync,

§

impl<FSet> Unpin for DateTimeNames<FSet>
where <<FSet as DateTimeNamesMarker>::YearNames as NamesContainer<YearNamesV1, YearNameLength>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::MonthNames as NamesContainer<MonthNamesV1, MonthNameLength>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::WeekdayNames as NamesContainer<WeekdayNamesV1, WeekdayNameLength>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DayPeriodNamesV1, DayPeriodNameLength>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimeZoneEssentialsV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<LocationsRootV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<LocationsV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<ExemplarCitiesRootV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<ExemplarCitiesV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<MetazoneGenericNamesLongV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<MetazoneGenericNamesShortV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<MetazoneStandardNamesLongV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<MetazoneSpecificNamesLongV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<MetazoneSpecificNamesShortV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<MetazonePeriodV1, ()>>::Container: Unpin, FSet: Unpin,

§

impl<FSet> UnwindSafe for DateTimeNames<FSet>
where <<FSet as DateTimeNamesMarker>::YearNames as NamesContainer<YearNamesV1, YearNameLength>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::MonthNames as NamesContainer<MonthNamesV1, MonthNameLength>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::WeekdayNames as NamesContainer<WeekdayNamesV1, WeekdayNameLength>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DayPeriodNamesV1, DayPeriodNameLength>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimeZoneEssentialsV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<LocationsRootV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<LocationsV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<ExemplarCitiesRootV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<ExemplarCitiesV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<MetazoneGenericNamesLongV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<MetazoneGenericNamesShortV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<MetazoneStandardNamesLongV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<MetazoneSpecificNamesLongV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<MetazoneSpecificNamesShortV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<MetazonePeriodV1, ()>>::Container: UnwindSafe, FSet: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for T
where T: 'static,