18 releases (10 stable)

2.0.0-beta1 Nov 23, 2024
1.5.1 Jun 6, 2024
1.5.0 May 28, 2024
1.4.0 Nov 16, 2023
0.1.0 Oct 15, 2020

#81 in Internationalization (i18n)

Download history 25239/week @ 2024-08-23 23540/week @ 2024-08-30 26883/week @ 2024-09-06 22028/week @ 2024-09-13 58734/week @ 2024-09-20 59652/week @ 2024-09-27 63075/week @ 2024-10-04 37928/week @ 2024-10-11 34353/week @ 2024-10-18 30890/week @ 2024-10-25 30611/week @ 2024-11-01 36587/week @ 2024-11-08 35226/week @ 2024-11-15 22541/week @ 2024-11-22 25321/week @ 2024-11-29 24139/week @ 2024-12-06

115,312 downloads per month
Used in 48 crates (13 directly)

Unicode-3.0

4MB
61K SLoC

icu_datetime crates.io

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.

  1. Field Sets: icu::datetime::fieldsets
  2. Options: 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:

  1. Date
  2. DateTime
  3. Time
  4. UtcOffset
  5. TimeZoneInfo
  6. CustomZonedDateTime

Not all inputs are valid for all field sets.

Binary Size Tradeoffs

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.

Examples

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.");

More Information

For more information on development, authorship, contributing etc. please visit ICU4X home page.

Dependencies