| 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 |
//! Support for third-party datetime libraries. |
| 6 |
//! |
| 7 |
//! ICU4X provides traits to allow using types from popular third-party datetime libraries |
| 8 |
//! directly with ICU4X formatters like [`DateTimeFormatter`](crate::DateTimeFormatter). |
| 9 |
//! |
| 10 |
//! ✨ *To enable this support, the corresponding Cargo feature must be enabled:* |
| 11 |
//! |
| 12 |
//! - `unstable_chrono_0_4` for [chrono](https://crates.io/crates/chrono) |
| 13 |
//! - `unstable_jiff_0_2` for [jiff](https://crates.io/crates/jiff) |
| 14 |
//! - `unstable_time_0_3` for [time](https://crates.io/crates/time) |
| 15 |
//! |
| 16 |
//! <div class="stab unstable"> |
| 17 |
//! 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 18 |
//! including in SemVer minor releases. Use with caution. |
| 19 |
//! </div> |
| 20 |
//! |
| 21 |
//! # Chrono |
| 22 |
//! |
| 23 |
//! The following examples show how to use [`chrono`] types with ICU4X formatters. |
| 24 |
//! |
| 25 |
//! ``` |
| 26 |
//! # #[cfg(feature = "unstable_chrono_0_4")] { |
| 27 |
//! use icu::datetime::{fieldsets, DateTimeFormatter, NoCalendarFormatter}; |
| 28 |
//! use icu::locale::locale; |
| 29 |
//! use writeable::assert_writeable_eq; |
| 30 |
//! |
| 31 |
//! // Format a chrono::NaiveDate using fieldsets::YMD and the Buddhist calendar (default in the `th-TH` locale) |
| 32 |
//! let chrono_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 15).unwrap(); |
| 33 |
//! let dtf_date = DateTimeFormatter::try_new( |
| 34 |
//! locale!("th-TH").into(), |
| 35 |
//! fieldsets::YMD::medium(), |
| 36 |
//! ) |
| 37 |
//! .unwrap(); |
| 38 |
//! // 2025 CE is 2568 BE |
| 39 |
//! assert_writeable_eq!(dtf_date.format(&chrono_date), "15 ม.ค. 2568"); |
| 40 |
//! |
| 41 |
//! // Format a chrono::NaiveTime using fieldsets::T |
| 42 |
//! let chrono_time = chrono::NaiveTime::from_hms_opt(16, 9, 35).unwrap(); |
| 43 |
//! let dtf_time = NoCalendarFormatter::try_new( |
| 44 |
//! locale!("en-US").into(), |
| 45 |
//! fieldsets::T::medium(), |
| 46 |
//! ) |
| 47 |
//! .unwrap(); |
| 48 |
//! assert_writeable_eq!(dtf_time.format(&chrono_time), "4:09:35 PM"); |
| 49 |
//! |
| 50 |
//! // Format a chrono::NaiveDateTime using fieldsets::YMDT |
| 51 |
//! let chrono_datetime = chrono_date.and_time(chrono_time); |
| 52 |
//! let dtf_datetime = DateTimeFormatter::try_new( |
| 53 |
//! locale!("en-US").into(), |
| 54 |
//! fieldsets::YMDT::medium(), |
| 55 |
//! ) |
| 56 |
//! .unwrap(); |
| 57 |
//! assert_writeable_eq!(dtf_datetime.format(&chrono_datetime), "Jan 15, 2025, 4:09:35 PM"); |
| 58 |
//! |
| 59 |
//! // Format a chrono::DateTime using fieldsets::YMDT with a zone style |
| 60 |
//! let chrono_zoned = chrono_datetime.and_local_timezone(chrono::Utc).unwrap(); |
| 61 |
//! let dtf_zoned = DateTimeFormatter::try_new( |
| 62 |
//! locale!("en-US").into(), |
| 63 |
//! fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort), |
| 64 |
//! ) |
| 65 |
//! .unwrap(); |
| 66 |
//! assert_writeable_eq!(dtf_zoned.format(&chrono_zoned), "Jan 15, 2025, 4:09:35 PM GMT+0"); |
| 67 |
//! |
| 68 |
//! // Format a chrono::Weekday using fieldsets::E |
| 69 |
//! let chrono_weekday = chrono::Weekday::Wed; |
| 70 |
//! let dtf_weekday = DateTimeFormatter::try_new( |
| 71 |
//! locale!("en-US").into(), |
| 72 |
//! fieldsets::E::long(), |
| 73 |
//! ) |
| 74 |
//! .unwrap(); |
| 75 |
//! assert_writeable_eq!(dtf_weekday.format(&chrono_weekday), "Wednesday"); |
| 76 |
//! # } |
| 77 |
//! ``` |
| 78 |
//! |
| 79 |
//! Mismatched types and field sets will not compile: |
| 80 |
//! |
| 81 |
//! ```compile_fail,E0277 |
| 82 |
//! # #[cfg(feature = "unstable_chrono_0_4")] { |
| 83 |
//! use icu::datetime::{fieldsets, DateTimeFormatter}; |
| 84 |
//! use icu::locale::locale; |
| 85 |
//! let chrono_time = chrono::NaiveTime::from_hms_opt(16, 9, 35).unwrap(); |
| 86 |
//! let dtf_date = DateTimeFormatter::try_new( |
| 87 |
//! locale!("en-US").into(), |
| 88 |
//! fieldsets::YMD::medium(), |
| 89 |
//! ) |
| 90 |
//! .unwrap(); |
| 91 |
//! // NaiveTime does not have date fields required by YMD |
| 92 |
//! dtf_date.format(&chrono_time); |
| 93 |
//! # } |
| 94 |
//! ``` |
| 95 |
//! |
| 96 |
//! Note that [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter) |
| 97 |
//! cannot be used directly with third-party types. |
| 98 |
//! |
| 99 |
//! While third-party types are implicitly Gregorian, ICU4X supports many other calendar |
| 100 |
//! systems that users may prefer. To ensure correctness, [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter) |
| 101 |
//! requires a type that explicitly carries its calendar system, which third-party types |
| 102 |
//! do not. The following will not compile: |
| 103 |
//! |
| 104 |
//! ```compile_fail,E0277 |
| 105 |
//! # #[cfg(feature = "unstable_chrono_0_4")] { |
| 106 |
//! use icu::datetime::{fieldsets, FixedCalendarDateTimeFormatter}; |
| 107 |
//! use icu::locale::locale; |
| 108 |
//! |
| 109 |
//! let chrono_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 15).unwrap(); |
| 110 |
//! let dtf = FixedCalendarDateTimeFormatter::try_new( |
| 111 |
//! locale!("en-US").into(), |
| 112 |
//! fieldsets::YMD::medium(), |
| 113 |
//! ) |
| 114 |
//! .unwrap(); |
| 115 |
//! // NaiveDate does not implement InFixedCalendar |
| 116 |
//! dtf.format(&chrono_date); |
| 117 |
//! # } |
| 118 |
//! ``` |
| 119 |
//! |
| 120 |
//! Similarly, [`DateTimeFormatter::format_same_calendar`](crate::DateTimeFormatter::format_same_calendar) will not compile because it |
| 121 |
//! also requires the input type to explicitly carry its calendar system: |
| 122 |
//! |
| 123 |
//! ```compile_fail,E0277 |
| 124 |
//! # #[cfg(feature = "unstable_chrono_0_4")] { |
| 125 |
//! use icu::datetime::{fieldsets, DateTimeFormatter}; |
| 126 |
//! use icu::locale::locale; |
| 127 |
//! |
| 128 |
//! let chrono_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 15).unwrap(); |
| 129 |
//! let dtf = DateTimeFormatter::try_new( |
| 130 |
//! locale!("en-US").into(), |
| 131 |
//! fieldsets::YMD::medium(), |
| 132 |
//! ) |
| 133 |
//! .unwrap(); |
| 134 |
//! // NaiveDate does not implement InSameCalendar |
| 135 |
//! let _ = dtf.format_same_calendar(&chrono_date); |
| 136 |
//! # } |
| 137 |
//! ``` |
| 138 |
//! |
| 139 |
//! # Jiff |
| 140 |
//! |
| 141 |
//! The following examples show how to use [`jiff`] types with ICU4X formatters. |
| 142 |
//! |
| 143 |
//! ``` |
| 144 |
//! # #[cfg(feature = "unstable_jiff_0_2")] { |
| 145 |
//! use icu::datetime::{fieldsets, DateTimeFormatter, NoCalendarFormatter}; |
| 146 |
//! use icu::locale::locale; |
| 147 |
//! use writeable::assert_writeable_eq; |
| 148 |
//! |
| 149 |
//! // Format a jiff::civil::Date using fieldsets::YMD and the Buddhist calendar (default in the `th-TH` locale) |
| 150 |
//! let jiff_date = jiff::civil::date(2025, 1, 15); |
| 151 |
//! let dtf_date = DateTimeFormatter::try_new( |
| 152 |
//! locale!("th-TH").into(), |
| 153 |
//! fieldsets::YMD::medium(), |
| 154 |
//! ) |
| 155 |
//! .unwrap(); |
| 156 |
//! // 2025 CE is 2568 BE |
| 157 |
//! assert_writeable_eq!(dtf_date.format(&jiff_date), "15 ม.ค. 2568"); |
| 158 |
//! |
| 159 |
//! // Format a jiff::civil::Time using fieldsets::T |
| 160 |
//! let jiff_time = jiff::civil::time(16, 9, 35, 0); |
| 161 |
//! let dtf_time = NoCalendarFormatter::try_new( |
| 162 |
//! locale!("en-US").into(), |
| 163 |
//! fieldsets::T::medium(), |
| 164 |
//! ) |
| 165 |
//! .unwrap(); |
| 166 |
//! assert_writeable_eq!(dtf_time.format(&jiff_time), "4:09:35 PM"); |
| 167 |
//! |
| 168 |
//! // Format a jiff::civil::DateTime using fieldsets::YMDT |
| 169 |
//! let jiff_datetime = jiff_date.at(16, 9, 35, 0); |
| 170 |
//! let dtf_datetime = DateTimeFormatter::try_new( |
| 171 |
//! locale!("en-US").into(), |
| 172 |
//! fieldsets::YMDT::medium(), |
| 173 |
//! ) |
| 174 |
//! .unwrap(); |
| 175 |
//! assert_writeable_eq!(dtf_datetime.format(&jiff_datetime), "Jan 15, 2025, 4:09:35 PM"); |
| 176 |
//! |
| 177 |
//! // Format a jiff::Zoned using fieldsets::YMDT with a zone style |
| 178 |
//! let jiff_zoned = jiff_datetime.in_tz("UTC").unwrap(); |
| 179 |
//! let dtf_zoned = DateTimeFormatter::try_new( |
| 180 |
//! locale!("en-US").into(), |
| 181 |
//! fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort), |
| 182 |
//! ) |
| 183 |
//! .unwrap(); |
| 184 |
//! assert_writeable_eq!(dtf_zoned.format(&jiff_zoned), "Jan 15, 2025, 4:09:35 PM GMT+0"); |
| 185 |
//! |
| 186 |
//! // Format a jiff::civil::Weekday using fieldsets::E |
| 187 |
//! let jiff_weekday = jiff::civil::Weekday::Wednesday; |
| 188 |
//! let dtf_weekday = DateTimeFormatter::try_new( |
| 189 |
//! locale!("en-US").into(), |
| 190 |
//! fieldsets::E::long(), |
| 191 |
//! ) |
| 192 |
//! .unwrap(); |
| 193 |
//! assert_writeable_eq!(dtf_weekday.format(&jiff_weekday), "Wednesday"); |
| 194 |
//! # } |
| 195 |
//! ``` |
| 196 |
//! |
| 197 |
//! Mismatched types and field sets will not compile: |
| 198 |
//! |
| 199 |
//! ```compile_fail,E0277 |
| 200 |
//! # #[cfg(feature = "unstable_jiff_0_2")] { |
| 201 |
//! use icu::datetime::{fieldsets, NoCalendarFormatter}; |
| 202 |
//! use icu::locale::locale; |
| 203 |
//! let jiff_date = jiff::civil::date(2025, 1, 15); |
| 204 |
//! let dtf_time = NoCalendarFormatter::try_new( |
| 205 |
//! locale!("en-US").into(), |
| 206 |
//! fieldsets::T::medium(), |
| 207 |
//! ) |
| 208 |
//! .unwrap(); |
| 209 |
//! // civil::Date does not have time fields required by T |
| 210 |
//! dtf_time.format(&jiff_date); |
| 211 |
//! # } |
| 212 |
//! ``` |
| 213 |
//! |
| 214 |
//! Note that [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter) |
| 215 |
//! cannot be used directly with third-party types. |
| 216 |
//! |
| 217 |
//! While third-party types are implicitly Gregorian, ICU4X supports many other calendar |
| 218 |
//! systems that users may prefer. To ensure correctness, [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter) |
| 219 |
//! requires a type that explicitly carries its calendar system, which third-party types |
| 220 |
//! do not. The following will not compile: |
| 221 |
//! |
| 222 |
//! ```compile_fail,E0277 |
| 223 |
//! # #[cfg(feature = "unstable_jiff_0_2")] { |
| 224 |
//! use icu::datetime::{fieldsets, FixedCalendarDateTimeFormatter}; |
| 225 |
//! use icu::locale::locale; |
| 226 |
//! |
| 227 |
//! let jiff_date = jiff::civil::date(2025, 1, 15); |
| 228 |
//! let dtf = FixedCalendarDateTimeFormatter::try_new( |
| 229 |
//! locale!("en-US").into(), |
| 230 |
//! fieldsets::YMD::medium(), |
| 231 |
//! ) |
| 232 |
//! .unwrap(); |
| 233 |
//! // civil::Date does not implement InFixedCalendar |
| 234 |
//! dtf.format(&jiff_date); |
| 235 |
//! # } |
| 236 |
//! ``` |
| 237 |
//! |
| 238 |
//! Similarly, [`DateTimeFormatter::format_same_calendar`](crate::DateTimeFormatter::format_same_calendar) will not compile because it |
| 239 |
//! also requires the input type to explicitly carry its calendar system: |
| 240 |
//! |
| 241 |
//! ```compile_fail,E0277 |
| 242 |
//! # #[cfg(feature = "unstable_jiff_0_2")] { |
| 243 |
//! use icu::datetime::{fieldsets, DateTimeFormatter}; |
| 244 |
//! use icu::locale::locale; |
| 245 |
//! |
| 246 |
//! let jiff_date = jiff::civil::date(2025, 1, 15); |
| 247 |
//! let dtf = DateTimeFormatter::try_new( |
| 248 |
//! locale!("en-US").into(), |
| 249 |
//! fieldsets::YMD::medium(), |
| 250 |
//! ) |
| 251 |
//! .unwrap(); |
| 252 |
//! // civil::Date does not implement InSameCalendar |
| 253 |
//! let _ = dtf.format_same_calendar(&jiff_date); |
| 254 |
//! # } |
| 255 |
//! ``` |
| 256 |
//! |
| 257 |
//! # Time |
| 258 |
//! |
| 259 |
//! The following examples show how to use [`time`] types with ICU4X formatters. |
| 260 |
//! |
| 261 |
//! ``` |
| 262 |
//! # #[cfg(feature = "unstable_time_0_3")] { |
| 263 |
//! use icu::datetime::{fieldsets, DateTimeFormatter, NoCalendarFormatter}; |
| 264 |
//! use icu::locale::locale; |
| 265 |
//! use writeable::assert_writeable_eq; |
| 266 |
//! |
| 267 |
//! // Format a time::Date using fieldsets::YMD and the Buddhist calendar (default in the `th-TH` locale) |
| 268 |
//! let time_date = time::Date::from_calendar_date(2025, time::Month::January, 15).unwrap(); |
| 269 |
//! let dtf_date = DateTimeFormatter::try_new( |
| 270 |
//! locale!("th-TH").into(), |
| 271 |
//! fieldsets::YMD::medium(), |
| 272 |
//! ) |
| 273 |
//! .unwrap(); |
| 274 |
//! // 2025 CE is 2568 BE |
| 275 |
//! assert_writeable_eq!(dtf_date.format(&time_date), "15 ม.ค. 2568"); |
| 276 |
//! |
| 277 |
//! // Format a time::Time using fieldsets::T |
| 278 |
//! let time_time = time::Time::from_hms(16, 9, 35).unwrap(); |
| 279 |
//! let dtf_time = NoCalendarFormatter::try_new( |
| 280 |
//! locale!("en-US").into(), |
| 281 |
//! fieldsets::T::medium(), |
| 282 |
//! ) |
| 283 |
//! .unwrap(); |
| 284 |
//! assert_writeable_eq!(dtf_time.format(&time_time), "4:09:35 PM"); |
| 285 |
//! |
| 286 |
//! // Format a time::PrimitiveDateTime using fieldsets::YMDT |
| 287 |
//! let time_datetime = time::PrimitiveDateTime::new(time_date, time_time); |
| 288 |
//! let dtf_datetime = DateTimeFormatter::try_new( |
| 289 |
//! locale!("en-US").into(), |
| 290 |
//! fieldsets::YMDT::medium(), |
| 291 |
//! ) |
| 292 |
//! .unwrap(); |
| 293 |
//! assert_writeable_eq!(dtf_datetime.format(&time_datetime), "Jan 15, 2025, 4:09:35 PM"); |
| 294 |
//! |
| 295 |
//! // Format a time::OffsetDateTime using fieldsets::YMDT with a zone style |
| 296 |
//! let time_offset = time_datetime.assume_utc(); |
| 297 |
//! let dtf_zoned = DateTimeFormatter::try_new( |
| 298 |
//! locale!("en-US").into(), |
| 299 |
//! fieldsets::YMDT::medium().with_zone(fieldsets::zone::LocalizedOffsetShort), |
| 300 |
//! ) |
| 301 |
//! .unwrap(); |
| 302 |
//! assert_writeable_eq!(dtf_zoned.format(&time_offset), "Jan 15, 2025, 4:09:35 PM GMT+0"); |
| 303 |
//! |
| 304 |
//! // Format a time::Weekday using fieldsets::E |
| 305 |
//! let time_weekday = time::Weekday::Wednesday; |
| 306 |
//! let dtf_weekday = DateTimeFormatter::try_new( |
| 307 |
//! locale!("en-US").into(), |
| 308 |
//! fieldsets::E::long(), |
| 309 |
//! ) |
| 310 |
//! .unwrap(); |
| 311 |
//! assert_writeable_eq!(dtf_weekday.format(&time_weekday), "Wednesday"); |
| 312 |
//! # } |
| 313 |
//! ``` |
| 314 |
//! |
| 315 |
//! Mismatched types and field sets will not compile: |
| 316 |
//! |
| 317 |
//! ```compile_fail,E0277 |
| 318 |
//! # #[cfg(feature = "unstable_time_0_3")] { |
| 319 |
//! use icu::datetime::{fieldsets, DateTimeFormatter}; |
| 320 |
//! use icu::locale::locale; |
| 321 |
//! let time_weekday = time::Weekday::Wednesday; |
| 322 |
//! let dtf_date = DateTimeFormatter::try_new( |
| 323 |
//! locale!("en-US").into(), |
| 324 |
//! fieldsets::YMD::medium(), |
| 325 |
//! ) |
| 326 |
//! .unwrap(); |
| 327 |
//! // Weekday does not have year/month/day fields required by YMD |
| 328 |
//! dtf_date.format(&time_weekday); |
| 329 |
//! # } |
| 330 |
//! ``` |
| 331 |
//! |
| 332 |
//! Note that [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter) |
| 333 |
//! cannot be used directly with third-party types. |
| 334 |
//! |
| 335 |
//! While third-party types are implicitly Gregorian, ICU4X supports many other calendar |
| 336 |
//! systems that users may prefer. To ensure correctness, [`FixedCalendarDateTimeFormatter`](crate::FixedCalendarDateTimeFormatter) |
| 337 |
//! requires a type that explicitly carries its calendar system, which third-party types |
| 338 |
//! do not. The following will not compile: |
| 339 |
//! |
| 340 |
//! ```compile_fail,E0277 |
| 341 |
//! # #[cfg(feature = "unstable_time_0_3")] { |
| 342 |
//! use icu::datetime::{fieldsets, FixedCalendarDateTimeFormatter}; |
| 343 |
//! use icu::locale::locale; |
| 344 |
//! |
| 345 |
//! let time_date = time::Date::from_calendar_date(2025, time::Month::January, 15).unwrap(); |
| 346 |
//! let dtf = FixedCalendarDateTimeFormatter::try_new( |
| 347 |
//! locale!("en-US").into(), |
| 348 |
//! fieldsets::YMD::medium(), |
| 349 |
//! ) |
| 350 |
//! .unwrap(); |
| 351 |
//! // time::Date does not implement InFixedCalendar |
| 352 |
//! dtf.format(&time_date); |
| 353 |
//! # } |
| 354 |
//! ``` |
| 355 |
//! |
| 356 |
//! Similarly, [`DateTimeFormatter::format_same_calendar`](crate::DateTimeFormatter::format_same_calendar) will not compile because it |
| 357 |
//! also requires the input type to explicitly carry its calendar system: |
| 358 |
//! |
| 359 |
//! ```compile_fail,E0277 |
| 360 |
//! # #[cfg(feature = "unstable_time_0_3")] { |
| 361 |
//! use icu::datetime::{fieldsets, DateTimeFormatter}; |
| 362 |
//! use icu::locale::locale; |
| 363 |
//! |
| 364 |
//! let time_date = time::Date::from_calendar_date(2025, time::Month::January, 15).unwrap(); |
| 365 |
//! let dtf = DateTimeFormatter::try_new( |
| 366 |
//! locale!("en-US").into(), |
| 367 |
//! fieldsets::YMD::medium(), |
| 368 |
//! ) |
| 369 |
//! .unwrap(); |
| 370 |
//! // time::Date does not implement InSameCalendar |
| 371 |
//! let _ = dtf.format_same_calendar(&time_date); |
| 372 |
//! # } |
| 373 |
//! ``` |