icu_datetime/
lib.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
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//! Islamic, 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//!
34//! Not all inputs are valid for all field sets.
35//!
36//! # Binary Size Tradeoffs
37//!
38//! The datetime crate has been engineered with a focus on giving developers the ability to
39//! tune binary size to their needs. The table illustrates the two main tradeoffs, field sets
40//! and calendar systems:
41//!
42//! | Factor | Static (Lower Binary Size) | Dynamic (Greater Binary Size) |
43//! |---|---|---|
44//! | Field Sets | Specific [`fieldsets`] types | Enumerations from [`fieldsets::enums`] |
45//! | Calendar Systems | [`FixedCalendarDateTimeFormatter`] | [`DateTimeFormatter`] |
46//!
47//! If formatting times and time zones without dates, consider using [`NoCalendarFormatter`].
48//!
49//! # Examples
50//!
51//! ```
52//! use icu::datetime::fieldsets;
53//! use icu::datetime::input::Date;
54//! use icu::datetime::input::{DateTime, Time};
55//! use icu::datetime::DateTimeFormatter;
56//! use icu::locale::{locale, Locale};
57//! use writeable::assert_writeable_eq;
58//!
59//! // Field set for year, month, day, hour, and minute with a medium length:
60//! let field_set = fieldsets::YMDT::medium().hm();
61//!
62//! // Create a formatter for Argentinian Spanish:
63//! let locale = locale!("es-AR");
64//! let dtf = DateTimeFormatter::try_new(locale.into(), field_set).unwrap();
65//!
66//! // Format something:
67//! let datetime = DateTime {
68//!     date: Date::try_new_iso(2025, 1, 15).unwrap(),
69//!     time: Time::try_new(16, 9, 35, 0).unwrap(),
70//! };
71//! let formatted_date = dtf.format(&datetime);
72//!
73//! assert_writeable_eq!(formatted_date, "15 de ene de 2025, 4:09 p. m.");
74//! ```
75
76// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
77#![cfg_attr(not(any(test, doc)), no_std)]
78#![cfg_attr(
79    not(test),
80    deny(
81        clippy::indexing_slicing,
82        clippy::unwrap_used,
83        clippy::expect_used,
84        clippy::panic,
85        clippy::exhaustive_structs,
86        clippy::exhaustive_enums,
87        clippy::trivially_copy_pass_by_ref,
88        missing_debug_implementations,
89    )
90)]
91#![warn(missing_docs)]
92
93extern crate alloc;
94
95mod combo;
96mod error;
97mod external_loaders;
98pub mod fieldsets;
99mod format;
100mod neo;
101pub mod options;
102pub mod parts;
103pub mod pattern;
104pub mod provider;
105pub(crate) mod raw;
106pub mod scaffold;
107pub(crate) mod size_test_macro;
108
109pub use error::{DateTimeFormatterLoadError, DateTimeWriteError, MismatchedCalendarError};
110
111pub use neo::DateTimeFormatter;
112pub use neo::DateTimeFormatterPreferences;
113pub use neo::FixedCalendarDateTimeFormatter;
114pub use neo::FormattedDateTime;
115pub use neo::NoCalendarFormatter;
116pub use options::Length;
117
118/// Locale preferences used by this crate
119pub mod preferences {
120    /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
121    #[doc = "\n"] // prevent autoformatting
122    pub use icu_locale_core::preferences::extensions::unicode::keywords::CalendarAlgorithm;
123    /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
124    #[doc = "\n"] // prevent autoformatting
125    pub use icu_locale_core::preferences::extensions::unicode::keywords::HourCycle;
126    /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
127    #[doc = "\n"] // prevent autoformatting
128    pub use icu_locale_core::preferences::extensions::unicode::keywords::NumberingSystem;
129}
130
131/// Types that can be fed to [`DateTimeFormatter`]/[`FixedCalendarDateTimeFormatter`].
132pub mod input {
133    /// **This is a reexport of a type in [`icu_calendar`]**.
134    #[doc = "\n"] // prevent autoformatting
135    pub use icu_calendar::Date;
136    /// **This is a reexport of a type in [`icu_time`]**.
137    #[doc = "\n"] // prevent autoformatting
138    pub use icu_time::zone::UtcOffset;
139    /// **This is a reexport of a type in [`icu_time`]**.
140    #[doc = "\n"] // prevent autoformatting
141    pub use icu_time::DateTime;
142    /// **This is a reexport of a type in [`icu_time`]**.
143    #[doc = "\n"] // prevent autoformatting
144    pub use icu_time::Time;
145    /// **This is a reexport of a type in [`icu_time`]**.
146    #[doc = "\n"] // prevent autoformatting
147    pub use icu_time::TimeZone;
148    /// **This is a reexport of a type in [`icu_time`]**.
149    #[doc = "\n"] // prevent autoformatting
150    pub use icu_time::TimeZoneInfo;
151    /// **This is a reexport of a type in [`icu_time`]**.
152    #[doc = "\n"] // prevent autoformatting
153    pub use icu_time::ZonedDateTime;
154}