| 1 |
# icu_datetime [](https://crates.io/crates/icu_datetime) |
| 2 |
|
| 3 |
<!-- cargo-rdme start --> |
| 4 |
|
| 5 |
Localized formatting of dates, times, and time zones. |
| 6 |
|
| 7 |
This module is published as its own crate ([`icu_datetime`](https://docs.rs/icu_datetime/latest/icu_datetime/)) |
| 8 |
and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project. |
| 9 |
|
| 10 |
ICU4X datetime formatting follows the Unicode UTS 35 standard for [Semantic Skeletons](https://unicode.org/reports/tr35/tr35-dates.html#Semantic_Skeletons). |
| 11 |
First you choose a _field set_, then you configure the formatting _options_ to your desired context. |
| 12 |
|
| 13 |
1. Field Sets: [`icu::datetime::fieldsets`](fieldsets) |
| 14 |
2. Options: [`icu::datetime::options`](options) |
| 15 |
|
| 16 |
ICU4X supports formatting in over one dozen _calendar systems_, including Gregorian, Buddhist, |
| 17 |
Hijri, and more. The calendar system is usually derived from the locale, but it can also be |
| 18 |
specified explicitly. |
| 19 |
|
| 20 |
The main formatter in this crate is [`DateTimeFormatter`], which supports all field sets, |
| 21 |
options, and calendar systems. Additional formatter types are available to developers in |
| 22 |
resource-constrained environments. |
| 23 |
|
| 24 |
The formatters accept input types from the [`calendar`](icu_calendar) and |
| 25 |
[`timezone`](icu_time) crates (Also reexported from the [`input`] module of this crate): |
| 26 |
|
| 27 |
1. [`Date`](icu_calendar::Date) |
| 28 |
2. [`DateTime`](icu_time::DateTime) |
| 29 |
3. [`Time`](icu_time::Time) |
| 30 |
4. [`UtcOffset`](icu_time::zone::UtcOffset) |
| 31 |
5. [`TimeZoneInfo`](icu_time::TimeZoneInfo) |
| 32 |
6. [`ZonedDateTime`](icu_time::ZonedDateTime) |
| 33 |
7. And datetime types from third party crates; see [`input::unstable_third_party`]. |
| 34 |
|
| 35 |
Not all inputs are valid for all field sets. |
| 36 |
|
| 37 |
## Examples |
| 38 |
|
| 39 |
```rust |
| 40 |
use icu::datetime::DateTimeFormatter; |
| 41 |
use icu::datetime::fieldsets; |
| 42 |
use icu::datetime::input::Date; |
| 43 |
use icu::datetime::input::{DateTime, Time}; |
| 44 |
use icu::locale::{Locale, locale}; |
| 45 |
use writeable::assert_writeable_eq; |
| 46 |
|
| 47 |
// Field set for year, month, day, hour, and minute with a medium length: |
| 48 |
let field_set_with_options = fieldsets::YMD::medium().with_time_hm(); |
| 49 |
|
| 50 |
// Create a formatter for Argentinian Spanish: |
| 51 |
let locale = locale!("es-AR"); |
| 52 |
let dtf = DateTimeFormatter::try_new(locale.into(), field_set_with_options) |
| 53 |
.unwrap(); |
| 54 |
|
| 55 |
// Format something: |
| 56 |
let datetime = DateTime { |
| 57 |
date: Date::try_new_iso(2025, 1, 15).unwrap(), |
| 58 |
time: Time::try_new(16, 9, 35, 0).unwrap(), |
| 59 |
}; |
| 60 |
let formatted_date = dtf.format(&datetime); |
| 61 |
|
| 62 |
assert_writeable_eq!(formatted_date, "15 de ene de 2025, 4:09 p. m."); |
| 63 |
``` |
| 64 |
|
| 65 |
## Binary Size Considerations |
| 66 |
|
| 67 |
### Avoid linking unnecessary field sets data |
| 68 |
|
| 69 |
There are two APIs for fieldsets: |
| 70 |
* "static" field sets, like [`fieldsets::YMD`], where each field set is a *type*. |
| 71 |
* "dynamic" field sets, like [`fieldsets::enums::CompositeFieldSet`], where each field set is a *value*. |
| 72 |
|
| 73 |
While dynamic fields sets may offer a more powerful API, using them in constructors links data for all |
| 74 |
possible values, i.e. all patterns, that the dynamic field set can represent, even if they are |
| 75 |
unreachable in code. |
| 76 |
|
| 77 |
Static field sets on the other hand leverage the type system to let the compiler drop unneeded data. |
| 78 |
|
| 79 |
#### Example |
| 80 |
|
| 81 |
```rust |
| 82 |
use icu::datetime::DateTimeFormatter; |
| 83 |
use icu::datetime::fieldsets::YMD; |
| 84 |
use icu::datetime::fieldsets::enums::{CompositeFieldSet, DateFieldSet}; |
| 85 |
|
| 86 |
// This constructor only links data required for YMD |
| 87 |
let a: DateTimeFormatter<YMD> = |
| 88 |
DateTimeFormatter::try_new(Default::default(), YMD::medium()).unwrap(); |
| 89 |
|
| 90 |
// This constructor links data for *all possible field sets*, even though we only use YMD |
| 91 |
let b: DateTimeFormatter<CompositeFieldSet> = |
| 92 |
DateTimeFormatter::try_new(Default::default(), CompositeFieldSet::Date(DateFieldSet::YMD(YMD::medium()))).unwrap(); |
| 93 |
|
| 94 |
// If a DateTimeFormatter<CompositeFieldSet> is required, cast after construction instead: |
| 95 |
let c: DateTimeFormatter<CompositeFieldSet> = a.cast_into_fset::<CompositeFieldSet>(); |
| 96 |
``` |
| 97 |
|
| 98 |
### Avoid linking unnecessary calendar data |
| 99 |
|
| 100 |
All field sets that contain dates use different data for each calendar system when used with [`DateTimeFormatter`]. |
| 101 |
This is good i18n practice, as in general the calendar system should be derived from the user locale, |
| 102 |
not fixed in code. However, there are legitimate use cases where only one calendar system is supported, |
| 103 |
in which case [`DateTimeFormatter`] would link unused data. In this case [`FixedCalendarDateTimeFormatter`] |
| 104 |
can be used, which is generic in a calendar type and only links the data for that calendar. |
| 105 |
|
| 106 |
Using [`FixedCalendarDateTimeFormatter`] also avoids linking code that converts inputs to the user's calendar. |
| 107 |
For field sets that don't contain dates, this can also be achieved using [`NoCalendarFormatter`]. |
| 108 |
|
| 109 |
<!-- cargo-rdme end --> |
| 110 |
|
| 111 |
## More Information |
| 112 |
|
| 113 |
For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). |