icu_capi 2.2.0

C interface to ICU4X
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use ffi::TimeZoneInfo;

#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
pub mod ffi {
    use alloc::boxed::Box;

    use crate::unstable::{
        date::ffi::{Date, IsoDate},
        datetime::ffi::IsoDateTime,
        time::ffi::Time,
        variant_offset::ffi::UtcOffset,
    };

    #[diplomat::opaque]
    #[diplomat::rust_link(icu::time::TimeZone, Struct)]
    pub struct TimeZone(pub(crate) icu_time::TimeZone);

    impl TimeZone {
        /// The unknown time zone.
        #[diplomat::rust_link(icu::time::TimeZoneInfo::unknown, FnInStruct)]
        #[diplomat::rust_link(icu::time::TimeZone::unknown, FnInStruct, hidden)]
        #[diplomat::attr(auto, named_constructor)]
        pub fn unknown() -> Box<TimeZone> {
            Box::new(TimeZone(icu_time::TimeZone::UNKNOWN))
        }

        /// Whether the time zone is the unknown zone.
        #[diplomat::rust_link(icu::time::TimeZone::is_unknown, FnInStruct)]
        pub fn is_unknown(&self) -> bool {
            self.0.is_unknown()
        }

        /// Construct a [`TimeZone`] from an IANA time zone ID.
        #[diplomat::rust_link(icu::time::TimeZone::from_iana_id, FnInStruct)]
        #[diplomat::demo(default_constructor)]
        #[diplomat::attr(auto, named_constructor = "from_iana_id")]
        #[cfg(feature = "compiled_data")]
        pub fn create_from_iana_id<'a>(iana_id: &'a DiplomatStr) -> Box<Self> {
            Box::new(Self(
                icu_time::zone::IanaParser::new().parse_from_utf8(iana_id),
            ))
        }

        /// Construct a [`TimeZone`] from a Windows time zone ID and region.
        #[diplomat::rust_link(icu::time::TimeZone::from_windows_id, FnInStruct)]
        #[diplomat::attr(auto, named_constructor = "from_windows_id")]
        #[cfg(feature = "compiled_data")]
        pub fn create_from_windows_id<'a>(
            windows_id: &'a DiplomatStr,
            region: &'a DiplomatStr,
        ) -> Box<Self> {
            Box::new(Self(
                icu_time::zone::WindowsParser::new()
                    .parse_from_utf8(
                        windows_id,
                        icu_locale_core::subtags::Region::try_from_utf8(region).ok(),
                    )
                    .unwrap_or(icu_time::TimeZone::UNKNOWN),
            ))
        }

        /// Construct a [`TimeZone`] from the platform-specific ID.
        #[diplomat::rust_link(icu::time::TimeZone::from_system_id, FnInStruct)]
        #[diplomat::attr(auto, named_constructor = "from_system_id")]
        #[cfg(feature = "compiled_data")]
        pub fn create_from_system_id<'a>(
            id: &'a DiplomatStr,
            _region: &'a DiplomatStr,
        ) -> Box<Self> {
            #[cfg(target_os = "windows")]
            return Self::create_from_windows_id(id, _region);
            #[cfg(not(target_os = "windows"))]
            return Self::create_from_iana_id(id);
        }

        /// Creates a time zone from a BCP-47 string.
        ///
        /// Returns the unknown time zone if the string is not a valid BCP-47 subtag.
        #[diplomat::rust_link(icu::time::TimeZone, Struct, compact)]
        #[diplomat::attr(auto, named_constructor = "from_bcp47")]
        pub fn create_from_bcp47(id: &DiplomatStr) -> Box<Self> {
            icu_locale_core::subtags::Subtag::try_from_utf8(id)
                .ok()
                .map(icu_time::TimeZone)
                .map(TimeZone)
                .map(Box::new)
                .unwrap_or_else(Self::unknown)
        }

        #[diplomat::rust_link(icu::time::TimeZone::with_offset, FnInStruct)]
        pub fn with_offset(&self, offset: &UtcOffset) -> Box<TimeZoneInfo> {
            Box::new(self.0.with_offset(Some(offset.0)).into())
        }

        #[diplomat::rust_link(icu::time::TimeZone::without_offset, FnInStruct)]
        pub fn without_offset(&self) -> Box<TimeZoneInfo> {
            Box::new(self.0.without_offset().into())
        }
    }

    #[diplomat::enum_convert(icu_time::zone::TimeZoneVariant, needs_wildcard)]
    #[diplomat::rust_link(icu::time::zone::TimeZoneVariant, Enum)]
    #[non_exhaustive]
    #[deprecated(note = "type not needed anymore")]
    #[diplomat::attr(dart, disable)]
    pub enum TimeZoneVariant {
        // TimeZoneVariant in Rust doesn't have a default, but it is useful to have one
        // here for consistent behavior.
        #[diplomat::attr(auto, default)]
        Standard,
        Daylight,
    }

    #[allow(deprecated)]
    impl TimeZoneVariant {
        #[diplomat::rust_link(icu::time::zone::TimeZoneVariant::from_rearguard_isdst, FnInEnum)]
        #[diplomat::rust_link(icu::time::TimeZoneInfo::with_variant, FnInStruct)]
        #[deprecated(note = "type not needed anymore")]
        #[allow(deprecated)] // remove in 3.0
        pub fn from_rearguard_isdst(isdst: bool) -> Self {
            icu_time::zone::TimeZoneVariant::from_rearguard_isdst(isdst).into()
        }
    }

    #[diplomat::opaque]
    #[diplomat::rust_link(icu::time::TimeZoneInfo, Struct)]
    #[diplomat::rust_link(icu::time::zone::models::AtTime, Struct, hidden)]
    #[diplomat::rust_link(icu::time::zone::models::Base, Struct, hidden)]
    #[diplomat::rust_link(icu::time::zone::models::Full, Struct, hidden)]
    #[derive(Clone, Copy)]
    pub struct TimeZoneInfo {
        pub(crate) id: icu_time::TimeZone,
        pub(crate) offset: Option<icu_time::zone::UtcOffset>,
        pub(crate) zone_name_timestamp: Option<icu_time::zone::ZoneNameTimestamp>,
    }

    impl TimeZoneInfo {
        /// Creates a time zone for UTC (Coordinated Universal Time).
        #[diplomat::rust_link(icu::time::TimeZoneInfo::utc, FnInStruct)]
        #[diplomat::rust_link(icu::time::zone::UtcOffset::zero, FnInStruct, hidden)]
        #[diplomat::attr(auto, named_constructor)]
        pub fn utc() -> Box<TimeZoneInfo> {
            Box::new(icu_time::TimeZoneInfo::utc().into())
        }

        /// Creates a time zone info from parts.
        ///
        /// `variant` is ignored.
        #[diplomat::attr(auto, constructor)]
        #[allow(deprecated)]
        #[diplomat::attr(dart, disable)]
        pub fn from_parts(
            id: &TimeZone,
            offset: Option<&UtcOffset>,
            _variant: Option<TimeZoneVariant>,
        ) -> Box<TimeZoneInfo> {
            Box::new(Self {
                id: id.0,
                offset: offset.map(|o| o.0),
                zone_name_timestamp: None,
            })
        }

        #[diplomat::rust_link(icu::time::TimeZoneInfo::id, FnInStruct)]
        #[diplomat::attr(demo_gen, disable)] // this just returns a constructor argument
        #[diplomat::attr(auto, getter)]
        pub fn id(&self) -> Box<TimeZone> {
            Box::new(TimeZone(self.id))
        }

        /// Sets the datetime at which to interpret the time zone
        /// for display name lookup.
        ///
        /// Notes:
        ///
        /// - If not set, the formatting datetime is used if possible.
        /// - If the offset is not set, the datetime is interpreted as UTC.
        /// - The constraints are the same as with `ZoneNameTimestamp` in Rust.
        /// - Set to year 1000 or 9999 for a reference far in the past or future.
        #[diplomat::rust_link(icu::time::TimeZoneInfo::at_date_time, FnInStruct)]
        #[diplomat::rust_link(icu::time::zone::ZoneNameTimestamp, Struct, compact)]
        #[diplomat::rust_link(
            icu::time::TimeZoneInfo::with_zone_name_timestamp,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::from_date_time_iso,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::from_zoned_date_time_iso,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::far_in_future,
            FnInStruct,
            hidden
        )] // documented
        #[diplomat::rust_link(icu::time::zone::ZoneNameTimestamp::far_in_past, FnInStruct, hidden)] // documented
        pub fn at_date_time_iso(&self, date: &IsoDate, time: &Time) -> Box<Self> {
            Box::new(Self {
                zone_name_timestamp: Some(
                    self.id
                        .with_offset(self.offset)
                        .at_date_time(icu_time::DateTime {
                            date: date.0,
                            time: time.0,
                        })
                        .zone_name_timestamp(),
                ),
                ..*self
            })
        }

        /// Sets the datetime at which to interpret the time zone
        /// for display name lookup.
        ///
        /// Notes:
        ///
        /// - If not set, the formatting datetime is used if possible.
        /// - If the offset is not set, the datetime is interpreted as UTC.
        /// - The constraints are the same as with `ZoneNameTimestamp` in Rust.
        /// - Set to year 1000 or 9999 for a reference far in the past or future.
        #[diplomat::rust_link(icu::time::TimeZoneInfo::at_date_time, FnInStruct)]
        #[diplomat::rust_link(icu::time::zone::ZoneNameTimestamp, Struct, compact)]
        #[diplomat::rust_link(
            icu::time::TimeZoneInfo::with_zone_name_timestamp,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::from_date_time,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::from_zoned_date_time,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::far_in_future,
            FnInStruct,
            hidden
        )] // documented
        #[diplomat::rust_link(icu::time::zone::ZoneNameTimestamp::far_in_past, FnInStruct, hidden)] // documented
        pub fn at_date_time(&self, date: &Date, time: &Time) -> Box<Self> {
            Box::new(Self {
                zone_name_timestamp: Some(
                    self.id
                        .with_offset(self.offset)
                        .at_date_time(icu_time::DateTime {
                            date: date.0.clone(),
                            time: time.0,
                        })
                        .zone_name_timestamp(),
                ),
                ..*self
            })
        }

        /// Sets the timestamp, in milliseconds since Unix epoch, at which to interpret the time zone
        /// for display name lookup.
        ///
        /// Notes:
        ///
        /// - If not set, the formatting datetime is used if possible.
        /// - The constraints are the same as with `ZoneNameTimestamp` in Rust.
        #[diplomat::rust_link(icu::time::TimeZoneInfo::with_zone_name_timestamp, FnInStruct)]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::from_epoch_seconds,
            FnInStruct,
            compact
        )]
        pub fn at_timestamp(&self, timestamp: i64) -> Box<Self> {
            Box::new(Self {
                zone_name_timestamp: Some(icu_time::zone::ZoneNameTimestamp::from_epoch_seconds(
                    timestamp / 1000,
                )),
                ..*self
            })
        }

        #[diplomat::rust_link(icu::time::TimeZoneInfo::zone_name_timestamp, FnInStruct)]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::to_zoned_date_time_iso,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::time::zone::ZoneNameTimestamp::to_date_time_iso,
            FnInStruct,
            hidden
        )]
        #[diplomat::attr(auto, getter)]
        /// Returns the `DateTime` for the UTC zone name reference time
        pub fn zone_name_date_time(&self) -> Option<IsoDateTime> {
            let datetime = self.zone_name_timestamp?.to_zoned_date_time_iso();
            Some(IsoDateTime {
                date: Box::new(IsoDate(datetime.date)),
                time: Box::new(Time(datetime.time)),
            })
        }

        #[diplomat::rust_link(icu::time::TimeZoneInfo::with_variant, FnInStruct)]
        #[deprecated(note = "returns unmodified copy")]
        #[diplomat::attr(dart, disable)]
        #[allow(deprecated)]
        pub fn with_variant(&self, _time_variant: TimeZoneVariant) -> Box<Self> {
            Box::new(*self)
        }

        #[diplomat::attr(auto, getter)]
        #[diplomat::rust_link(icu::time::TimeZoneInfo::offset, FnInStruct)]
        pub fn offset(&self) -> Option<Box<UtcOffset>> {
            self.offset.map(UtcOffset).map(Box::new)
        }

        #[deprecated(note = "does nothing")]
        #[diplomat::attr(dart, disable)]
        #[diplomat::rust_link(icu::time::TimeZoneInfo::infer_variant, FnInStruct)]
        #[diplomat::rust_link(icu::time::zone::TimeZoneVariant, Enum, compact)]
        #[allow(deprecated)]
        pub fn infer_variant(
            &self,
            _offset_calculator: &crate::unstable::variant_offset::ffi::VariantOffsetsCalculator,
        ) -> Option<()> {
            Some(())
        }

        #[diplomat::rust_link(icu::time::TimeZoneInfo::variant, FnInStruct)]
        #[diplomat::attr(demo_gen, disable)] // this just returns a constructor argument
        #[deprecated(note = "always returns null")]
        #[diplomat::attr(dart, disable)]
        #[allow(deprecated)]
        pub fn variant(&self) -> Option<TimeZoneVariant> {
            None
        }
    }
}

impl ffi::TimeZoneInfo {
    pub(crate) fn as_rust_at_time<C: icu_calendar::Calendar>(
        &self,
        date: Option<icu_calendar::Date<C>>,
        time: Option<icu_time::Time>,
    ) -> icu_time::TimeZoneInfo<icu_time::zone::models::AtTime> {
        let base = self.id.with_offset(self.offset);
        if let Some(zone_name_timestamp) = self.zone_name_timestamp {
            base.with_zone_name_timestamp(zone_name_timestamp)
        } else if let Some(date) = date {
            base.at_date_time(icu_time::DateTime {
                date,
                time: time.unwrap_or(icu_time::Time::noon()),
            })
        } else {
            base.with_zone_name_timestamp(icu_time::zone::ZoneNameTimestamp::far_in_future())
        }
    }
}

impl From<icu_time::zone::UtcOffset> for TimeZoneInfo {
    fn from(other: icu_time::zone::UtcOffset) -> Self {
        Self {
            id: icu_time::TimeZone::UNKNOWN,
            offset: Some(other),
            zone_name_timestamp: None,
        }
    }
}

impl From<icu_time::TimeZoneInfo<icu_time::zone::models::Base>> for TimeZoneInfo {
    fn from(other: icu_time::TimeZoneInfo<icu_time::zone::models::Base>) -> Self {
        Self {
            id: other.id(),
            offset: other.offset(),
            zone_name_timestamp: None,
        }
    }
}

impl From<icu_time::TimeZoneInfo<icu_time::zone::models::AtTime>> for TimeZoneInfo {
    fn from(other: icu_time::TimeZoneInfo<icu_time::zone::models::AtTime>) -> Self {
        Self {
            id: other.id(),
            offset: other.offset(),
            zone_name_timestamp: Some(other.zone_name_timestamp()),
        }
    }
}

#[allow(deprecated)]
impl From<icu_time::TimeZoneInfo<icu_time::zone::models::Full>> for TimeZoneInfo {
    fn from(other: icu_time::TimeZoneInfo<icu_time::zone::models::Full>) -> Self {
        Self {
            id: other.id(),
            offset: other.offset(),
            zone_name_timestamp: Some(other.zone_name_timestamp()),
        }
    }
}