icu_datetime 
Localized formatting of dates, times, and time zones.
This module is published as its own crate (icu_datetime)
and as part of the icu crate. See the latter for more details on the ICU4X project.
ICU4X datetime formatting follows the Unicode UTS 35 standard for Semantic Skeletons. First you choose a field set, then you configure the formatting options to your desired context.
- Field Sets:
icu::datetime::fieldsets - Options:
icu::datetime::options
ICU4X supports formatting in over one dozen calendar systems, including Gregorian, Buddhist, Hijri, and more. The calendar system is usually derived from the locale, but it can also be specified explicitly.
The main formatter in this crate is [DateTimeFormatter], which supports all field sets,
options, and calendar systems. Additional formatter types are available to developers in
resource-constrained environments.
The formatters accept input types from the calendar and
timezone crates (Also reexported from the [input] module of this crate):
Not all inputs are valid for all field sets.
Examples
use fieldsets;
use Date;
use ;
use DateTimeFormatter;
use ;
use assert_writeable_eq;
// Field set for year, month, day, hour, and minute with a medium length:
let field_set_with_options = YMDmedium.with_time_hm;
// Create a formatter for Argentinian Spanish:
let locale = locale!;
let dtf = try_new
.unwrap;
// Format something:
let datetime = DateTime ;
let formatted_date = dtf.format;
assert_writeable_eq!;
Binary Size Considerations
Avoid linking unnecessary field sets data
There are two APIs for fieldsets:
- "static" field sets, like [
fieldsets::YMD], where each field set is a type. - "dynamic" field sets, like [
fieldsets::enums::CompositeFieldSet], where each field set is a value.
While dynamic fields sets may offer a more powerful API, using them in constructors links data for all possible values, i.e. all patterns, that the dynamic field set can represent, even if they are unreachable in code.
Static field sets on the other hand leverage the type system to let the compiler drop unneeded data.
Example
use DateTimeFormatter;
use YMD;
use ;
// This constructor only links data required for YMD
let a: =
try_new.unwrap;
// This constructor links data for *all possible field sets*, even though we only use YMD
let b: =
try_new.unwrap;
// If a DateTimeFormatter<CompositeFieldSet> is required, cast after construction instead:
let c: = a.;
Avoid linking unnecessary calendar data
All field sets that contain dates use different data for each calendar system when used with [DateTimeFormatter].
This is good i18n practice, as in general the calendar system should be derived from the user locale,
not fixed in code. However, there are legitimate use cases where only one calendar system is supported,
in which case [DateTimeFormatter] would link unused data. In this case [FixedCalendarDateTimeFormatter]
can be used, which is generic in a calendar type and only links the data for that calendar.
Using [FixedCalendarDateTimeFormatter] also avoids linking code that converts inputs to the user's calendar.
For field sets that don't contain dates, this can also be achieved using [NoCalendarFormatter].
More Information
For more information on development, authorship, contributing etc. please visit ICU4X home page.