Struct icu_datetime::TypedDateTimeNames
source ยท pub struct TypedDateTimeNames<C: CldrCalendar, R: DateTimeNamesMarker = NeoDateTimeComponents> { /* private fields */ }
Expand description
A low-level type that formats datetime patterns with localized symbols. The calendar should be chosen at compile time. ๐ This item has a stack size of 464 bytes on the stable toolchain at release date.
Type parameters:
- The calendar chosen at compile time for additional type safety
- A components object type containing the fields that might be formatted
By default, the components object is set to NeoDateTimeComponents
,
meaning that dates and times, but not time zones, are supported. A smaller
components object results in smaller stack size.
To support all fields including time zones, use NeoComponents
.
ยงExamples
use icu::calendar::Gregorian;
use icu::calendar::DateTime;
use icu::datetime::TypedDateTimeNames;
use icu::datetime::fields::FieldLength;
use icu::datetime::fields;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::locale::locale;
use writeable::assert_try_writeable_eq;
// Create an instance that can format abbreviated month, weekday, and day period names:
let mut names: TypedDateTimeNames<Gregorian> =
TypedDateTimeNames::try_new(&locale!("uk").into()).unwrap();
names
.include_month_names(fields::Month::Format, FieldLength::Abbreviated)
.unwrap()
.include_weekday_names(fields::Weekday::Format, FieldLength::Abbreviated)
.unwrap()
.include_day_period_names(FieldLength::Abbreviated)
.unwrap();
// Create a pattern from a pattern string (note: K is the hour with h11 hour cycle):
let pattern_str = "E MMM d y -- K:mm a";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
// Test it:
let datetime = DateTime::try_new_gregorian_datetime(2023, 11, 20, 12, 35, 3).unwrap();
assert_try_writeable_eq!(names.with_pattern(&pattern).format(&datetime), "ะฟะฝ ะปะธัั. 20 2023 -- 0:35 ะฟะฟ");
If the correct data is not loaded, and error will occur:
use icu::calendar::Gregorian;
use icu::calendar::{Date, Time};
use icu::datetime::{DateTimeWriteError, TypedDateTimeNames};
use icu::datetime::fields::{Field, FieldLength, FieldSymbol, Weekday};
use icu::datetime::neo_pattern::DateTimePattern;
use icu::datetime::neo_skeleton::NeoComponents;
use icu::locale::locale;
use icu::timezone::{CustomTimeZone, CustomZonedDateTime};
use writeable::{Part, assert_try_writeable_parts_eq};
// Create an instance that can format all fields (NeoComponents):
let mut names: TypedDateTimeNames<Gregorian, NeoComponents> =
TypedDateTimeNames::try_new(&locale!("en").into()).unwrap();
// Create a pattern from a pattern string:
let pattern_str = "'It is:' E MMM d y G 'at' h:mm:ssSSS a zzzz";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
// The pattern string contains lots of symbols including "E", "MMM", and "a",
// but we did not load any data!
let mut dtz = CustomZonedDateTime::try_from_str("2023-11-20T11:35:03+00:00[Europe/London]").unwrap().to_calendar(Gregorian);
// Missing data is filled in on a best-effort basis, and an error is signaled.
assert_try_writeable_parts_eq!(
names.with_pattern(&pattern).format(&dtz),
"It is: mon M11 20 2023 ce at 11:35:03.000 AM +0000",
Err(DateTimeWriteError::MissingNames(Field { symbol: FieldSymbol::Weekday(Weekday::Format), length: FieldLength::One })),
[
(7, 10, Part::ERROR), // mon
(11, 14, Part::ERROR), // M11
(23, 25, Part::ERROR), // ce
(42, 44, Part::ERROR), // AM
(45, 50, Part::ERROR), // +0000
]
);
If the pattern contains fields inconsistent with the receiver, an error will occur:
use icu::calendar::Gregorian;
use icu::calendar::DateTime;
use icu::datetime::{DateTimeWriteError, TypedDateTimeNames};
use icu::datetime::fields::{Field, FieldLength, FieldSymbol, Weekday};
use icu::datetime::neo_pattern::DateTimePattern;
use icu::datetime::neo_skeleton::NeoTimeZoneSkeleton;
use icu::locale::locale;
use icu::timezone::CustomTimeZone;
use writeable::{Part, assert_try_writeable_parts_eq};
// Create an instance that can format abbreviated month, weekday, and day period names:
let mut names: TypedDateTimeNames<Gregorian, NeoTimeZoneSkeleton> =
TypedDateTimeNames::try_new(&locale!("en").into()).unwrap();
// Create a pattern from a pattern string:
let pattern_str = "'It is:' E MMM d y G 'at' h:mm:ssSSS a zzzz";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
// The pattern string contains lots of symbols including "E", "MMM", and "a",
// but the `TypedDateTimeNames` is configured to format only time zones!
// Further, the time zone we provide doesn't contain any offset into!
// Missing data is filled in on a best-effort basis, and an error is signaled.
assert_try_writeable_parts_eq!(
names.with_pattern(&pattern).format(&CustomTimeZone::new_empty()),
"It is: {E} {M} {d} {y} {G} at {h}:{m}:{s} {a} {GMT+?}",
Err(DateTimeWriteError::MissingInputField("iso_weekday")),
[
(7, 10, Part::ERROR), // {E}
(11, 14, Part::ERROR), // {M}
(15, 18, Part::ERROR), // {d}
(19, 22, Part::ERROR), // {y}
(23, 26, Part::ERROR), // {G}
(30, 33, Part::ERROR), // {h}
(34, 37, Part::ERROR), // {m}
(38, 41, Part::ERROR), // {s}
(42, 45, Part::ERROR), // {a}
(46, 53, Part::ERROR), // {GMT+?}
]
);
Implementationsยง
sourceยงimpl<C: CldrCalendar, R: DateTimeNamesMarker> TypedDateTimeNames<C, R>
impl<C: CldrCalendar, R: DateTimeNamesMarker> TypedDateTimeNames<C, R>
sourcepub fn try_new(locale: &DataLocale) -> Result<Self, DataError>
pub fn try_new(locale: &DataLocale) -> Result<Self, DataError>
Constructor that takes a selected locale and creates an empty instance.
For an example, see TypedDateTimeNames
.
โจ Enabled with the compiled_data
Cargo feature.
sourcepub fn try_new_unstable<P>(
provider: &P,
locale: &DataLocale,
) -> Result<Self, DataError>where
P: DataProvider<DecimalSymbolsV1Marker> + ?Sized,
pub fn try_new_unstable<P>(
provider: &P,
locale: &DataLocale,
) -> Result<Self, DataError>where
P: DataProvider<DecimalSymbolsV1Marker> + ?Sized,
A version of Self::try_new
that uses custom data provided by a DataProvider
.
๐ Help choosing a constructor
sourcepub fn load_year_names<P>(
&mut self,
provider: &P,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<C::YearNamesV1Marker> + ?Sized,
pub fn load_year_names<P>(
&mut self,
provider: &P,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<C::YearNamesV1Marker> + ?Sized,
Loads year (era or cycle) names for the specified length.
Does not support multiple field symbols or lengths. See #4337
sourcepub fn include_year_names(
&mut self,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>
pub fn include_year_names( &mut self, field_length: FieldLength, ) -> Result<&mut Self, SingleLoadError>
Includes year (era or cycle) names for the specified length.
Does not support multiple field symbols or lengths. See #4337
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::fields::FieldLength;
use icu::datetime::SingleLoadError;
use icu::datetime::TypedDateTimeNames;
use icu::locale::locale;
let mut names =
TypedDateTimeNames::<Gregorian>::try_new(&locale!("und").into())
.unwrap();
// First length is successful:
names.include_year_names(FieldLength::Wide).unwrap();
// Attempting to load the first length a second time will succeed:
names.include_year_names(FieldLength::Wide).unwrap();
// But loading a new length fails:
assert!(matches!(
names.include_year_names(FieldLength::Abbreviated),
Err(SingleLoadError::DuplicateField(_))
));
sourcepub fn load_month_names<P>(
&mut self,
provider: &P,
field_symbol: Month,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<C::MonthNamesV1Marker> + ?Sized,
pub fn load_month_names<P>(
&mut self,
provider: &P,
field_symbol: Month,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<C::MonthNamesV1Marker> + ?Sized,
Loads month names for the specified symbol and length.
Does not support multiple field symbols or lengths. See #4337
sourcepub fn include_month_names(
&mut self,
field_symbol: Month,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>
pub fn include_month_names( &mut self, field_symbol: Month, field_length: FieldLength, ) -> Result<&mut Self, SingleLoadError>
Includes month names for the specified symbol and length.
Does not support multiple field symbols or lengths. See #4337
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::fields::FieldLength;
use icu::datetime::SingleLoadError;
use icu::datetime::TypedDateTimeNames;
use icu::locale::locale;
let mut names =
TypedDateTimeNames::<Gregorian>::try_new(&locale!("und").into())
.unwrap();
let field_symbol = icu::datetime::fields::Month::Format;
let alt_field_symbol = icu::datetime::fields::Month::StandAlone;
// First length is successful:
names
.include_month_names(field_symbol, FieldLength::Wide)
.unwrap();
// Attempting to load the first length a second time will succeed:
names
.include_month_names(field_symbol, FieldLength::Wide)
.unwrap();
// But loading a new symbol or length fails:
assert!(matches!(
names.include_month_names(alt_field_symbol, FieldLength::Wide),
Err(SingleLoadError::DuplicateField(_))
));
assert!(matches!(
names.include_month_names(field_symbol, FieldLength::Abbreviated),
Err(SingleLoadError::DuplicateField(_))
));
sourcepub fn load_day_period_names<P>(
&mut self,
provider: &P,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<DayPeriodNamesV1Marker> + ?Sized,
pub fn load_day_period_names<P>(
&mut self,
provider: &P,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<DayPeriodNamesV1Marker> + ?Sized,
Loads day period names for the specified length.
Does not support multiple field symbols or lengths. See #4337
sourcepub fn include_day_period_names(
&mut self,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<DayPeriodNamesV1Marker>,
pub fn include_day_period_names(
&mut self,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<DayPeriodNamesV1Marker>,
Includes day period names for the specified length.
Does not support multiple field symbols or lengths. See #4337
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::fields::FieldLength;
use icu::datetime::SingleLoadError;
use icu::datetime::TypedDateTimeNames;
use icu::locale::locale;
let mut names =
TypedDateTimeNames::<Gregorian>::try_new(&locale!("und").into())
.unwrap();
// First length is successful:
names.include_day_period_names(FieldLength::Wide).unwrap();
// Attempting to load the first length a second time will succeed:
names.include_day_period_names(FieldLength::Wide).unwrap();
// But loading a new length fails:
assert!(matches!(
names.include_day_period_names(FieldLength::Abbreviated),
Err(SingleLoadError::DuplicateField(_))
));
sourcepub fn load_weekday_names<P>(
&mut self,
provider: &P,
field_symbol: Weekday,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<WeekdayNamesV1Marker> + ?Sized,
pub fn load_weekday_names<P>(
&mut self,
provider: &P,
field_symbol: Weekday,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<WeekdayNamesV1Marker> + ?Sized,
Loads weekday names for the specified symbol and length.
Does not support multiple field symbols or lengths. See #4337
sourcepub fn include_weekday_names(
&mut self,
field_symbol: Weekday,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<WeekdayNamesV1Marker>,
pub fn include_weekday_names(
&mut self,
field_symbol: Weekday,
field_length: FieldLength,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<WeekdayNamesV1Marker>,
Includes weekday names for the specified symbol and length.
Does not support multiple field symbols or lengths. See #4337
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::fields::FieldLength;
use icu::datetime::SingleLoadError;
use icu::datetime::TypedDateTimeNames;
use icu::locale::locale;
let mut names =
TypedDateTimeNames::<Gregorian>::try_new(&locale!("und").into())
.unwrap();
let field_symbol = icu::datetime::fields::Weekday::Format;
let alt_field_symbol = icu::datetime::fields::Weekday::StandAlone;
// First length is successful:
names
.include_weekday_names(field_symbol, FieldLength::Wide)
.unwrap();
// Attempting to load the first length a second time will succeed:
names
.include_weekday_names(field_symbol, FieldLength::Wide)
.unwrap();
// But loading a new symbol or length fails:
assert!(matches!(
names.include_weekday_names(alt_field_symbol, FieldLength::Wide),
Err(SingleLoadError::DuplicateField(_))
));
assert!(matches!(
names.include_weekday_names(field_symbol, FieldLength::Abbreviated),
Err(SingleLoadError::DuplicateField(_))
));
sourcepub fn load_time_zone_essentials<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<TimeZoneFormatsV1Marker> + ?Sized,
pub fn load_time_zone_essentials<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<TimeZoneFormatsV1Marker> + ?Sized,
Loads shared essential patterns for time zone formatting.
sourcepub fn include_time_zone_essentials(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<TimeZoneFormatsV1Marker>,
pub fn include_time_zone_essentials(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<TimeZoneFormatsV1Marker>,
Includes shared essential patterns for time zone formatting.
This data should always be loaded when performing time zone formatting. By itself, it allows localized offset formats.
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::TypedDateTimeNames;
use icu::datetime::neo_skeleton::NeoTimeZoneSkeleton;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::locale::locale;
use icu::timezone::CustomZonedDateTime;
use writeable::assert_try_writeable_eq;
let mut zone_london_winter = CustomZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]").unwrap().zone;
let mut zone_london_summer = CustomZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]").unwrap().zone;
let mut names =
TypedDateTimeNames::<Gregorian, NeoTimeZoneSkeleton>::try_new(&locale!("en-GB").into())
.unwrap();
names
.include_time_zone_essentials()
.unwrap();
// Create a pattern with symbol `OOOO`:
let pattern_str = "'Your time zone is:' OOOO";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: GMT",
);
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_summer),
"Your time zone is: GMT+01:00",
);
// Now try `V`:
let pattern_str = "'Your time zone is:' V";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: gblon",
);
// Now try `Z`:
let pattern_str = "'Your time zone is:' Z";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: +0000",
);
// Now try `ZZZZZ`:
let pattern_str = "'Your time zone is:' ZZZZZ";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: Z",
);
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_summer),
"Your time zone is: +01:00",
);
sourcepub fn load_time_zone_exemplar_city_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<ExemplarCitiesV1Marker> + ?Sized,
pub fn load_time_zone_exemplar_city_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<ExemplarCitiesV1Marker> + ?Sized,
Loads exemplar cities for time zone formatting.
sourcepub fn include_time_zone_exemplar_city_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneGenericNamesShortV1Marker>,
pub fn include_time_zone_exemplar_city_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneGenericNamesShortV1Marker>,
Includes exemplar cities for time zone formatting.
Important: When performing manual time zone data loading, in addition to the specific time zone format data, also call either:
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::TypedDateTimeNames;
use icu::datetime::neo_skeleton::NeoTimeZoneSkeleton;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::locale::locale;
use icu::timezone::CustomZonedDateTime;
use writeable::assert_try_writeable_eq;
let mut zone_london_winter = CustomZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]").unwrap().zone;
let mut names =
TypedDateTimeNames::<Gregorian, NeoTimeZoneSkeleton>::try_new(&locale!("en-GB").into())
.unwrap();
names
.include_time_zone_essentials()
.unwrap();
names
.include_time_zone_exemplar_city_names()
.unwrap();
// Create a pattern with symbol `VVV`:
let pattern_str = "'Your time zone is:' VVV";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: London",
);
// Now try `VVVV`:
let pattern_str = "'Your time zone is:' VVVV";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: London Time",
);
sourcepub fn load_time_zone_generic_long_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<MetazoneGenericNamesLongV1Marker> + ?Sized,
pub fn load_time_zone_generic_long_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<MetazoneGenericNamesLongV1Marker> + ?Sized,
Loads generic non-location long time zone names.
sourcepub fn include_time_zone_generic_long_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneGenericNamesLongV1Marker>,
pub fn include_time_zone_generic_long_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneGenericNamesLongV1Marker>,
Includes generic non-location long time zone names.
Important: When performing manual time zone data loading, in addition to the specific time zone format data, also call either:
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::TypedDateTimeNames;
use icu::datetime::neo_skeleton::NeoTimeZoneSkeleton;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::locale::locale;
use icu::timezone::CustomZonedDateTime;
use writeable::assert_try_writeable_eq;
let mut zone_london_winter = CustomZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]").unwrap().zone;
let mut zone_london_summer = CustomZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]").unwrap().zone;
let mut names =
TypedDateTimeNames::<Gregorian, NeoTimeZoneSkeleton>::try_new(&locale!("en-GB").into())
.unwrap();
names
.include_time_zone_essentials()
.unwrap();
names
.include_time_zone_generic_long_names()
.unwrap();
// Create a pattern with symbol `vvvv`:
let pattern_str = "'Your time zone is:' vvvv";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: Greenwich Mean Time",
);
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_summer),
"Your time zone is: Greenwich Mean Time", // TODO
);
sourcepub fn load_time_zone_generic_short_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<MetazoneGenericNamesShortV1Marker> + ?Sized,
pub fn load_time_zone_generic_short_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<MetazoneGenericNamesShortV1Marker> + ?Sized,
Loads generic non-location short time zone names.
sourcepub fn include_time_zone_generic_short_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneGenericNamesShortV1Marker>,
pub fn include_time_zone_generic_short_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneGenericNamesShortV1Marker>,
Includes generic non-location short time zone names.
Important: When performing manual time zone data loading, in addition to the specific time zone format data, also call either:
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::TypedDateTimeNames;
use icu::datetime::neo_skeleton::NeoTimeZoneSkeleton;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::locale::locale;
use icu::timezone::CustomZonedDateTime;
use writeable::assert_try_writeable_eq;
let mut zone_london_winter = CustomZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]").unwrap().zone;
let mut zone_london_summer = CustomZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]").unwrap().zone;
let mut names =
TypedDateTimeNames::<Gregorian, NeoTimeZoneSkeleton>::try_new(&locale!("en-GB").into())
.unwrap();
names
.include_time_zone_essentials()
.unwrap();
names
.include_time_zone_generic_short_names()
.unwrap();
// Create a pattern with symbol `v`:
let pattern_str = "'Your time zone is:' v";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: GMT",
);
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_summer),
"Your time zone is: GMT", // TODO
);
sourcepub fn load_time_zone_specific_long_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<MetazoneSpecificNamesLongV1Marker> + ?Sized,
pub fn load_time_zone_specific_long_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<MetazoneSpecificNamesLongV1Marker> + ?Sized,
Loads specific non-location long time zone names.
sourcepub fn include_time_zone_specific_long_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneSpecificNamesLongV1Marker>,
pub fn include_time_zone_specific_long_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneSpecificNamesLongV1Marker>,
Includes specific non-location long time zone names.
Important: When performing manual time zone data loading, in addition to the specific time zone format data, also call either:
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::TypedDateTimeNames;
use icu::datetime::neo_skeleton::NeoTimeZoneSkeleton;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::locale::locale;
use icu::timezone::CustomZonedDateTime;
use writeable::assert_try_writeable_eq;
let mut zone_london_winter = CustomZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]").unwrap().zone;
let mut zone_london_summer = CustomZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]").unwrap().zone;
let mut names =
TypedDateTimeNames::<Gregorian, NeoTimeZoneSkeleton>::try_new(&locale!("en-GB").into())
.unwrap();
names
.include_time_zone_essentials()
.unwrap();
names
.include_time_zone_specific_long_names()
.unwrap();
// Create a pattern with symbol `zzzz`:
let pattern_str = "'Your time zone is:' zzzz";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: Greenwich Mean Time",
);
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_summer),
"Your time zone is: British Summer Time",
);
sourcepub fn load_time_zone_specific_short_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<MetazoneSpecificNamesShortV1Marker> + ?Sized,
pub fn load_time_zone_specific_short_names<P>(
&mut self,
provider: &P,
) -> Result<&mut Self, SingleLoadError>where
P: DataProvider<MetazoneSpecificNamesShortV1Marker> + ?Sized,
Loads specific non-location short time zone names.
sourcepub fn include_time_zone_specific_short_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneSpecificNamesShortV1Marker>,
pub fn include_time_zone_specific_short_names(
&mut self,
) -> Result<&mut Self, SingleLoadError>where
Baked: DataProvider<MetazoneSpecificNamesShortV1Marker>,
Includes specific non-location short time zone names.
Important: When performing manual time zone data loading, in addition to the specific time zone format data, also call either:
ยงExamples
use icu::calendar::Gregorian;
use icu::datetime::TypedDateTimeNames;
use icu::datetime::neo_skeleton::NeoTimeZoneSkeleton;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::locale::locale;
use icu::timezone::CustomZonedDateTime;
use writeable::assert_try_writeable_eq;
let mut zone_london_winter = CustomZonedDateTime::try_from_str("2024-01-01T00:00:00+00:00[Europe/London]").unwrap().zone;
let mut zone_london_summer = CustomZonedDateTime::try_from_str("2024-07-01T00:00:00+01:00[Europe/London]").unwrap().zone;
let mut names =
TypedDateTimeNames::<Gregorian, NeoTimeZoneSkeleton>::try_new(&locale!("en-GB").into())
.unwrap();
names
.include_time_zone_essentials()
.unwrap();
names
.include_time_zone_specific_short_names()
.unwrap();
// Create a pattern with symbol `z`:
let pattern_str = "'Your time zone is:' z";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_winter),
"Your time zone is: GMT",
);
assert_try_writeable_eq!(
names.with_pattern(&pattern).format(&zone_london_summer),
"Your time zone is: BST",
);
sourcepub fn set_week_calculator(
&mut self,
week_calculator: WeekCalculator,
) -> &mut Self
pub fn set_week_calculator( &mut self, week_calculator: WeekCalculator, ) -> &mut Self
Sets the week calculator to use with patterns requiring week numbering.
ยงExamples
use icu::calendar::week::WeekCalculator;
use icu::calendar::Date;
use icu::calendar::Gregorian;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::datetime::TypedDateTimeNames;
use icu::locale::locale;
use writeable::assert_try_writeable_eq;
let mut names =
TypedDateTimeNames::<Gregorian>::try_new(&locale!("en").into())
.unwrap();
// Load the week calculator and set it here:
let mut week_calculator =
WeekCalculator::try_new(&locale!("en").into()).unwrap();
names.set_week_calculator(week_calculator);
// Format a pattern needing week data:
let pattern_str = "'Week' w 'of' Y";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
let date = Date::try_new_gregorian_date(2023, 12, 5).unwrap();
assert_try_writeable_eq!(
names.with_pattern(&pattern).format_date(&date),
"Week 49 of 2023"
);
sourcepub fn with_pattern<'l>(
&'l self,
pattern: &'l DateTimePattern,
) -> DateTimePatternFormatter<'l, C, R>
pub fn with_pattern<'l>( &'l self, pattern: &'l DateTimePattern, ) -> DateTimePatternFormatter<'l, C, R>
Associates this TypedDateTimeNames
with a pattern
without loading additional data for that pattern.
sourcepub fn load_for_pattern<'l, P>(
&'l mut self,
provider: &P,
pattern: &'l DateTimePattern,
) -> Result<DateTimePatternFormatter<'l, C, R>, LoadError>where
P: DataProvider<C::YearNamesV1Marker> + DataProvider<C::MonthNamesV1Marker> + DataProvider<WeekdayNamesV1Marker> + DataProvider<DayPeriodNamesV1Marker> + DataProvider<TimeZoneFormatsV1Marker> + DataProvider<ExemplarCitiesV1Marker> + DataProvider<MetazoneGenericNamesLongV1Marker> + DataProvider<MetazoneGenericNamesShortV1Marker> + DataProvider<MetazoneSpecificNamesLongV1Marker> + DataProvider<MetazoneSpecificNamesShortV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<WeekDataV2Marker> + ?Sized,
pub fn load_for_pattern<'l, P>(
&'l mut self,
provider: &P,
pattern: &'l DateTimePattern,
) -> Result<DateTimePatternFormatter<'l, C, R>, LoadError>where
P: DataProvider<C::YearNamesV1Marker> + DataProvider<C::MonthNamesV1Marker> + DataProvider<WeekdayNamesV1Marker> + DataProvider<DayPeriodNamesV1Marker> + DataProvider<TimeZoneFormatsV1Marker> + DataProvider<ExemplarCitiesV1Marker> + DataProvider<MetazoneGenericNamesLongV1Marker> + DataProvider<MetazoneGenericNamesShortV1Marker> + DataProvider<MetazoneSpecificNamesLongV1Marker> + DataProvider<MetazoneSpecificNamesShortV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<WeekDataV2Marker> + ?Sized,
Associates this TypedDateTimeNames
with a datetime pattern
and loads all data required for that pattern.
Does not duplicate textual field symbols. See #4337
sourcepub fn include_for_pattern<'l>(
&'l mut self,
pattern: &'l DateTimePattern,
) -> Result<DateTimePatternFormatter<'l, C, R>, LoadError>where
Baked: DataProvider<C::YearNamesV1Marker> + DataProvider<C::MonthNamesV1Marker> + DataProvider<WeekdayNamesV1Marker> + DataProvider<DayPeriodNamesV1Marker> + DataProvider<TimeZoneFormatsV1Marker> + DataProvider<MetazoneGenericNamesShortV1Marker>,
pub fn include_for_pattern<'l>(
&'l mut self,
pattern: &'l DateTimePattern,
) -> Result<DateTimePatternFormatter<'l, C, R>, LoadError>where
Baked: DataProvider<C::YearNamesV1Marker> + DataProvider<C::MonthNamesV1Marker> + DataProvider<WeekdayNamesV1Marker> + DataProvider<DayPeriodNamesV1Marker> + DataProvider<TimeZoneFormatsV1Marker> + DataProvider<MetazoneGenericNamesShortV1Marker>,
Associates this TypedDateTimeNames
with a pattern
and includes all data required for that pattern.
Does not support duplicate textual field symbols. See #4337
ยงExamples
use icu::calendar::DateTime;
use icu::calendar::Gregorian;
use icu::datetime::neo_pattern::DateTimePattern;
use icu::datetime::TypedDateTimeNames;
use icu::locale::locale;
use writeable::assert_try_writeable_eq;
let mut names =
TypedDateTimeNames::<Gregorian>::try_new(&locale!("en").into())
.unwrap();
// Create a pattern from a pattern string:
let pattern_str = "EEEE 'on week' w 'of' Y G (MMM d) 'at' h:mm a";
let pattern: DateTimePattern = pattern_str.parse().unwrap();
// Load data for the pattern and format:
let datetime =
DateTime::try_new_gregorian_datetime(2023, 12, 5, 17, 43, 12).unwrap();
assert_try_writeable_eq!(
names
.include_for_pattern(&pattern)
.unwrap()
.format(&datetime),
"Tuesday on week 49 of 2023 AD (Dec 5) at 5:43 PM"
);
Trait Implementationsยง
Auto Trait Implementationsยง
impl<C, R> Freeze for TypedDateTimeNames<C, R>where
<<R as DateTimeNamesMarker>::YearNames as DateTimeNamesHolderTrait<YearNamesV1Marker>>::Container<FieldLength>: Freeze,
<<R as DateTimeNamesMarker>::MonthNames as DateTimeNamesHolderTrait<MonthNamesV1Marker>>::Container<(Month, FieldLength)>: Freeze,
<<R as DateTimeNamesMarker>::WeekdayNames as DateTimeNamesHolderTrait<WeekdayNamesV1Marker>>::Container<(Weekday, FieldLength)>: Freeze,
<<R as DateTimeNamesMarker>::DayPeriodNames as DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>>::Container<FieldLength>: Freeze,
<<R as DateTimeNamesMarker>::ZoneEssentials as DateTimeNamesHolderTrait<TimeZoneFormatsV1Marker>>::Container<()>: Freeze,
<<R as DateTimeNamesMarker>::ZoneExemplarCities as DateTimeNamesHolderTrait<ExemplarCitiesV1Marker>>::Container<()>: Freeze,
<<R as DateTimeNamesMarker>::ZoneGenericLong as DateTimeNamesHolderTrait<MetazoneGenericNamesLongV1Marker>>::Container<()>: Freeze,
<<R as DateTimeNamesMarker>::ZoneGenericShort as DateTimeNamesHolderTrait<MetazoneGenericNamesShortV1Marker>>::Container<()>: Freeze,
<<R as DateTimeNamesMarker>::ZoneSpecificLong as DateTimeNamesHolderTrait<MetazoneSpecificNamesLongV1Marker>>::Container<()>: Freeze,
<<R as DateTimeNamesMarker>::ZoneSpecificShort as DateTimeNamesHolderTrait<MetazoneSpecificNamesShortV1Marker>>::Container<()>: Freeze,
impl<C, R> RefUnwindSafe for TypedDateTimeNames<C, R>where
<<R as DateTimeNamesMarker>::YearNames as DateTimeNamesHolderTrait<YearNamesV1Marker>>::Container<FieldLength>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::MonthNames as DateTimeNamesHolderTrait<MonthNamesV1Marker>>::Container<(Month, FieldLength)>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::WeekdayNames as DateTimeNamesHolderTrait<WeekdayNamesV1Marker>>::Container<(Weekday, FieldLength)>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::DayPeriodNames as DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>>::Container<FieldLength>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::ZoneEssentials as DateTimeNamesHolderTrait<TimeZoneFormatsV1Marker>>::Container<()>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::ZoneExemplarCities as DateTimeNamesHolderTrait<ExemplarCitiesV1Marker>>::Container<()>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::ZoneGenericLong as DateTimeNamesHolderTrait<MetazoneGenericNamesLongV1Marker>>::Container<()>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::ZoneGenericShort as DateTimeNamesHolderTrait<MetazoneGenericNamesShortV1Marker>>::Container<()>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::ZoneSpecificLong as DateTimeNamesHolderTrait<MetazoneSpecificNamesLongV1Marker>>::Container<()>: RefUnwindSafe,
<<R as DateTimeNamesMarker>::ZoneSpecificShort as DateTimeNamesHolderTrait<MetazoneSpecificNamesShortV1Marker>>::Container<()>: RefUnwindSafe,
C: RefUnwindSafe,
R: RefUnwindSafe,
impl<C, R> Send for TypedDateTimeNames<C, R>where
<<R as DateTimeNamesMarker>::YearNames as DateTimeNamesHolderTrait<YearNamesV1Marker>>::Container<FieldLength>: Send,
<<R as DateTimeNamesMarker>::MonthNames as DateTimeNamesHolderTrait<MonthNamesV1Marker>>::Container<(Month, FieldLength)>: Send,
<<R as DateTimeNamesMarker>::WeekdayNames as DateTimeNamesHolderTrait<WeekdayNamesV1Marker>>::Container<(Weekday, FieldLength)>: Send,
<<R as DateTimeNamesMarker>::DayPeriodNames as DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>>::Container<FieldLength>: Send,
<<R as DateTimeNamesMarker>::ZoneEssentials as DateTimeNamesHolderTrait<TimeZoneFormatsV1Marker>>::Container<()>: Send,
<<R as DateTimeNamesMarker>::ZoneExemplarCities as DateTimeNamesHolderTrait<ExemplarCitiesV1Marker>>::Container<()>: Send,
<<R as DateTimeNamesMarker>::ZoneGenericLong as DateTimeNamesHolderTrait<MetazoneGenericNamesLongV1Marker>>::Container<()>: Send,
<<R as DateTimeNamesMarker>::ZoneGenericShort as DateTimeNamesHolderTrait<MetazoneGenericNamesShortV1Marker>>::Container<()>: Send,
<<R as DateTimeNamesMarker>::ZoneSpecificLong as DateTimeNamesHolderTrait<MetazoneSpecificNamesLongV1Marker>>::Container<()>: Send,
<<R as DateTimeNamesMarker>::ZoneSpecificShort as DateTimeNamesHolderTrait<MetazoneSpecificNamesShortV1Marker>>::Container<()>: Send,
C: Send,
R: Send,
impl<C, R> Sync for TypedDateTimeNames<C, R>where
<<R as DateTimeNamesMarker>::YearNames as DateTimeNamesHolderTrait<YearNamesV1Marker>>::Container<FieldLength>: Sync,
<<R as DateTimeNamesMarker>::MonthNames as DateTimeNamesHolderTrait<MonthNamesV1Marker>>::Container<(Month, FieldLength)>: Sync,
<<R as DateTimeNamesMarker>::WeekdayNames as DateTimeNamesHolderTrait<WeekdayNamesV1Marker>>::Container<(Weekday, FieldLength)>: Sync,
<<R as DateTimeNamesMarker>::DayPeriodNames as DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>>::Container<FieldLength>: Sync,
<<R as DateTimeNamesMarker>::ZoneEssentials as DateTimeNamesHolderTrait<TimeZoneFormatsV1Marker>>::Container<()>: Sync,
<<R as DateTimeNamesMarker>::ZoneExemplarCities as DateTimeNamesHolderTrait<ExemplarCitiesV1Marker>>::Container<()>: Sync,
<<R as DateTimeNamesMarker>::ZoneGenericLong as DateTimeNamesHolderTrait<MetazoneGenericNamesLongV1Marker>>::Container<()>: Sync,
<<R as DateTimeNamesMarker>::ZoneGenericShort as DateTimeNamesHolderTrait<MetazoneGenericNamesShortV1Marker>>::Container<()>: Sync,
<<R as DateTimeNamesMarker>::ZoneSpecificLong as DateTimeNamesHolderTrait<MetazoneSpecificNamesLongV1Marker>>::Container<()>: Sync,
<<R as DateTimeNamesMarker>::ZoneSpecificShort as DateTimeNamesHolderTrait<MetazoneSpecificNamesShortV1Marker>>::Container<()>: Sync,
C: Sync,
R: Sync,
impl<C, R> Unpin for TypedDateTimeNames<C, R>where
<<R as DateTimeNamesMarker>::YearNames as DateTimeNamesHolderTrait<YearNamesV1Marker>>::Container<FieldLength>: Unpin,
<<R as DateTimeNamesMarker>::MonthNames as DateTimeNamesHolderTrait<MonthNamesV1Marker>>::Container<(Month, FieldLength)>: Unpin,
<<R as DateTimeNamesMarker>::WeekdayNames as DateTimeNamesHolderTrait<WeekdayNamesV1Marker>>::Container<(Weekday, FieldLength)>: Unpin,
<<R as DateTimeNamesMarker>::DayPeriodNames as DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>>::Container<FieldLength>: Unpin,
<<R as DateTimeNamesMarker>::ZoneEssentials as DateTimeNamesHolderTrait<TimeZoneFormatsV1Marker>>::Container<()>: Unpin,
<<R as DateTimeNamesMarker>::ZoneExemplarCities as DateTimeNamesHolderTrait<ExemplarCitiesV1Marker>>::Container<()>: Unpin,
<<R as DateTimeNamesMarker>::ZoneGenericLong as DateTimeNamesHolderTrait<MetazoneGenericNamesLongV1Marker>>::Container<()>: Unpin,
<<R as DateTimeNamesMarker>::ZoneGenericShort as DateTimeNamesHolderTrait<MetazoneGenericNamesShortV1Marker>>::Container<()>: Unpin,
<<R as DateTimeNamesMarker>::ZoneSpecificLong as DateTimeNamesHolderTrait<MetazoneSpecificNamesLongV1Marker>>::Container<()>: Unpin,
<<R as DateTimeNamesMarker>::ZoneSpecificShort as DateTimeNamesHolderTrait<MetazoneSpecificNamesShortV1Marker>>::Container<()>: Unpin,
C: Unpin,
R: Unpin,
impl<C, R> UnwindSafe for TypedDateTimeNames<C, R>where
<<R as DateTimeNamesMarker>::YearNames as DateTimeNamesHolderTrait<YearNamesV1Marker>>::Container<FieldLength>: UnwindSafe,
<<R as DateTimeNamesMarker>::MonthNames as DateTimeNamesHolderTrait<MonthNamesV1Marker>>::Container<(Month, FieldLength)>: UnwindSafe,
<<R as DateTimeNamesMarker>::WeekdayNames as DateTimeNamesHolderTrait<WeekdayNamesV1Marker>>::Container<(Weekday, FieldLength)>: UnwindSafe,
<<R as DateTimeNamesMarker>::DayPeriodNames as DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>>::Container<FieldLength>: UnwindSafe,
<<R as DateTimeNamesMarker>::ZoneEssentials as DateTimeNamesHolderTrait<TimeZoneFormatsV1Marker>>::Container<()>: UnwindSafe,
<<R as DateTimeNamesMarker>::ZoneExemplarCities as DateTimeNamesHolderTrait<ExemplarCitiesV1Marker>>::Container<()>: UnwindSafe,
<<R as DateTimeNamesMarker>::ZoneGenericLong as DateTimeNamesHolderTrait<MetazoneGenericNamesLongV1Marker>>::Container<()>: UnwindSafe,
<<R as DateTimeNamesMarker>::ZoneGenericShort as DateTimeNamesHolderTrait<MetazoneGenericNamesShortV1Marker>>::Container<()>: UnwindSafe,
<<R as DateTimeNamesMarker>::ZoneSpecificLong as DateTimeNamesHolderTrait<MetazoneSpecificNamesLongV1Marker>>::Container<()>: UnwindSafe,
<<R as DateTimeNamesMarker>::ZoneSpecificShort as DateTimeNamesHolderTrait<MetazoneSpecificNamesShortV1Marker>>::Container<()>: UnwindSafe,
C: UnwindSafe,
R: UnwindSafe,
Blanket Implementationsยง
sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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