Expand description
Enumerations over field sets.
These enumerations can be used when the field set is not known at compile time. However, they may contribute negatively to the binary size of the formatters.
The most general type is CompositeFieldSet
, which supports all field
sets in a single enumeration. CompositeDateTimeFieldSet
is a good
choice when you don’t need to format time zones.
Summary of all the types:
Type | Supported Field Sets |
---|---|
DateFieldSet | Date |
CalendarPeriodFieldSet | Calendar Period |
TimeFieldSet | Time |
ZoneFieldSet | Zone |
DateAndTimeFieldSet | Date + Time |
CompositeDateTimeFieldSet | Date, Calendar Period, Time, Date + Time |
CompositeFieldSet | All |
§Examples
Format with the time display depending on a runtime boolean:
use icu::calendar::DateTime;
use icu::datetime::fieldsets;
use icu::datetime::fieldsets::enums::CompositeDateTimeFieldSet;
use icu::datetime::DateTimeFormatter;
use icu::locale::locale;
use writeable::Writeable;
fn get_field_set(should_display_time: bool) -> CompositeDateTimeFieldSet {
if should_display_time {
let field_set = fieldsets::MDT::medium().hm();
CompositeDateTimeFieldSet::DateTime(
fieldsets::enums::DateAndTimeFieldSet::MDT(field_set),
)
} else {
let field_set = fieldsets::MD::medium();
CompositeDateTimeFieldSet::Date(fieldsets::enums::DateFieldSet::MD(
field_set,
))
}
}
let datetime = DateTime::try_new_iso(2025, 1, 15, 16, 0, 0).unwrap();
let results = [true, false]
.map(get_field_set)
.map(|field_set| {
DateTimeFormatter::try_new(locale!("en-US").into(), field_set)
.unwrap()
})
.map(|formatter| formatter.format_any_calendar(&datetime).to_string());
assert_eq!(results, ["Jan 15, 4:00 PM", "Jan 15"])
Enums§
- An enumeration over all possible calendar period field sets.
- An enum supporting date, calendar period, time, and date+time field sets and options.
- An enum supporting all possible field sets and options.
- An enumeration over all possible date+time composite field sets.
- An enumeration over all possible date field sets.
- An enumeration over all possible time field sets.
- An enumeration over all possible zone field sets.