Debian Codegraph @

debian/rust-vendor/icu_datetime/src/lib.rs

raw
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 // https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
6 #![cfg_attr(not(any(test, doc)), no_std)]
7 #![cfg_attr(
8 not(test),
9 deny(
10 clippy::indexing_slicing,
11 clippy::unwrap_used,
12 clippy::expect_used,
13 clippy::panic,
14 )
15 )]
16 #![warn(missing_docs)]
17
18 //! Localized formatting of dates, times, and time zones.
19 //!
20 //! This module is published as its own crate ([`icu_datetime`](https://docs.rs/icu_datetime/latest/icu_datetime/))
21 //! and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.
22 //!
23 //! ICU4X datetime formatting follows the Unicode UTS 35 standard for [Semantic Skeletons](https://unicode.org/reports/tr35/tr35-dates.html#Semantic_Skeletons).
24 //! First you choose a _field set_, then you configure the formatting _options_ to your desired context.
25 //!
26 //! 1. Field Sets: [`icu::datetime::fieldsets`](fieldsets)
27 //! 2. Options: [`icu::datetime::options`](options)
28 //!
29 //! ICU4X supports formatting in over one dozen _calendar systems_, including Gregorian, Buddhist,
30 //! Hijri, and more. The calendar system is usually derived from the locale, but it can also be
31 //! specified explicitly.
32 //!
33 //! The main formatter in this crate is [`DateTimeFormatter`], which supports all field sets,
34 //! options, and calendar systems. Additional formatter types are available to developers in
35 //! resource-constrained environments.
36 //!
37 //! The formatters accept input types from the [`calendar`](icu_calendar) and
38 //! [`timezone`](icu_time) crates (Also reexported from the [`input`] module of this crate):
39 //!
40 //! 1. [`Date`](icu_calendar::Date)
41 //! 2. [`DateTime`](icu_time::DateTime)
42 //! 3. [`Time`](icu_time::Time)
43 //! 4. [`UtcOffset`](icu_time::zone::UtcOffset)
44 //! 5. [`TimeZoneInfo`](icu_time::TimeZoneInfo)
45 //! 6. [`ZonedDateTime`](icu_time::ZonedDateTime)
46 //! 7. And datetime types from third party crates; see [`input::unstable_third_party`].
47 //!
48 //! Not all inputs are valid for all field sets.
49 //!
50 //! # Examples
51 //!
52 //! ```
53 //! use icu::datetime::DateTimeFormatter;
54 //! use icu::datetime::fieldsets;
55 //! use icu::datetime::input::Date;
56 //! use icu::datetime::input::{DateTime, Time};
57 //! use icu::locale::{Locale, locale};
58 //! use writeable::assert_writeable_eq;
59 //!
60 //! // Field set for year, month, day, hour, and minute with a medium length:
61 //! let field_set_with_options = fieldsets::YMD::medium().with_time_hm();
62 //!
63 //! // Create a formatter for Argentinian Spanish:
64 //! let locale = locale!("es-AR");
65 //! let dtf = DateTimeFormatter::try_new(locale.into(), field_set_with_options)
66 //! .unwrap();
67 //!
68 //! // Format something:
69 //! let datetime = DateTime {
70 //! date: Date::try_new_iso(2025, 1, 15).unwrap(),
71 //! time: Time::try_new(16, 9, 35, 0).unwrap(),
72 //! };
73 //! let formatted_date = dtf.format(&datetime);
74 //!
75 //! assert_writeable_eq!(formatted_date, "15 de ene de 2025, 4:09 p. m.");
76 //! ```
77 //!
78 //! # Binary Size Considerations
79 //!
80 //! ## Avoid linking unnecessary field sets data
81 //!
82 //! There are two APIs for fieldsets:
83 //! * "static" field sets, like [`fieldsets::YMD`], where each field set is a *type*.
84 //! * "dynamic" field sets, like [`fieldsets::enums::CompositeFieldSet`], where each field set is a *value*.
85 //!
86 //! While dynamic fields sets may offer a more powerful API, using them in constructors links data for all
87 //! possible values, i.e. all patterns, that the dynamic field set can represent, even if they are
88 //! unreachable in code.
89 //!
90 //! Static field sets on the other hand leverage the type system to let the compiler drop unneeded data.
91 //!
92 //! ### Example
93 //!
94 //! ```
95 //! use icu::datetime::DateTimeFormatter;
96 //! use icu::datetime::fieldsets::YMD;
97 //! use icu::datetime::fieldsets::enums::{CompositeFieldSet, DateFieldSet};
98 //!
99 //! // This constructor only links data required for YMD
100 //! let a: DateTimeFormatter<YMD> =
101 //! DateTimeFormatter::try_new(Default::default(), YMD::medium()).unwrap();
102 //!
103 //! // This constructor links data for *all possible field sets*, even though we only use YMD
104 //! let b: DateTimeFormatter<CompositeFieldSet> =
105 //! DateTimeFormatter::try_new(Default::default(), CompositeFieldSet::Date(DateFieldSet::YMD(YMD::medium()))).unwrap();
106 //!
107 //! // If a DateTimeFormatter<CompositeFieldSet> is required, cast after construction instead:
108 //! let c: DateTimeFormatter<CompositeFieldSet> = a.cast_into_fset::<CompositeFieldSet>();
109 //! ```
110 //!
111 //! ## Avoid linking unnecessary calendar data
112 //!
113 //! All field sets that contain dates use different data for each calendar system when used with [`DateTimeFormatter`].
114 //! This is good i18n practice, as in general the calendar system should be derived from the user locale,
115 //! not fixed in code. However, there are legitimate use cases where only one calendar system is supported,
116 //! in which case [`DateTimeFormatter`] would link unused data. In this case [`FixedCalendarDateTimeFormatter`]
117 //! can be used, which is generic in a calendar type and only links the data for that calendar.
118 //!
119 //! Using [`FixedCalendarDateTimeFormatter`] also avoids linking code that converts inputs to the user's calendar.
120 //! For field sets that don't contain dates, this can also be achieved using [`NoCalendarFormatter`].
121
122 extern crate alloc;
123
124 #[cfg(feature = "unstable_chrono_0_4")]
125 mod chrono;
126 mod combo;
127 mod error;
128 mod external_loaders;
129 pub mod fieldsets;
130 mod format;
131 #[cfg(feature = "unstable_jiff_0_2")]
132 mod jiff;
133 mod neo;
134 pub mod options;
135 pub mod parts;
136 pub mod pattern;
137 pub mod provider;
138 /// Date and time range formatting.
139 #[cfg(feature = "unstable")]
140 pub mod range;
141 pub(crate) mod raw;
142 pub mod scaffold;
143 pub(crate) mod size_test_macro;
144 #[cfg(feature = "unstable_time_0_3")]
145 mod time_crate;
146 pub mod unchecked;
147
148 pub use error::{DateTimeFormatterLoadError, MismatchedCalendarError};
149
150 pub use neo::DateTimeFormatter;
151 pub use neo::DateTimeFormatterPreferences;
152 pub use neo::FixedCalendarDateTimeFormatter;
153 pub use neo::FormattedDateTime;
154 pub use neo::NoCalendarFormatter;
155
156 /// Locale preferences used by this crate
157 pub mod preferences {
158 /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
159 #[doc = "\n"] // prevent autoformatting
160 pub use icu_locale_core::preferences::extensions::unicode::keywords::CalendarAlgorithm;
161 /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
162 #[doc = "\n"] // prevent autoformatting
163 pub use icu_locale_core::preferences::extensions::unicode::keywords::HijriCalendarAlgorithm;
164 /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
165 #[doc = "\n"] // prevent autoformatting
166 pub use icu_locale_core::preferences::extensions::unicode::keywords::HourCycle;
167 /// **This is a reexport of a type in [`icu::locale`](icu_locale_core::preferences::extensions::unicode::keywords)**.
168 #[doc = "\n"] // prevent autoformatting
169 pub use icu_locale_core::preferences::extensions::unicode::keywords::NumberingSystem;
170 }
171
172 /// Types that can be fed to [`DateTimeFormatter`]/[`FixedCalendarDateTimeFormatter`].
173 ///
174 /// This module contains re-exports from the [`icu_calendar`] and [`icu_time`] crates.
175 pub mod input {
176 #[path = "third_party.rs"]
177 pub mod unstable_third_party;
178
179 pub use icu_calendar::Date;
180 pub use icu_time::DateTime;
181 pub use icu_time::Time;
182 pub use icu_time::TimeZone;
183 pub use icu_time::TimeZoneInfo;
184 pub use icu_time::ZonedDateTime;
185 pub use icu_time::zone::UtcOffset;
186
187 #[cfg(feature = "unstable")]
188 pub use icu_time::ZonedTime;
189 }