Crates.io | icu_datetime |
lib.rs | icu_datetime |
version | 2.0.0-beta1 |
source | src |
created_at | 2020-10-15 15:38:22.267337 |
updated_at | 2024-11-23 02:18:34.411856 |
description | API for formatting date and time to user readable textual representation |
homepage | https://icu4x.unicode.org |
repository | https://github.com/unicode-org/icu4x |
max_upload_size | |
id | 300044 |
size | 1,026,350 |
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.
icu::datetime::fieldsets
icu::datetime::options
ICU4X supports formatting in over one dozen calendar systems, including Gregorian, Buddhist, Islamic, 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:
Not all inputs are valid for all field sets.
The datetime crate has been engineered with a focus on giving developers the ability to tune binary size to their needs. The table illustrates the two main tradeoffs, field sets and calendar systems:
Factor | Static (Lower Binary Size) | Dynamic (Greater Binary Size) |
---|---|---|
Field Sets | Specific [fieldsets ] types |
Enumerations from [fieldsets::enums ] |
Calendar Systems | [FixedCalendarDateTimeFormatter ] |
[DateTimeFormatter ] |
If formatting times and time zones without dates, consider using [TimeFormatter
].
use icu::calendar::DateTime;
use icu::datetime::fieldsets;
use icu::datetime::DateTimeFormatter;
use icu::locale::{locale, Locale};
use writeable::assert_writeable_eq;
// Field set for year, month, day, hour, and minute with a medium length:
let field_set = fieldsets::YMDT::medium().hm();
// Create a formatter for Argentinian Spanish:
let locale = locale!("es-AR");
let dtf = DateTimeFormatter::try_new(locale.into(), field_set).unwrap();
// Format something:
let datetime = DateTime::try_new_iso(2025, 1, 15, 16, 9, 35).unwrap();
let formatted_date = dtf.format_any_calendar(&datetime);
assert_writeable_eq!(formatted_date, "15 de ene de 2025, 4:09 p. m.");
For more information on development, authorship, contributing etc. please visit ICU4X home page
.