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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// 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 ).
//! Support for third-party datetime libraries.
//!
//! ICU4X provides traits to allow using types from popular third-party datetime libraries
//! directly with ICU4X formatters like [`DateTimeFormatter`](crate::DateTimeFormatter).
//!
//! ✨ *To enable this support, the corresponding Cargo feature must be enabled:*
//!
//! - `unstable_chrono_0_4` for [chrono](https://crates.io/crates/chrono)
//! - `unstable_jiff_0_2` for [jiff](https://crates.io/crates/jiff)
//! - `unstable_time_0_3` for [time](https://crates.io/crates/time)
//!
//! <div class="stab unstable">
//! 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
//! including in SemVer minor releases. Use with caution.
//! </div>
//!
//! # Chrono
//!
//! The following examples show how to use [`chrono`] types with ICU4X formatters.
//!
//! ```
//! # #[cfg(feature = "unstable_chrono_0_4")] {
//! use icu::datetime::{fieldsets, DateTimeFormatter, NoCalendarFormatter};
//! use icu::locale::locale;
//! use writeable::assert_writeable_eq;
//!
//! // Format a chrono::NaiveDate using fieldsets::YMD and the Buddhist calendar (default in the `th-TH` locale)
//! let chrono_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
//! let dtf_date = DateTimeFormatter::try_new(
//! locale!("th-TH").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // 2025 CE is 2568 BE
//! assert_writeable_eq!(dtf_date.format(&chrono_date), "15 ม.ค. 2568");
//!
//! // Format a chrono::NaiveTime using fieldsets::T
//! let chrono_time = chrono::NaiveTime::from_hms_opt(16, 9, 35).unwrap();
//! let dtf_time = NoCalendarFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::T::medium(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_time.format(&chrono_time), "4:09:35 PM");
//!
//! // Format a chrono::NaiveDateTime using fieldsets::YMDT
//! let chrono_datetime = chrono_date.and_time(chrono_time);
//! let dtf_datetime = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMDT::medium(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_datetime.format(&chrono_datetime), "Jan 15, 2025, 4:09:35 PM");
//!
//! // Format a chrono::DateTime using fieldsets::YMDT with a zone style
//! let chrono_zoned = chrono_datetime.and_local_timezone(chrono::Utc).unwrap();
//! let dtf_zoned = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_zoned.format(&chrono_zoned), "Jan 15, 2025, 4:09:35 PM GMT+0");
//!
//! // Format a chrono::Weekday using fieldsets::E
//! let chrono_weekday = chrono::Weekday::Wed;
//! let dtf_weekday = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::E::long(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_weekday.format(&chrono_weekday), "Wednesday");
//! # }
//! ```
//!
//! Mismatched types and field sets will not compile:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_chrono_0_4")] {
//! use icu::datetime::{fieldsets, DateTimeFormatter};
//! use icu::locale::locale;
//! let chrono_time = chrono::NaiveTime::from_hms_opt(16, 9, 35).unwrap();
//! let dtf_date = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // NaiveTime does not have date fields required by YMD
//! dtf_date.format(&chrono_time);
//! # }
//! ```
//!
//! Note that [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter)
//! cannot be used directly with third-party types.
//!
//! While third-party types are implicitly Gregorian, ICU4X supports many other calendar
//! systems that users may prefer. To ensure correctness, [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter)
//! requires a type that explicitly carries its calendar system, which third-party types
//! do not. The following will not compile:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_chrono_0_4")] {
//! use icu::datetime::{fieldsets, FixedCalendarDateTimeFormatter};
//! use icu::locale::locale;
//!
//! let chrono_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
//! let dtf = FixedCalendarDateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // NaiveDate does not implement InFixedCalendar
//! dtf.format(&chrono_date);
//! # }
//! ```
//!
//! Similarly, [`DateTimeFormatter::format_same_calendar`](crate::DateTimeFormatter::format_same_calendar) will not compile because it
//! also requires the input type to explicitly carry its calendar system:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_chrono_0_4")] {
//! use icu::datetime::{fieldsets, DateTimeFormatter};
//! use icu::locale::locale;
//!
//! let chrono_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 15).unwrap();
//! let dtf = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // NaiveDate does not implement InSameCalendar
//! let _ = dtf.format_same_calendar(&chrono_date);
//! # }
//! ```
//!
//! # Jiff
//!
//! The following examples show how to use [`jiff`] types with ICU4X formatters.
//!
//! ```
//! # #[cfg(feature = "unstable_jiff_0_2")] {
//! use icu::datetime::{fieldsets, DateTimeFormatter, NoCalendarFormatter};
//! use icu::locale::locale;
//! use writeable::assert_writeable_eq;
//!
//! // Format a jiff::civil::Date using fieldsets::YMD and the Buddhist calendar (default in the `th-TH` locale)
//! let jiff_date = jiff::civil::date(2025, 1, 15);
//! let dtf_date = DateTimeFormatter::try_new(
//! locale!("th-TH").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // 2025 CE is 2568 BE
//! assert_writeable_eq!(dtf_date.format(&jiff_date), "15 ม.ค. 2568");
//!
//! // Format a jiff::civil::Time using fieldsets::T
//! let jiff_time = jiff::civil::time(16, 9, 35, 0);
//! let dtf_time = NoCalendarFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::T::medium(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_time.format(&jiff_time), "4:09:35 PM");
//!
//! // Format a jiff::civil::DateTime using fieldsets::YMDT
//! let jiff_datetime = jiff_date.at(16, 9, 35, 0);
//! let dtf_datetime = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMDT::medium(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_datetime.format(&jiff_datetime), "Jan 15, 2025, 4:09:35 PM");
//!
//! // Format a jiff::Zoned using fieldsets::YMDT with a zone style
//! let jiff_zoned = jiff_datetime.in_tz("UTC").unwrap();
//! let dtf_zoned = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_zoned.format(&jiff_zoned), "Jan 15, 2025, 4:09:35 PM GMT+0");
//!
//! // Format a jiff::civil::Weekday using fieldsets::E
//! let jiff_weekday = jiff::civil::Weekday::Wednesday;
//! let dtf_weekday = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::E::long(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_weekday.format(&jiff_weekday), "Wednesday");
//! # }
//! ```
//!
//! Mismatched types and field sets will not compile:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_jiff_0_2")] {
//! use icu::datetime::{fieldsets, NoCalendarFormatter};
//! use icu::locale::locale;
//! let jiff_date = jiff::civil::date(2025, 1, 15);
//! let dtf_time = NoCalendarFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::T::medium(),
//! )
//! .unwrap();
//! // civil::Date does not have time fields required by T
//! dtf_time.format(&jiff_date);
//! # }
//! ```
//!
//! Note that [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter)
//! cannot be used directly with third-party types.
//!
//! While third-party types are implicitly Gregorian, ICU4X supports many other calendar
//! systems that users may prefer. To ensure correctness, [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter)
//! requires a type that explicitly carries its calendar system, which third-party types
//! do not. The following will not compile:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_jiff_0_2")] {
//! use icu::datetime::{fieldsets, FixedCalendarDateTimeFormatter};
//! use icu::locale::locale;
//!
//! let jiff_date = jiff::civil::date(2025, 1, 15);
//! let dtf = FixedCalendarDateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // civil::Date does not implement InFixedCalendar
//! dtf.format(&jiff_date);
//! # }
//! ```
//!
//! Similarly, [`DateTimeFormatter::format_same_calendar`](crate::DateTimeFormatter::format_same_calendar) will not compile because it
//! also requires the input type to explicitly carry its calendar system:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_jiff_0_2")] {
//! use icu::datetime::{fieldsets, DateTimeFormatter};
//! use icu::locale::locale;
//!
//! let jiff_date = jiff::civil::date(2025, 1, 15);
//! let dtf = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // civil::Date does not implement InSameCalendar
//! let _ = dtf.format_same_calendar(&jiff_date);
//! # }
//! ```
//!
//! # Time
//!
//! The following examples show how to use [`time`] types with ICU4X formatters.
//!
//! ```
//! # #[cfg(feature = "unstable_time_0_3")] {
//! use icu::datetime::{fieldsets, DateTimeFormatter, NoCalendarFormatter};
//! use icu::locale::locale;
//! use writeable::assert_writeable_eq;
//!
//! // Format a time::Date using fieldsets::YMD and the Buddhist calendar (default in the `th-TH` locale)
//! let time_date = time::Date::from_calendar_date(2025, time::Month::January, 15).unwrap();
//! let dtf_date = DateTimeFormatter::try_new(
//! locale!("th-TH").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // 2025 CE is 2568 BE
//! assert_writeable_eq!(dtf_date.format(&time_date), "15 ม.ค. 2568");
//!
//! // Format a time::Time using fieldsets::T
//! let time_time = time::Time::from_hms(16, 9, 35).unwrap();
//! let dtf_time = NoCalendarFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::T::medium(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_time.format(&time_time), "4:09:35 PM");
//!
//! // Format a time::PrimitiveDateTime using fieldsets::YMDT
//! let time_datetime = time::PrimitiveDateTime::new(time_date, time_time);
//! let dtf_datetime = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMDT::medium(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_datetime.format(&time_datetime), "Jan 15, 2025, 4:09:35 PM");
//!
//! // Format a time::OffsetDateTime using fieldsets::YMDT with a zone style
//! let time_offset = time_datetime.assume_utc();
//! let dtf_zoned = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_zoned.format(&time_offset), "Jan 15, 2025, 4:09:35 PM GMT+0");
//!
//! // Format a time::Weekday using fieldsets::E
//! let time_weekday = time::Weekday::Wednesday;
//! let dtf_weekday = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::E::long(),
//! )
//! .unwrap();
//! assert_writeable_eq!(dtf_weekday.format(&time_weekday), "Wednesday");
//! # }
//! ```
//!
//! Mismatched types and field sets will not compile:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_time_0_3")] {
//! use icu::datetime::{fieldsets, DateTimeFormatter};
//! use icu::locale::locale;
//! let time_weekday = time::Weekday::Wednesday;
//! let dtf_date = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // Weekday does not have year/month/day fields required by YMD
//! dtf_date.format(&time_weekday);
//! # }
//! ```
//!
//! Note that [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter)
//! cannot be used directly with third-party types.
//!
//! While third-party types are implicitly Gregorian, ICU4X supports many other calendar
//! systems that users may prefer. To ensure correctness, [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter)
//! requires a type that explicitly carries its calendar system, which third-party types
//! do not. The following will not compile:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_time_0_3")] {
//! use icu::datetime::{fieldsets, FixedCalendarDateTimeFormatter};
//! use icu::locale::locale;
//!
//! let time_date = time::Date::from_calendar_date(2025, time::Month::January, 15).unwrap();
//! let dtf = FixedCalendarDateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // time::Date does not implement InFixedCalendar
//! dtf.format(&time_date);
//! # }
//! ```
//!
//! Similarly, [`DateTimeFormatter::format_same_calendar`](crate::DateTimeFormatter::format_same_calendar) will not compile because it
//! also requires the input type to explicitly carry its calendar system:
//!
//! ```compile_fail,E0277
//! # #[cfg(feature = "unstable_time_0_3")] {
//! use icu::datetime::{fieldsets, DateTimeFormatter};
//! use icu::locale::locale;
//!
//! let time_date = time::Date::from_calendar_date(2025, time::Month::January, 15).unwrap();
//! let dtf = DateTimeFormatter::try_new(
//! locale!("en-US").into(),
//! fieldsets::YMD::medium(),
//! )
//! .unwrap();
//! // time::Date does not implement InSameCalendar
//! let _ = dtf.format_same_calendar(&time_date);
//! # }
//! ```