/rust/registry/src/index.crates.io-6f17d22bba15001f/icu_capi-1.5.1/src/timezone.rs
Line | Count | Source (jump to first uncovered line) |
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 | | use icu_timezone::CustomTimeZone; |
6 | | |
7 | 0 | #[diplomat::bridge] Unexecuted instantiation: ICU4XCustomTimeZone_create_from_string Unexecuted instantiation: ICU4XCustomTimeZone_create_empty Unexecuted instantiation: ICU4XCustomTimeZone_create_utc Unexecuted instantiation: ICU4XCustomTimeZone_try_set_gmt_offset_seconds Unexecuted instantiation: ICU4XCustomTimeZone_clear_gmt_offset Unexecuted instantiation: ICU4XCustomTimeZone_gmt_offset_seconds Unexecuted instantiation: ICU4XCustomTimeZone_is_gmt_offset_positive Unexecuted instantiation: ICU4XCustomTimeZone_is_gmt_offset_zero Unexecuted instantiation: ICU4XCustomTimeZone_gmt_offset_has_minutes Unexecuted instantiation: ICU4XCustomTimeZone_gmt_offset_has_seconds Unexecuted instantiation: ICU4XCustomTimeZone_try_set_time_zone_id Unexecuted instantiation: ICU4XCustomTimeZone_try_set_iana_time_zone_id Unexecuted instantiation: ICU4XCustomTimeZone_try_set_iana_time_zone_id_2 Unexecuted instantiation: ICU4XCustomTimeZone_clear_time_zone_id Unexecuted instantiation: ICU4XCustomTimeZone_time_zone_id Unexecuted instantiation: ICU4XCustomTimeZone_try_set_metazone_id Unexecuted instantiation: ICU4XCustomTimeZone_clear_metazone_id Unexecuted instantiation: ICU4XCustomTimeZone_metazone_id Unexecuted instantiation: ICU4XCustomTimeZone_try_set_zone_variant Unexecuted instantiation: ICU4XCustomTimeZone_clear_zone_variant Unexecuted instantiation: ICU4XCustomTimeZone_zone_variant Unexecuted instantiation: ICU4XCustomTimeZone_set_standard_time Unexecuted instantiation: ICU4XCustomTimeZone_set_daylight_time Unexecuted instantiation: ICU4XCustomTimeZone_is_standard_time Unexecuted instantiation: ICU4XCustomTimeZone_is_daylight_time Unexecuted instantiation: ICU4XCustomTimeZone_maybe_calculate_metazone Unexecuted instantiation: ICU4XCustomTimeZone_destroy |
8 | | pub mod ffi { |
9 | | use crate::errors::ffi::ICU4XError; |
10 | | use alloc::boxed::Box; |
11 | | use core::fmt::Write; |
12 | | use icu_timezone::CustomTimeZone; |
13 | | use icu_timezone::GmtOffset; |
14 | | use icu_timezone::ZoneVariant; |
15 | | |
16 | | #[diplomat::opaque] |
17 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone, Struct)] |
18 | | pub struct ICU4XCustomTimeZone(pub CustomTimeZone); |
19 | | |
20 | | impl ICU4XCustomTimeZone { |
21 | | /// Creates a time zone from an offset string. |
22 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::from_str, FnInStruct)] |
23 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::try_from_bytes, FnInStruct, hidden)] |
24 | | #[diplomat::rust_link(icu::timezone::GmtOffset::from_str, FnInStruct, hidden)] |
25 | | #[diplomat::rust_link(icu::timezone::GmtOffset::try_from_bytes, FnInStruct, hidden)] |
26 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "from_string")] |
27 | 0 | pub fn create_from_string(s: &DiplomatStr) -> Result<Box<ICU4XCustomTimeZone>, ICU4XError> { |
28 | 0 | Ok(Box::new(ICU4XCustomTimeZone::from( |
29 | 0 | CustomTimeZone::try_from_bytes(s)?, |
30 | | ))) |
31 | 0 | } |
32 | | |
33 | | /// Creates a time zone with no information. |
34 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::new_empty, FnInStruct)] |
35 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "empty")] |
36 | 0 | pub fn create_empty() -> Box<ICU4XCustomTimeZone> { |
37 | 0 | Box::new(CustomTimeZone::new_empty().into()) |
38 | 0 | } |
39 | | |
40 | | /// Creates a time zone for UTC. |
41 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::utc, FnInStruct)] |
42 | | #[diplomat::rust_link(icu::timezone::GmtOffset::utc, FnInStruct, hidden)] |
43 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "utc")] |
44 | 0 | pub fn create_utc() -> Box<ICU4XCustomTimeZone> { |
45 | 0 | Box::new(CustomTimeZone::utc().into()) |
46 | 0 | } |
47 | | |
48 | | /// Sets the `gmt_offset` field from offset seconds. |
49 | | /// |
50 | | /// Errors if the offset seconds are out of range. |
51 | | #[diplomat::rust_link(icu::timezone::GmtOffset::try_from_offset_seconds, FnInStruct)] |
52 | | #[diplomat::rust_link(icu::timezone::GmtOffset, Struct, compact)] |
53 | | #[diplomat::rust_link( |
54 | | icu::timezone::GmtOffset::from_offset_seconds_unchecked, |
55 | | FnInStruct, |
56 | | hidden |
57 | | )] |
58 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::new_with_offset, FnInStruct, hidden)] |
59 | 0 | pub fn try_set_gmt_offset_seconds( |
60 | 0 | &mut self, |
61 | 0 | offset_seconds: i32, |
62 | 0 | ) -> Result<(), ICU4XError> { |
63 | 0 | self.0.gmt_offset = Some(GmtOffset::try_from_offset_seconds(offset_seconds)?); |
64 | 0 | Ok(()) |
65 | 0 | } |
66 | | |
67 | | /// Clears the `gmt_offset` field. |
68 | | #[diplomat::rust_link(icu::timezone::GmtOffset::offset_seconds, FnInStruct)] |
69 | | #[diplomat::rust_link(icu::timezone::GmtOffset, Struct, compact)] |
70 | 0 | pub fn clear_gmt_offset(&mut self) { |
71 | 0 | self.0.gmt_offset.take(); |
72 | 0 | } |
73 | | |
74 | | /// Returns the value of the `gmt_offset` field as offset seconds. |
75 | | /// |
76 | | /// Errors if the `gmt_offset` field is empty. |
77 | | #[diplomat::rust_link(icu::timezone::GmtOffset::offset_seconds, FnInStruct)] |
78 | | #[diplomat::rust_link(icu::timezone::GmtOffset, Struct, compact)] |
79 | | #[diplomat::attr(supports = accessors, getter)] |
80 | 0 | pub fn gmt_offset_seconds(&self) -> Result<i32, ICU4XError> { |
81 | 0 | self.0 |
82 | 0 | .gmt_offset |
83 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError) |
84 | 0 | .map(GmtOffset::offset_seconds) |
85 | 0 | } |
86 | | |
87 | | /// Returns whether the `gmt_offset` field is positive. |
88 | | /// |
89 | | /// Errors if the `gmt_offset` field is empty. |
90 | | #[diplomat::rust_link(icu::timezone::GmtOffset::is_positive, FnInStruct)] |
91 | | #[diplomat::attr(supports = accessors, getter)] |
92 | 0 | pub fn is_gmt_offset_positive(&self) -> Result<bool, ICU4XError> { |
93 | 0 | self.0 |
94 | 0 | .gmt_offset |
95 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError) |
96 | 0 | .map(GmtOffset::is_positive) |
97 | 0 | } |
98 | | |
99 | | /// Returns whether the `gmt_offset` field is zero. |
100 | | /// |
101 | | /// Errors if the `gmt_offset` field is empty (which is not the same as zero). |
102 | | #[diplomat::rust_link(icu::timezone::GmtOffset::is_zero, FnInStruct)] |
103 | | #[diplomat::attr(supports = accessors, getter)] |
104 | 0 | pub fn is_gmt_offset_zero(&self) -> Result<bool, ICU4XError> { |
105 | 0 | self.0 |
106 | 0 | .gmt_offset |
107 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError) |
108 | 0 | .map(GmtOffset::is_zero) |
109 | 0 | } |
110 | | |
111 | | /// Returns whether the `gmt_offset` field has nonzero minutes. |
112 | | /// |
113 | | /// Errors if the `gmt_offset` field is empty. |
114 | | #[diplomat::rust_link(icu::timezone::GmtOffset::has_minutes, FnInStruct)] |
115 | | #[diplomat::attr(supports = accessors, getter)] |
116 | 0 | pub fn gmt_offset_has_minutes(&self) -> Result<bool, ICU4XError> { |
117 | 0 | self.0 |
118 | 0 | .gmt_offset |
119 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError) |
120 | 0 | .map(GmtOffset::has_minutes) |
121 | 0 | } |
122 | | |
123 | | /// Returns whether the `gmt_offset` field has nonzero seconds. |
124 | | /// |
125 | | /// Errors if the `gmt_offset` field is empty. |
126 | | #[diplomat::rust_link(icu::timezone::GmtOffset::has_seconds, FnInStruct)] |
127 | | #[diplomat::attr(supports = accessors, getter)] |
128 | 0 | pub fn gmt_offset_has_seconds(&self) -> Result<bool, ICU4XError> { |
129 | 0 | self.0 |
130 | 0 | .gmt_offset |
131 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError) |
132 | 0 | .map(GmtOffset::has_seconds) |
133 | 0 | } |
134 | | |
135 | | /// Sets the `time_zone_id` field from a BCP-47 string. |
136 | | /// |
137 | | /// Errors if the string is not a valid BCP-47 time zone ID. |
138 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::time_zone_id, StructField)] |
139 | | #[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id, Struct, compact)] |
140 | | #[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::from_str, FnInStruct, hidden)] |
141 | | #[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id::deref, FnInStruct, hidden)] |
142 | | #[diplomat::rust_link( |
143 | | icu::timezone::TimeZoneBcp47Id::Target, |
144 | | AssociatedTypeInStruct, |
145 | | hidden |
146 | | )] |
147 | 0 | pub fn try_set_time_zone_id(&mut self, id: &DiplomatStr) -> Result<(), ICU4XError> { |
148 | 0 | self.0.time_zone_id = Some(icu_timezone::TimeZoneBcp47Id( |
149 | 0 | tinystr::TinyAsciiStr::from_bytes(id) |
150 | 0 | .map_err(|_| ICU4XError::TimeZoneInvalidIdError)?, |
151 | | )); |
152 | 0 | Ok(()) |
153 | 0 | } |
154 | | |
155 | | /// Sets the `time_zone_id` field from an IANA string by looking up |
156 | | /// the corresponding BCP-47 string. |
157 | | /// |
158 | | /// Errors if the string is not a valid BCP-47 time zone ID. |
159 | | #[diplomat::rust_link(icu::timezone::IanaToBcp47MapperBorrowed::get, FnInStruct)] |
160 | 0 | pub fn try_set_iana_time_zone_id( |
161 | 0 | &mut self, |
162 | 0 | mapper: &crate::iana_bcp47_mapper::ffi::ICU4XIanaToBcp47Mapper, |
163 | 0 | id: &DiplomatStr, |
164 | 0 | ) -> Result<(), ICU4XError> { |
165 | 0 | let id = core::str::from_utf8(id).map_err(|_| ICU4XError::TimeZoneInvalidIdError)?; |
166 | | self.0.time_zone_id = Some( |
167 | 0 | mapper |
168 | 0 | .0 |
169 | 0 | .as_borrowed() |
170 | 0 | .get(id) |
171 | 0 | .ok_or(ICU4XError::TimeZoneInvalidIdError)?, |
172 | | ); |
173 | 0 | Ok(()) |
174 | 0 | } |
175 | | |
176 | | // *** TODO: in 2.0 please replace try_set_iana_time_zone_id with try_set_iana_time_zone_id_2 *** |
177 | | |
178 | | /// Sets the `time_zone_id` field from an IANA string by looking up |
179 | | /// the corresponding BCP-47 string. |
180 | | /// |
181 | | /// Errors if the string is not a valid BCP-47 time zone ID. |
182 | 0 | pub fn try_set_iana_time_zone_id_2( |
183 | 0 | &mut self, |
184 | 0 | mapper: &crate::timezone_mapper::ffi::ICU4XTimeZoneIdMapper, |
185 | 0 | id: &DiplomatStr, |
186 | 0 | ) -> Result<(), ICU4XError> { |
187 | 0 | self.0.time_zone_id = Some( |
188 | 0 | mapper |
189 | 0 | .0 |
190 | 0 | .as_borrowed() |
191 | 0 | .iana_bytes_to_bcp47(id) |
192 | 0 | .ok_or(ICU4XError::TimeZoneInvalidIdError)?, |
193 | | ); |
194 | 0 | Ok(()) |
195 | 0 | } |
196 | | |
197 | | /// Clears the `time_zone_id` field. |
198 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::time_zone_id, StructField)] |
199 | | #[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id, Struct, compact)] |
200 | 0 | pub fn clear_time_zone_id(&mut self) { |
201 | 0 | self.0.time_zone_id.take(); |
202 | 0 | } |
203 | | |
204 | | /// Writes the value of the `time_zone_id` field as a string. |
205 | | /// |
206 | | /// Errors if the `time_zone_id` field is empty. |
207 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::time_zone_id, StructField)] |
208 | | #[diplomat::rust_link(icu::timezone::TimeZoneBcp47Id, Struct, compact)] |
209 | | #[diplomat::attr(supports = accessors, getter)] |
210 | 0 | pub fn time_zone_id( |
211 | 0 | &self, |
212 | 0 | write: &mut diplomat_runtime::DiplomatWriteable, |
213 | 0 | ) -> Result<(), ICU4XError> { |
214 | 0 | write.write_str( |
215 | 0 | self.0 |
216 | 0 | .time_zone_id |
217 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError)? |
218 | | .0 |
219 | 0 | .as_str(), |
220 | 0 | )?; |
221 | 0 | Ok(()) |
222 | 0 | } |
223 | | |
224 | | /// Sets the `metazone_id` field from a string. |
225 | | /// |
226 | | /// Errors if the string is not a valid BCP-47 metazone ID. |
227 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::metazone_id, StructField)] |
228 | | #[diplomat::rust_link(icu::timezone::MetazoneId, Struct, compact)] |
229 | | #[diplomat::rust_link(icu::timezone::MetazoneId::from_str, FnInStruct, hidden)] |
230 | 0 | pub fn try_set_metazone_id(&mut self, id: &DiplomatStr) -> Result<(), ICU4XError> { |
231 | 0 | self.0.metazone_id = Some(icu_timezone::MetazoneId( |
232 | 0 | tinystr::TinyAsciiStr::from_bytes(id) |
233 | 0 | .map_err(|_| ICU4XError::TimeZoneInvalidIdError)?, |
234 | | )); |
235 | 0 | Ok(()) |
236 | 0 | } |
237 | | |
238 | | /// Clears the `metazone_id` field. |
239 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::metazone_id, StructField)] |
240 | | #[diplomat::rust_link(icu::timezone::MetazoneId, Struct, compact)] |
241 | 0 | pub fn clear_metazone_id(&mut self) { |
242 | 0 | self.0.metazone_id.take(); |
243 | 0 | } |
244 | | |
245 | | /// Writes the value of the `metazone_id` field as a string. |
246 | | /// |
247 | | /// Errors if the `metazone_id` field is empty. |
248 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::metazone_id, StructField)] |
249 | | #[diplomat::rust_link(icu::timezone::MetazoneId, Struct, compact)] |
250 | | #[diplomat::attr(supports = accessors, getter)] |
251 | 0 | pub fn metazone_id( |
252 | 0 | &self, |
253 | 0 | write: &mut diplomat_runtime::DiplomatWriteable, |
254 | 0 | ) -> Result<(), ICU4XError> { |
255 | 0 | write.write_str( |
256 | 0 | self.0 |
257 | 0 | .metazone_id |
258 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError)? |
259 | | .0 |
260 | 0 | .as_str(), |
261 | 0 | )?; |
262 | 0 | Ok(()) |
263 | 0 | } |
264 | | |
265 | | /// Sets the `zone_variant` field from a string. |
266 | | /// |
267 | | /// Errors if the string is not a valid zone variant. |
268 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField)] |
269 | | #[diplomat::rust_link(icu::timezone::ZoneVariant, Struct, compact)] |
270 | | #[diplomat::rust_link(icu::timezone::ZoneVariant::from_str, FnInStruct, hidden)] |
271 | 0 | pub fn try_set_zone_variant(&mut self, id: &DiplomatStr) -> Result<(), ICU4XError> { |
272 | 0 | self.0.zone_variant = Some(icu_timezone::ZoneVariant( |
273 | 0 | tinystr::TinyAsciiStr::from_bytes(id) |
274 | 0 | .map_err(|_| ICU4XError::TimeZoneInvalidIdError)?, |
275 | | )); |
276 | 0 | Ok(()) |
277 | 0 | } |
278 | | |
279 | | /// Clears the `zone_variant` field. |
280 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField)] |
281 | | #[diplomat::rust_link(icu::timezone::ZoneVariant, Struct, compact)] |
282 | 0 | pub fn clear_zone_variant(&mut self) { |
283 | 0 | self.0.zone_variant.take(); |
284 | 0 | } |
285 | | |
286 | | /// Writes the value of the `zone_variant` field as a string. |
287 | | /// |
288 | | /// Errors if the `zone_variant` field is empty. |
289 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField)] |
290 | | #[diplomat::rust_link(icu::timezone::ZoneVariant, Struct, compact)] |
291 | | #[diplomat::attr(supports = accessors, getter)] |
292 | 0 | pub fn zone_variant( |
293 | 0 | &self, |
294 | 0 | write: &mut diplomat_runtime::DiplomatWriteable, |
295 | 0 | ) -> Result<(), ICU4XError> { |
296 | 0 | write.write_str( |
297 | 0 | self.0 |
298 | 0 | .zone_variant |
299 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError)? |
300 | | .0 |
301 | 0 | .as_str(), |
302 | 0 | )?; |
303 | 0 | Ok(()) |
304 | 0 | } |
305 | | |
306 | | /// Sets the `zone_variant` field to "standard" time, which may or may |
307 | | /// not correspond to a display name with "Standard" in its name. |
308 | | #[diplomat::rust_link(icu::timezone::ZoneVariant::standard, FnInStruct)] |
309 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField, compact)] |
310 | 0 | pub fn set_standard_time(&mut self) { |
311 | 0 | self.0.zone_variant = Some(ZoneVariant::standard()) |
312 | 0 | } |
313 | | |
314 | | /// Sets the `zone_variant` field to "daylight" time, which may or may |
315 | | /// not correspond to a display name with "Daylight" in its name. |
316 | | #[diplomat::rust_link(icu::timezone::ZoneVariant::daylight, FnInStruct)] |
317 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField, compact)] |
318 | 0 | pub fn set_daylight_time(&mut self) { |
319 | 0 | self.0.zone_variant = Some(ZoneVariant::daylight()) |
320 | 0 | } |
321 | | |
322 | | /// Returns whether the `zone_variant` field is standard time. |
323 | | /// |
324 | | /// Errors if the `zone_variant` field is empty. |
325 | | #[diplomat::rust_link(icu::timezone::ZoneVariant::standard, FnInStruct)] |
326 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField, compact)] |
327 | | #[diplomat::attr(supports = accessors, getter)] |
328 | 0 | pub fn is_standard_time(&self) -> Result<bool, ICU4XError> { |
329 | 0 | Ok(self |
330 | 0 | .0 |
331 | 0 | .zone_variant |
332 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError)? |
333 | 0 | == ZoneVariant::standard()) |
334 | 0 | } |
335 | | |
336 | | /// Returns whether the `zone_variant` field is daylight time. |
337 | | /// |
338 | | /// Errors if the `zone_variant` field is empty. |
339 | | #[diplomat::rust_link(icu::timezone::ZoneVariant::daylight, FnInStruct)] |
340 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::zone_variant, StructField, compact)] |
341 | | #[diplomat::attr(supports = accessors, getter)] |
342 | 0 | pub fn is_daylight_time(&self) -> Result<bool, ICU4XError> { |
343 | 0 | Ok(self |
344 | 0 | .0 |
345 | 0 | .zone_variant |
346 | 0 | .ok_or(ICU4XError::TimeZoneMissingInputError)? |
347 | 0 | == ZoneVariant::daylight()) |
348 | 0 | } |
349 | | |
350 | | /// Sets the metazone based on the time zone and the local timestamp. |
351 | | #[diplomat::rust_link(icu::timezone::CustomTimeZone::maybe_calculate_metazone, FnInStruct)] |
352 | | #[diplomat::rust_link( |
353 | | icu::timezone::MetazoneCalculator::compute_metazone_from_time_zone, |
354 | | FnInStruct, |
355 | | compact |
356 | | )] |
357 | | #[cfg(feature = "icu_timezone")] |
358 | 0 | pub fn maybe_calculate_metazone( |
359 | 0 | &mut self, |
360 | 0 | metazone_calculator: &crate::metazone_calculator::ffi::ICU4XMetazoneCalculator, |
361 | 0 | local_datetime: &crate::datetime::ffi::ICU4XIsoDateTime, |
362 | 0 | ) { |
363 | 0 | self.0 |
364 | 0 | .maybe_calculate_metazone(&metazone_calculator.0, &local_datetime.0); |
365 | 0 | } |
366 | | } |
367 | | } |
368 | | |
369 | | impl From<CustomTimeZone> for ffi::ICU4XCustomTimeZone { |
370 | 0 | fn from(other: CustomTimeZone) -> Self { |
371 | 0 | Self(other) |
372 | 0 | } |
373 | | } |
374 | | |
375 | | impl From<ffi::ICU4XCustomTimeZone> for CustomTimeZone { |
376 | 0 | fn from(other: ffi::ICU4XCustomTimeZone) -> Self { |
377 | 0 | other.0 |
378 | 0 | } |
379 | | } |