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::calendar::ffi::Calendar; 13 use crate::date::ffi::{Date, IsoDate}; 14 use crate::errors::ffi::CalendarParseError; 15 use crate::iana_parser::ffi::IanaParser; 16 use crate::time::ffi::Time; 17 use crate::timezone::ffi::TimeZoneInfo; 18 use crate::utc_offset::ffi::UtcOffsetCalculator; 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_from_str, FnInStruct)] 32 #[diplomat::rust_link(icu::time::ZonedDateTime::try_from_utf8, FnInStruct, hidden)] 33 #[diplomat::attr(auto, named_constructor = "from_string")] from_string( v: &DiplomatStr, iana_parser: &IanaParser, offset_calculator: &UtcOffsetCalculator, ) -> Result<ZonedIsoDateTime, CalendarParseError>34 pub fn from_string( 35 v: &DiplomatStr, 36 iana_parser: &IanaParser, 37 offset_calculator: &UtcOffsetCalculator, 38 ) -> Result<ZonedIsoDateTime, CalendarParseError> { 39 let icu_time::ZonedDateTime { date, time, zone } = 40 icu_time::ZonedDateTime::try_from_utf8( 41 v, 42 Iso, 43 iana_parser.0.as_borrowed(), 44 &offset_calculator.0, 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 54 /// An ICU4X DateTime object capable of containing a date, time, and zone for any calendar. 55 #[diplomat::rust_link(icu::time::ZonedDateTime, Struct)] 56 #[diplomat::out] 57 pub struct ZonedDateTime { 58 pub date: Box<Date>, 59 pub time: Box<Time>, 60 pub zone: Box<TimeZoneInfo>, 61 } 62 63 impl ZonedDateTime { 64 /// Creates a new [`ZonedDateTime`] from an IXDTF string. 65 #[diplomat::rust_link(icu::time::ZonedDateTime::try_from_str, FnInStruct)] 66 #[diplomat::rust_link(icu::time::ZonedDateTime::try_from_utf8, FnInStruct, hidden)] 67 #[diplomat::attr(auto, named_constructor = "from_string")] from_string( v: &DiplomatStr, calendar: &Calendar, iana_parser: &IanaParser, offset_calculator: &UtcOffsetCalculator, ) -> Result<ZonedDateTime, CalendarParseError>68 pub fn from_string( 69 v: &DiplomatStr, 70 calendar: &Calendar, 71 iana_parser: &IanaParser, 72 offset_calculator: &UtcOffsetCalculator, 73 ) -> Result<ZonedDateTime, CalendarParseError> { 74 let icu_time::ZonedDateTime { date, time, zone } = 75 icu_time::ZonedDateTime::try_from_utf8( 76 v, 77 calendar.0.clone(), 78 iana_parser.0.as_borrowed(), 79 &offset_calculator.0, 80 )?; 81 Ok(ZonedDateTime { 82 date: Box::new(Date(date)), 83 time: Box::new(Time(time)), 84 zone: Box::new(TimeZoneInfo::from(zone)), 85 }) 86 } 87 88 /// Creates a new [`ZonedDateTime`] from a location-only IXDTF string. 89 #[diplomat::rust_link(icu::time::ZonedDateTime::try_location_only_from_str, FnInStruct)] 90 #[diplomat::rust_link( 91 icu::time::ZonedDateTime::try_location_only_from_utf8, 92 FnInStruct, 93 hidden 94 )] 95 #[diplomat::attr(auto, named_constructor = "location_only_from_string")] location_only_from_string( v: &DiplomatStr, calendar: &Calendar, iana_parser: &IanaParser, ) -> Result<ZonedDateTime, CalendarParseError>96 pub fn location_only_from_string( 97 v: &DiplomatStr, 98 calendar: &Calendar, 99 iana_parser: &IanaParser, 100 ) -> Result<ZonedDateTime, CalendarParseError> { 101 let icu_time::ZonedDateTime { date, time, zone } = 102 icu_time::ZonedDateTime::try_location_only_from_utf8( 103 v, 104 calendar.0.clone(), 105 iana_parser.0.as_borrowed(), 106 )?; 107 Ok(ZonedDateTime { 108 date: Box::new(Date(date)), 109 time: Box::new(Time(time)), 110 zone: Box::new(TimeZoneInfo::from(zone)), 111 }) 112 } 113 114 /// Creates a new [`ZonedDateTime`] from an offset-only IXDTF string. 115 #[diplomat::rust_link(icu::time::ZonedDateTime::try_offset_only_from_str, FnInStruct)] 116 #[diplomat::rust_link( 117 icu::time::ZonedDateTime::try_offset_only_from_utf8, 118 FnInStruct, 119 hidden 120 )] 121 #[diplomat::attr(auto, named_constructor = "offset_only_from_string")] offset_only_from_string( v: &DiplomatStr, calendar: &Calendar, ) -> Result<ZonedDateTime, CalendarParseError>122 pub fn offset_only_from_string( 123 v: &DiplomatStr, 124 calendar: &Calendar, 125 ) -> Result<ZonedDateTime, CalendarParseError> { 126 let icu_time::ZonedDateTime { date, time, zone } = 127 icu_time::ZonedDateTime::try_offset_only_from_utf8(v, calendar.0.clone())?; 128 Ok(ZonedDateTime { 129 date: Box::new(Date(date)), 130 time: Box::new(Time(time)), 131 zone: Box::new(TimeZoneInfo::from(zone)), 132 }) 133 } 134 135 /// Creates a new [`ZonedDateTime`] from an IXDTF string, without requiring the offset or calculating the zone variant. 136 #[diplomat::rust_link(icu::time::ZonedDateTime::try_loose_from_str, FnInStruct)] 137 #[diplomat::rust_link(icu::time::ZonedDateTime::try_loose_from_utf8, FnInStruct, hidden)] 138 #[diplomat::attr(auto, named_constructor = "loose_from_string")] loose_from_string( v: &DiplomatStr, calendar: &Calendar, iana_parser: &IanaParser, ) -> Result<ZonedDateTime, CalendarParseError>139 pub fn loose_from_string( 140 v: &DiplomatStr, 141 calendar: &Calendar, 142 iana_parser: &IanaParser, 143 ) -> Result<ZonedDateTime, CalendarParseError> { 144 let icu_time::ZonedDateTime { date, time, zone } = 145 icu_time::ZonedDateTime::try_loose_from_utf8( 146 v, 147 calendar.0.clone(), 148 iana_parser.0.as_borrowed(), 149 )?; 150 Ok(ZonedDateTime { 151 date: Box::new(Date(date)), 152 time: Box::new(Time(time)), 153 zone: Box::new(TimeZoneInfo::from(zone)), 154 }) 155 } 156 } 157 } 158