9 releases (2 stable)

new 1.1.0 Jan 26, 2023
1.0.0 Sep 27, 2022
1.0.0-beta1 Aug 5, 2022
0.6.0 May 17, 2022
0.1.0 Oct 15, 2020

#10 in Internationalization (i18n)

Download history 2070/week @ 2022-10-07 2212/week @ 2022-10-14 2969/week @ 2022-10-21 2433/week @ 2022-10-28 2259/week @ 2022-11-04 2286/week @ 2022-11-11 1574/week @ 2022-11-18 1833/week @ 2022-11-25 1427/week @ 2022-12-02 1199/week @ 2022-12-09 1441/week @ 2022-12-16 397/week @ 2022-12-23 448/week @ 2022-12-30 1389/week @ 2023-01-06 1107/week @ 2023-01-13 1711/week @ 2023-01-20

4,718 downloads per month
Used in 12 crates (7 directly)

Unicode-DFS-2016

3.5MB
37K SLoC

icu_datetime crates.io

Formatting date and time.

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.

[TypedDateTimeFormatter] and [DateTimeFormatter] are the main types of the component. They accepts a set of arguments which allow it to collect necessary data from the data provider, and once instantiated, can be used to quickly format any date and time provided. There are variants of these types that can format greater or fewer components, including [TypedDateFormatter] & [DateFormatter], [TypedZonedDateTimeFormatter] & [ZonedDateTimeFormatter], [TimeFormatter], and TimeZoneFormatter

These formatters work with types from the calendar module, like Date, DateTime, and Time, and timezone::CustomTimeZone, however other types may be used provided they implement the traits from the [input] module.

Each instance of a date-related formatter (i.e. not [TimeFormatter] or TimeZoneFormatter is associated with a particular Calendar. The "Typed" vs untyped formatter distinction is to help with this. For example, if you know at compile time that you will only be formatting Gregorian dates, you can use TypedDateTimeFormatter<Gregorian> and the APIs will make sure that only Gregorian DateTimes are used with the calendar. On the other hand, if you want to be able to select the calendar at runtime, you can use [DateTimeFormatter] with the calendar specified in the locale, and use it with DateTime,AnyCalendar. These formatters still require dates associated with the appropriate calendar (though they will convert ISO dates to the calendar if provided), they just do not force the programmer to pick the calendar at compile time.

Examples

use icu::calendar::{DateTime, Gregorian};
use icu::datetime::{
    options::length, DateTimeFormatter, DateTimeFormatterOptions,
    TypedDateTimeFormatter,
};
use icu::locid::{locale, Locale};
use std::str::FromStr;
use writeable::assert_writeable_eq;

// See the next code example for a more ergonomic example with .into().
let options =
    DateTimeFormatterOptions::Length(length::Bag::from_date_time_style(
        length::Date::Medium,
        length::Time::Short,
    ));

// You can work with a formatter that can select the calendar at runtime:
let locale = Locale::from_str("en-u-ca-gregory").unwrap();
let dtf = DateTimeFormatter::try_new_unstable(
    &icu_testdata::unstable(),
    &locale.into(),
    options.clone(),
)
.expect("Failed to create DateTimeFormatter instance.");

// Or one that selects a calendar at compile time:
let typed_dtf = TypedDateTimeFormatter::<Gregorian>::try_new_unstable(
    &icu_testdata::unstable(),
    &locale!("en").into(),
    options,
)
.expect("Failed to create TypedDateTimeFormatter instance.");

let typed_date =
    DateTime::try_new_gregorian_datetime(2020, 9, 12, 12, 34, 28).unwrap();
// prefer using ISO dates with DateTimeFormatter
let date = typed_date.to_iso().to_any();

let formatted_date = dtf.format(&date).expect("Calendars should match");
let typed_formatted_date = typed_dtf.format(&typed_date);

assert_writeable_eq!(formatted_date, "Sep 12, 2020, 12:34 PM");
assert_writeable_eq!(typed_formatted_date, "Sep 12, 2020, 12:34 PM");

let formatted_date_string =
    dtf.format_to_string(&date).expect("Calendars should match");
let typed_formatted_date_string = typed_dtf.format_to_string(&typed_date);

assert_eq!(formatted_date_string, "Sep 12, 2020, 12:34 PM");
assert_eq!(typed_formatted_date_string, "Sep 12, 2020, 12:34 PM");

The options can be created more ergonomically using the Into trait to automatically convert a [options::length::Bag] into a [DateTimeFormatterOptions::Length].

use icu::calendar::Gregorian;
use icu::datetime::{
    options::length, DateTimeFormatterOptions, TypedDateTimeFormatter,
};
use icu::locid::locale;
let options = length::Bag::from_date_time_style(
    length::Date::Medium,
    length::Time::Short,
)
.into();

let dtf = TypedDateTimeFormatter::<Gregorian>::try_new_unstable(
    &icu_testdata::unstable(),
    &locale!("en").into(),
    options,
);

At the moment, the crate provides only options using the Length bag, but in the future, we expect to add more ways to customize the output, like skeletons, and components.

More Information

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

Dependencies

~0.5–5.5MB
~91K SLoC