zoned_datetime.rs (8052B)
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 #[diplomat::bridge] 6 #[diplomat::abi_rename = "icu4x_{0}_mv1"] 7 #[diplomat::attr(auto, namespace = "icu4x")] 8 pub mod ffi { 9 use alloc::boxed::Box; 10 use icu_calendar::Iso; 11 12 use crate::unstable::calendar::ffi::Calendar; 13 use crate::unstable::date::ffi::{Date, IsoDate}; 14 use crate::unstable::errors::ffi::Rfc9557ParseError; 15 use crate::unstable::iana_parser::ffi::IanaParser; 16 use crate::unstable::time::ffi::Time; 17 use crate::unstable::timezone::ffi::TimeZoneInfo; 18 use crate::unstable::variant_offset::ffi::{UtcOffset, VariantOffsetsCalculator}; 19 20 /// An ICU4X ZonedDateTime object capable of containing a ISO-8601 date, time, and zone. 21 #[diplomat::rust_link(icu::time::ZonedDateTime, Struct)] 22 #[diplomat::out] 23 pub struct ZonedIsoDateTime { 24 pub date: Box<IsoDate>, 25 pub time: Box<Time>, 26 pub zone: Box<TimeZoneInfo>, 27 } 28 29 impl ZonedIsoDateTime { 30 /// Creates a new [`ZonedIsoDateTime`] from an IXDTF string. 31 #[diplomat::rust_link(icu::time::ZonedDateTime::try_full_from_str, FnInStruct)] 32 #[diplomat::rust_link(icu::time::ZonedDateTime::try_full_from_utf8, FnInStruct, hidden)] 33 #[diplomat::attr(all(supports = named_constructors, supports = fallible_constructors), named_constructor = "full_from_string")] 34 pub fn full_from_string( 35 v: &DiplomatStr, 36 iana_parser: &IanaParser, 37 offset_calculator: &VariantOffsetsCalculator, 38 ) -> Result<ZonedIsoDateTime, Rfc9557ParseError> { 39 let icu_time::ZonedDateTime { date, time, zone } = 40 icu_time::ZonedDateTime::try_full_from_utf8( 41 v, 42 Iso, 43 iana_parser.0.as_borrowed(), 44 offset_calculator.0.as_borrowed(), 45 )?; 46 Ok(ZonedIsoDateTime { 47 date: Box::new(IsoDate(date)), 48 time: Box::new(Time(time)), 49 zone: Box::new(TimeZoneInfo::from(zone)), 50 }) 51 } 52 53 /// Creates a new [`ZonedIsoDateTime`] from milliseconds since epoch (timestamp) and a UTC offset. 54 /// 55 /// Note: [`ZonedIsoDateTime`]s created with this constructor can only be formatted using localized offset zone styles. 56 #[diplomat::rust_link( 57 icu::time::ZonedDateTime::from_epoch_milliseconds_and_utc_offset, 58 FnInStruct 59 )] 60 #[diplomat::attr(all(supports = named_constructors, supports = fallible_constructors), named_constructor = "from_epoch_milliseconds_and_utc_offset")] 61 pub fn from_epoch_milliseconds_and_utc_offset( 62 epoch_milliseconds: i64, 63 utc_offset: &UtcOffset, 64 ) -> ZonedIsoDateTime { 65 let zdt = icu_time::ZonedDateTime::from_epoch_milliseconds_and_utc_offset( 66 epoch_milliseconds, 67 utc_offset.0, 68 ); 69 ZonedIsoDateTime { 70 date: Box::new(IsoDate(zdt.date)), 71 time: Box::new(Time(zdt.time)), 72 zone: Box::new(TimeZoneInfo::from(utc_offset.0)), 73 } 74 } 75 } 76 77 /// An ICU4X DateTime object capable of containing a date, time, and zone for any calendar. 78 #[diplomat::rust_link(icu::time::ZonedDateTime, Struct)] 79 #[diplomat::out] 80 pub struct ZonedDateTime { 81 pub date: Box<Date>, 82 pub time: Box<Time>, 83 pub zone: Box<TimeZoneInfo>, 84 } 85 86 impl ZonedDateTime { 87 /// Creates a new [`ZonedDateTime`] from an IXDTF string. 88 #[diplomat::rust_link(icu::time::ZonedDateTime::try_full_from_str, FnInStruct)] 89 #[diplomat::rust_link(icu::time::ZonedDateTime::try_from_utf8, FnInStruct, hidden)] 90 #[diplomat::attr(all(supports = named_constructors, supports = fallible_constructors), named_constructor = "full_from_string")] 91 pub fn full_from_string( 92 v: &DiplomatStr, 93 calendar: &Calendar, 94 iana_parser: &IanaParser, 95 offset_calculator: &VariantOffsetsCalculator, 96 ) -> Result<ZonedDateTime, Rfc9557ParseError> { 97 let icu_time::ZonedDateTime { date, time, zone } = 98 icu_time::ZonedDateTime::try_full_from_utf8( 99 v, 100 calendar.0.clone(), 101 iana_parser.0.as_borrowed(), 102 offset_calculator.0.as_borrowed(), 103 )?; 104 Ok(ZonedDateTime { 105 date: Box::new(Date(date)), 106 time: Box::new(Time(time)), 107 zone: Box::new(TimeZoneInfo::from(zone)), 108 }) 109 } 110 111 /// Creates a new [`ZonedDateTime`] from a location-only IXDTF string. 112 #[diplomat::rust_link(icu::time::ZonedDateTime::try_location_only_from_str, FnInStruct)] 113 #[diplomat::rust_link( 114 icu::time::ZonedDateTime::try_location_only_from_utf8, 115 FnInStruct, 116 hidden 117 )] 118 #[diplomat::attr(all(supports = named_constructors, supports = fallible_constructors), named_constructor = "location_only_from_string")] 119 pub fn location_only_from_string( 120 v: &DiplomatStr, 121 calendar: &Calendar, 122 iana_parser: &IanaParser, 123 ) -> Result<ZonedDateTime, Rfc9557ParseError> { 124 let icu_time::ZonedDateTime { date, time, zone } = 125 icu_time::ZonedDateTime::try_location_only_from_utf8( 126 v, 127 calendar.0.clone(), 128 iana_parser.0.as_borrowed(), 129 )?; 130 Ok(ZonedDateTime { 131 date: Box::new(Date(date)), 132 time: Box::new(Time(time)), 133 zone: Box::new(TimeZoneInfo::from(zone)), 134 }) 135 } 136 137 /// Creates a new [`ZonedDateTime`] from an offset-only IXDTF string. 138 #[diplomat::rust_link(icu::time::ZonedDateTime::try_offset_only_from_str, FnInStruct)] 139 #[diplomat::rust_link( 140 icu::time::ZonedDateTime::try_offset_only_from_utf8, 141 FnInStruct, 142 hidden 143 )] 144 #[diplomat::attr(all(supports = named_constructors, supports = fallible_constructors), named_constructor = "offset_only_from_string")] 145 pub fn offset_only_from_string( 146 v: &DiplomatStr, 147 calendar: &Calendar, 148 ) -> Result<ZonedDateTime, Rfc9557ParseError> { 149 let icu_time::ZonedDateTime { date, time, zone } = 150 icu_time::ZonedDateTime::try_offset_only_from_utf8(v, calendar.0.clone())?; 151 Ok(ZonedDateTime { 152 date: Box::new(Date(date)), 153 time: Box::new(Time(time)), 154 zone: Box::new(TimeZoneInfo::from(zone)), 155 }) 156 } 157 158 /// Creates a new [`ZonedDateTime`] from an IXDTF string, without requiring the offset or calculating the zone variant. 159 #[diplomat::rust_link(icu::time::ZonedDateTime::try_lenient_from_str, FnInStruct)] 160 #[diplomat::rust_link(icu::time::ZonedDateTime::try_lenient_from_utf8, FnInStruct, hidden)] 161 #[diplomat::attr(all(supports = named_constructors, supports = fallible_constructors), named_constructor = "lenient_from_string")] 162 pub fn lenient_from_string( 163 v: &DiplomatStr, 164 calendar: &Calendar, 165 iana_parser: &IanaParser, 166 ) -> Result<ZonedDateTime, Rfc9557ParseError> { 167 let icu_time::ZonedDateTime { date, time, zone } = 168 icu_time::ZonedDateTime::try_lenient_from_utf8( 169 v, 170 calendar.0.clone(), 171 iana_parser.0.as_borrowed(), 172 )?; 173 Ok(ZonedDateTime { 174 date: Box::new(Date(date)), 175 time: Box::new(Time(time)), 176 zone: Box::new(TimeZoneInfo::from(zone)), 177 }) 178 } 179 } 180 }