| 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 time libraries. |
| 6 |
//! |
| 7 |
//! ICU4X provides conversions from types from popular third-party time libraries |
| 8 |
//! into [`Time`](crate::Time), [`DateTime`](crate::DateTime), and [`ZonedDateTime`](crate::ZonedDateTime). |
| 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 time. |
| 24 |
//! |
| 25 |
//! ``` |
| 26 |
//! # #[cfg(feature = "unstable_chrono_0_4")] { |
| 27 |
//! use icu::calendar::Gregorian; |
| 28 |
//! use icu::time::{Time, DateTime, ZonedDateTime, zone::UtcOffset, zone::TimeZoneInfo, zone::models::AtTime}; |
| 29 |
//! |
| 30 |
//! // Convert a chrono::NaiveTime into an ICU4X Time |
| 31 |
//! let chrono_time = chrono::NaiveTime::from_hms_opt(16, 9, 35).unwrap(); |
| 32 |
//! let icu_time = Time::from(chrono_time); |
| 33 |
//! assert_eq!(u8::from(icu_time.hour), 16); |
| 34 |
//! |
| 35 |
//! // Convert a chrono::NaiveDateTime into an ICU4X DateTime<Gregorian> |
| 36 |
//! let chrono_date = chrono::NaiveDate::from_ymd_opt(2025, 1, 15).unwrap(); |
| 37 |
//! let chrono_datetime = chrono_date.and_time(chrono_time); |
| 38 |
//! let icu_datetime = DateTime::<Gregorian>::from(chrono_datetime); |
| 39 |
//! assert_eq!(icu_datetime.date.era_year().year, 2025); |
| 40 |
//! assert_eq!(u8::from(icu_datetime.time.hour), 16); |
| 41 |
//! |
| 42 |
//! // Convert a chrono::FixedOffset into an ICU4X UtcOffset |
| 43 |
//! let chrono_offset = chrono::FixedOffset::east_opt(3600).unwrap(); |
| 44 |
//! let icu_offset = UtcOffset::from(chrono_offset); |
| 45 |
//! assert_eq!(icu_offset.to_seconds(), 3600); |
| 46 |
//! |
| 47 |
//! // Convert a chrono::DateTime<Tz> into an ICU4X ZonedDateTime<Gregorian, TimeZoneInfo<AtTime>> |
| 48 |
//! let chrono_zoned = chrono_datetime.and_local_timezone(chrono::Utc).unwrap(); |
| 49 |
//! let icu_zoned = ZonedDateTime::<Gregorian, TimeZoneInfo<AtTime>>::from(&chrono_zoned); |
| 50 |
//! assert_eq!(icu_zoned.date.era_year().year, 2025); |
| 51 |
//! assert_eq!(u8::from(icu_zoned.time.hour), 16); |
| 52 |
//! assert_eq!(icu_zoned.zone.offset().unwrap().to_seconds(), 0); |
| 53 |
//! # } |
| 54 |
//! ``` |
| 55 |
//! |
| 56 |
//! # Jiff |
| 57 |
//! |
| 58 |
//! The following examples show how to use [`jiff`] types with ICU4X time. |
| 59 |
//! |
| 60 |
//! ``` |
| 61 |
//! # #[cfg(feature = "unstable_jiff_0_2")] { |
| 62 |
//! use icu::calendar::Gregorian; |
| 63 |
//! use icu::time::{Time, DateTime, ZonedDateTime, zone::UtcOffset, zone::TimeZoneInfo, zone::models::AtTime}; |
| 64 |
//! |
| 65 |
//! // Convert a jiff::civil::Time into an ICU4X Time |
| 66 |
//! let jiff_time = jiff::civil::time(16, 9, 35, 0); |
| 67 |
//! let icu_time = Time::from(jiff_time); |
| 68 |
//! assert_eq!(u8::from(icu_time.hour), 16); |
| 69 |
//! |
| 70 |
//! // Convert a jiff::civil::DateTime into an ICU4X DateTime<Gregorian> |
| 71 |
//! let jiff_date = jiff::civil::date(2025, 1, 15); |
| 72 |
//! let jiff_datetime = jiff_date.at(16, 9, 35, 0); |
| 73 |
//! let icu_datetime = DateTime::<Gregorian>::from(jiff_datetime); |
| 74 |
//! assert_eq!(icu_datetime.date.era_year().year, 2025); |
| 75 |
//! assert_eq!(u8::from(icu_datetime.time.hour), 16); |
| 76 |
//! |
| 77 |
//! // Convert a jiff::tz::Offset into an ICU4X UtcOffset |
| 78 |
//! let jiff_offset = jiff::tz::offset(1); |
| 79 |
//! let icu_offset = UtcOffset::from(jiff_offset); |
| 80 |
//! assert_eq!(icu_offset.to_seconds(), 3600); |
| 81 |
//! |
| 82 |
//! // Convert a jiff::Zoned into an ICU4X ZonedDateTime<Gregorian, TimeZoneInfo<AtTime>> |
| 83 |
//! # #[cfg(feature = "compiled_data")] { |
| 84 |
//! let jiff_zoned = jiff_datetime.to_zoned(jiff::tz::TimeZone::UTC).unwrap(); |
| 85 |
//! let icu_zoned = ZonedDateTime::<Gregorian, TimeZoneInfo<AtTime>>::from(&jiff_zoned); |
| 86 |
//! assert_eq!(icu_zoned.date.era_year().year, 2025); |
| 87 |
//! assert_eq!(u8::from(icu_zoned.time.hour), 16); |
| 88 |
//! assert_eq!(icu_zoned.zone.offset().unwrap().to_seconds(), 0); |
| 89 |
//! # } |
| 90 |
//! # } |
| 91 |
//! ``` |
| 92 |
//! |
| 93 |
//! # Time |
| 94 |
//! |
| 95 |
//! The following examples show how to use [`time`] types with ICU4X time. |
| 96 |
//! |
| 97 |
//! ``` |
| 98 |
//! # #[cfg(feature = "unstable_time_0_3")] { |
| 99 |
//! use icu::calendar::Gregorian; |
| 100 |
//! use icu::time::{Time, DateTime, ZonedDateTime, zone::UtcOffset}; |
| 101 |
//! |
| 102 |
//! // Convert a time::Time into an ICU4X Time |
| 103 |
//! let time_time = time::Time::from_hms(16, 9, 35).unwrap(); |
| 104 |
//! let icu_time = Time::from(time_time); |
| 105 |
//! assert_eq!(u8::from(icu_time.hour), 16); |
| 106 |
//! |
| 107 |
//! // Convert a time::PrimitiveDateTime into an ICU4X DateTime<Gregorian> |
| 108 |
//! let time_date = time::Date::from_calendar_date(2025, time::Month::January, 15).unwrap(); |
| 109 |
//! let time_datetime = time::PrimitiveDateTime::new(time_date, time_time); |
| 110 |
//! let icu_datetime = DateTime::<Gregorian>::from(time_datetime); |
| 111 |
//! assert_eq!(icu_datetime.date.era_year().year, 2025); |
| 112 |
//! assert_eq!(u8::from(icu_datetime.time.hour), 16); |
| 113 |
//! |
| 114 |
//! // Convert a time::UtcOffset into an ICU4X UtcOffset |
| 115 |
//! let time_offset = time::UtcOffset::from_hms(1, 0, 0).unwrap(); |
| 116 |
//! let icu_offset = UtcOffset::from(time_offset); |
| 117 |
//! assert_eq!(icu_offset.to_seconds(), 3600); |
| 118 |
//! |
| 119 |
//! // Convert a time::OffsetDateTime into an ICU4X ZonedDateTime<Gregorian, UtcOffset> |
| 120 |
//! let time_offset_dt = time_datetime.assume_utc(); |
| 121 |
//! let icu_zoned = ZonedDateTime::<Gregorian, UtcOffset>::from(&time_offset_dt); |
| 122 |
//! assert_eq!(icu_zoned.date.era_year().year, 2025); |
| 123 |
//! assert_eq!(u8::from(icu_zoned.time.hour), 16); |
| 124 |
//! assert_eq!(icu_zoned.zone.to_seconds(), 0); |
| 125 |
//! # } |
| 126 |
//! ``` |