1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Formatting date and time.
//!
//! This module is published as its own crate ([`icu_datetime`](https://docs.rs/icu_datetime/latest/icu_datetime/))
//! and as part of the [`icu`](https://docs.rs/icu/latest/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>`](TypedDateTimeFormatter) and the
//! APIs will make sure that only Gregorian [`DateTime`]s 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(&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(
//!     &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(
//!     &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.
//!
//! [data provider]: icu_provider
//! [`ICU4X`]: ../icu/index.html
//! [`Length`]: options::length
//! [`DateTime`]: calendar::{DateTime}
//! [`Date`]: calendar::{Date}
//! [`Time`]: calendar::types::{Time}
//! [`Calendar`]: calendar::{Calendar}
//! [`AnyCalendar`]: calendar::any_calendar::{AnyCalendar}
//! [`timezone::CustomTimeZone`]: icu::timezone::{CustomTimeZone}
//! [`TimeZoneFormatter`]: time_zone::TimeZoneFormatter

// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![cfg_attr(
    not(test),
    deny(
        clippy::indexing_slicing,
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::panic,
        clippy::exhaustive_structs,
        clippy::exhaustive_enums,
        missing_debug_implementations,
    )
)]
#![warn(missing_docs)]

extern crate alloc;

mod calendar;
mod datetime;
mod error;
pub mod fields;
mod format;
pub mod input;
pub mod options;
#[doc(hidden)]
pub mod pattern;
pub mod provider;
pub(crate) mod raw;
#[doc(hidden)]
#[allow(clippy::exhaustive_structs, clippy::exhaustive_enums)] // private-ish module
#[cfg(any(feature = "datagen", feature = "experimental"))]
pub mod skeleton;
pub mod time_zone;
mod zoned_datetime;

mod any;

pub use any::{DateFormatter, DateTimeFormatter, ZonedDateTimeFormatter};
pub use calendar::CldrCalendar;
pub use datetime::{TimeFormatter, TypedDateFormatter, TypedDateTimeFormatter};
pub use error::DateTimeError;
pub use format::datetime::FormattedDateTime;
pub use format::time_zone::FormattedTimeZone;
pub use format::zoned_datetime::FormattedZonedDateTime;
pub use options::DateTimeFormatterOptions;
pub use zoned_datetime::TypedZonedDateTimeFormatter;

#[doc(no_inline)]
pub use DateTimeError as Error;

#[cfg(test)]
mod tests {
    use super::*;
    use core::mem::size_of;
    use icu_calendar::week::WeekCalculator;
    use icu_calendar::Gregorian;
    use icu_decimal::FixedDecimalFormatter;
    use icu_plurals::PluralRules;
    use icu_provider::prelude::*;
    use icu_timezone::CustomTimeZone;
    use provider::calendar::patterns::GenericPatternV1Marker;
    use provider::calendar::patterns::PatternPluralsFromPatternsV1Marker;
    use provider::calendar::ErasedDateSymbolsV1Marker;
    use provider::calendar::TimeSymbolsV1Marker;
    use provider::time_zones::ExemplarCitiesV1Marker;
    use provider::time_zones::MetazoneGenericNamesLongV1Marker;
    use provider::time_zones::MetazoneGenericNamesShortV1Marker;
    use provider::time_zones::MetazoneSpecificNamesLongV1Marker;
    use provider::time_zones::MetazoneSpecificNamesShortV1Marker;
    use provider::time_zones::TimeZoneFormatsV1Marker;
    use time_zone::TimeZoneDataPayloads;
    use time_zone::TimeZoneFormatter;
    use time_zone::TimeZoneFormatterUnit;

    /// Checks that the size of the type is one of the given sizes.
    /// The size might differ across Rust versions or channels.
    macro_rules! check_size_of {
        ($sizes:pat, $type:path) => {
            assert!(
                matches!(size_of::<$type>(), $sizes),
                concat!(stringify!($type), " is of size {}"),
                size_of::<$type>()
            );
        };
    }

    #[test]
    // TODO(#3413): Delete this test when no longer needed.
    #[ignore] // Changes too much across nightlies.
    fn check_sizes() {
        check_size_of!(4616, DateFormatter);
        check_size_of!(5488, DateTimeFormatter);
        check_size_of!(6536, ZonedDateTimeFormatter);
        check_size_of!(1344, TimeFormatter);
        check_size_of!(1048, TimeZoneFormatter);
        check_size_of!(4568, TypedDateFormatter::<Gregorian>);
        check_size_of!(5440, TypedDateTimeFormatter::<Gregorian>);

        check_size_of!(88, DateTimeError);
        check_size_of!(184, FormattedDateTime);
        check_size_of!(16, FormattedTimeZone::<CustomTimeZone>);
        check_size_of!(168, FormattedZonedDateTime);

        if cfg!(feature = "experimental") {
            check_size_of!(13, DateTimeFormatterOptions);
        }

        type DP<M> = DataPayload<M>;
        check_size_of!(216, DP::<PatternPluralsFromPatternsV1Marker>);
        check_size_of!(1032 | 912, DP::<TimeSymbolsV1Marker>);
        check_size_of!(40, DP::<GenericPatternV1Marker>);
        check_size_of!(216, DP::<PatternPluralsFromPatternsV1Marker>);
        check_size_of!(5064 | 3912, DP::<ErasedDateSymbolsV1Marker>);
        check_size_of!(232 | 240, DP::<TimeZoneFormatsV1Marker>);
        check_size_of!(64, DP::<ExemplarCitiesV1Marker>);
        check_size_of!(120 | 112, DP::<MetazoneGenericNamesLongV1Marker>);
        check_size_of!(120 | 112, DP::<MetazoneGenericNamesShortV1Marker>);
        check_size_of!(216 | 208, DP::<MetazoneSpecificNamesLongV1Marker>);
        check_size_of!(216 | 208, DP::<MetazoneSpecificNamesShortV1Marker>);
        check_size_of!(2, WeekCalculator);
        check_size_of!(176, PluralRules);
        check_size_of!(256 | 216, FixedDecimalFormatter);
        check_size_of!(1024 | 936, TimeZoneDataPayloads);
        check_size_of!(3, TimeZoneFormatterUnit);
    }
}