| 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 date libraries. |
| 6 |
//! |
| 7 |
//! ICU4X provides conversions from types from popular third-party date libraries |
| 8 |
//! into [`Date`](crate::Date). |
| 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 calendars. |
| 24 |
//! |
| 25 |
//! ``` |
| 26 |
//! # #[cfg(feature = "unstable_chrono_0_4")] { |
| 27 |
//! use icu::calendar::{Date, Gregorian, types::Weekday}; |
| 28 |
//! |
| 29 |
//! // Convert a chrono::NaiveDate into an ICU4X Date<Gregorian> |
| 30 |
//! let chrono_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 15).unwrap(); |
| 31 |
//! let icu_date = Date::<Gregorian>::from(chrono_date); |
| 32 |
//! assert_eq!(icu_date.era_year().year, 2025); |
| 33 |
//! |
| 34 |
//! // Convert a chrono::Weekday into an ICU4X Weekday |
| 35 |
//! let chrono_weekday = chrono::Weekday::Wed; |
| 36 |
//! let icu_weekday = Weekday::from(chrono_weekday); |
| 37 |
//! assert_eq!(icu_weekday, Weekday::Wednesday); |
| 38 |
//! # } |
| 39 |
//! ``` |
| 40 |
//! |
| 41 |
//! # Jiff |
| 42 |
//! |
| 43 |
//! The following examples show how to use [`jiff`] types with ICU4X calendars. |
| 44 |
//! |
| 45 |
//! ``` |
| 46 |
//! # #[cfg(feature = "unstable_jiff_0_2")] { |
| 47 |
//! use icu::calendar::{Date, Gregorian, types::Weekday}; |
| 48 |
//! |
| 49 |
//! // Convert a jiff::civil::Date into an ICU4X Date<Gregorian> |
| 50 |
//! let jiff_date = jiff::civil::date(2025, 1, 15); |
| 51 |
//! let icu_date = Date::<Gregorian>::from(jiff_date); |
| 52 |
//! assert_eq!(icu_date.era_year().year, 2025); |
| 53 |
//! |
| 54 |
//! // Convert a jiff::civil::Weekday into an ICU4X Weekday |
| 55 |
//! let jiff_weekday = jiff::civil::Weekday::Wednesday; |
| 56 |
//! let icu_weekday = Weekday::from(jiff_weekday); |
| 57 |
//! assert_eq!(icu_weekday, Weekday::Wednesday); |
| 58 |
//! # } |
| 59 |
//! ``` |
| 60 |
//! |
| 61 |
//! # Time |
| 62 |
//! |
| 63 |
//! The following examples show how to use [`time`] types with ICU4X calendars. |
| 64 |
//! |
| 65 |
//! ``` |
| 66 |
//! # #[cfg(feature = "unstable_time_0_3")] { |
| 67 |
//! use icu::calendar::{Date, Gregorian, types::Weekday}; |
| 68 |
//! |
| 69 |
//! // Convert a time::Date into an ICU4X Date<Gregorian> |
| 70 |
//! let time_date = |
| 71 |
//! time::Date::from_calendar_date(2025, time::Month::January, 15).unwrap(); |
| 72 |
//! let icu_date = Date::<Gregorian>::from(time_date); |
| 73 |
//! assert_eq!(icu_date.era_year().year, 2025); |
| 74 |
//! |
| 75 |
//! // Convert a time::Weekday into an ICU4X Weekday |
| 76 |
//! let time_weekday = time::Weekday::Wednesday; |
| 77 |
//! let icu_weekday = Weekday::from(time_weekday); |
| 78 |
//! assert_eq!(icu_weekday, Weekday::Wednesday); |
| 79 |
//! # } |
| 80 |
//! ``` |