Struct DateTimeNames

Source
pub struct DateTimeNames<FSet>
where 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> DateTimeNames<FSet>
where FSet: DateTimeNamesMarker,

Source

pub fn try_new_with_calendar_without_number_formatting( prefs: DateTimeFormatterPreferences, calendar: AnyCalendar, ) -> Result<DateTimeNames<FSet>, UnsupportedCalendarError>

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

Source

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

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(YMD::long().with_time_hm()).unwrap();

assert_writeable_eq!(
    formatter.format(&DateTime {
        date: Date::try_new_iso(2025, 2, 13).unwrap(),
        time: Time::start_of_day(),
    }),
    "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, DateTimeNames<FSet>)>

Loads a pattern for the given field set with compiled data 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.

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

§Examples
use icu::calendar::{AnyCalendar, AnyCalendarKind};
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 kind = AnyCalendarKind::new(locale!("es-MX").into());
let calendar = AnyCalendar::new(kind);

let names = DateTimeNames::try_new_with_calendar_without_number_formatting(
    locale!("es-MX").into(),
    calendar,
)
.expect("All locale-default calendars are supported");

let field_set = T::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::start_of_day()), "12:00 a.m.");
Source

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

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, DateTimeNames<FSet>)>
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> DateTimeNames<FSet>
where FSet: DateTimeNamesMarker,

Source

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

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> AsMut<FixedCalendarDateTimeNames<(), FSet>> for DateTimeNames<FSet>
where FSet: DateTimeNamesMarker,

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> AsRef<FixedCalendarDateTimeNames<(), FSet>> for DateTimeNames<FSet>
where FSet: DateTimeNamesMarker,

Source§

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

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

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

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 for DateTimeNames<FSet>
where FSet: Debug + DateTimeNamesMarker,

Source§

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

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<DatetimeNamesWeekdayV1, WeekdayNameLength>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DatetimeNamesDayperiodV1, DayPeriodNameLength>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimezoneNamesEssentialsV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<TimezoneNamesLocationsRootV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<TimezoneNamesLocationsOverrideV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<TimezoneNamesCitiesRootV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<TimezoneNamesCitiesOverrideV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<TimezoneNamesGenericLongV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<TimezoneNamesGenericShortV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<TimezoneNamesStandardLongV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<TimezoneNamesSpecificLongV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<TimezoneNamesSpecificShortV1, ()>>::Container: Freeze, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<TimezoneMetazonePeriodsV1, ()>>::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<DatetimeNamesWeekdayV1, WeekdayNameLength>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DatetimeNamesDayperiodV1, DayPeriodNameLength>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimezoneNamesEssentialsV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<TimezoneNamesLocationsRootV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<TimezoneNamesLocationsOverrideV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<TimezoneNamesCitiesRootV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<TimezoneNamesCitiesOverrideV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<TimezoneNamesGenericLongV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<TimezoneNamesGenericShortV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<TimezoneNamesStandardLongV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<TimezoneNamesSpecificLongV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<TimezoneNamesSpecificShortV1, ()>>::Container: RefUnwindSafe, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<TimezoneMetazonePeriodsV1, ()>>::Container: RefUnwindSafe, FSet: RefUnwindSafe,

§

impl<FSet> !Send for DateTimeNames<FSet>

§

impl<FSet> !Sync for DateTimeNames<FSet>

§

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<DatetimeNamesWeekdayV1, WeekdayNameLength>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DatetimeNamesDayperiodV1, DayPeriodNameLength>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimezoneNamesEssentialsV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<TimezoneNamesLocationsRootV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<TimezoneNamesLocationsOverrideV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<TimezoneNamesCitiesRootV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<TimezoneNamesCitiesOverrideV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<TimezoneNamesGenericLongV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<TimezoneNamesGenericShortV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<TimezoneNamesStandardLongV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<TimezoneNamesSpecificLongV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<TimezoneNamesSpecificShortV1, ()>>::Container: Unpin, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<TimezoneMetazonePeriodsV1, ()>>::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<DatetimeNamesWeekdayV1, WeekdayNameLength>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::DayPeriodNames as NamesContainer<DatetimeNamesDayperiodV1, DayPeriodNameLength>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneEssentials as NamesContainer<TimezoneNamesEssentialsV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneLocationsRoot as NamesContainer<TimezoneNamesLocationsRootV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneLocations as NamesContainer<TimezoneNamesLocationsOverrideV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneExemplarsRoot as NamesContainer<TimezoneNamesCitiesRootV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneExemplars as NamesContainer<TimezoneNamesCitiesOverrideV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneGenericLong as NamesContainer<TimezoneNamesGenericLongV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneGenericShort as NamesContainer<TimezoneNamesGenericShortV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneStandardLong as NamesContainer<TimezoneNamesStandardLongV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneSpecificLong as NamesContainer<TimezoneNamesSpecificLongV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::ZoneSpecificShort as NamesContainer<TimezoneNamesSpecificShortV1, ()>>::Container: UnwindSafe, <<FSet as DateTimeNamesMarker>::MetazoneLookup as NamesContainer<TimezoneMetazonePeriodsV1, ()>>::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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.
Source§

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