icu_provider_source/time_zones/
mod.rs

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
// 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 crate::cldr_serde;
use crate::IterableDataProviderCached;
use crate::SourceDataProvider;
use icu::datetime::provider::time_zones::*;
use icu::timezone::provider::*;
use icu_locale_core::subtags::Region;
use icu_provider::prelude::*;
use std::collections::BTreeMap;
use std::collections::HashSet;
use tinystr::tinystr;

mod convert;
mod names;
mod windows;

// TODO: cache these
impl SourceDataProvider {
    /// Returns a map from time zone long identifier to time zone BCP-47 ID.
    ///
    /// For example: "America/Chicago" to "uschi"
    fn compute_bcp47_tzids_btreemap(&self) -> Result<BTreeMap<String, TimeZoneBcp47Id>, DataError> {
        let bcp47_tzids_resource = &self
            .cldr()?
            .bcp47()
            .read_and_parse::<cldr_serde::time_zones::bcp47_tzid::Resource>("timezone.json")?
            .keyword
            .u
            .time_zones
            .values;

        let mut bcp47_tzids = BTreeMap::new();
        for (bcp47_tzid, bcp47_tzid_data) in bcp47_tzids_resource.iter() {
            if let Some(alias) = &bcp47_tzid_data.alias {
                if bcp47_tzid.as_str() == "unk" {
                    // ignore the unknown time zone
                    continue;
                }
                for data_value in alias.split(' ') {
                    bcp47_tzids.insert(data_value.to_string(), *bcp47_tzid);
                }
            }
        }
        Ok(bcp47_tzids)
    }

    /// Returns a map from BCP-47 ID to a single canonical long identifier.
    ///
    /// For example: "inccu" to "Asia/Kolkata"
    fn compute_canonical_tzids_btreemap(
        &self,
    ) -> Result<BTreeMap<TimeZoneBcp47Id, String>, DataError> {
        let bcp47_tzids_resource = &self
            .cldr()?
            .bcp47()
            .read_and_parse::<cldr_serde::time_zones::bcp47_tzid::Resource>("timezone.json")?
            .keyword
            .u
            .time_zones
            .values;

        let mut canonical_tzids = BTreeMap::new();
        for (bcp47_tzid, bcp47_tzid_data) in bcp47_tzids_resource.iter() {
            if bcp47_tzid.as_str() == "unk" {
                // ignore the unknown time zone
            } else if Some(true) == bcp47_tzid_data.deprecated {
                // skip
            } else if let Some(iana) = &bcp47_tzid_data.iana {
                canonical_tzids.insert(*bcp47_tzid, iana.clone());
            } else if let Some(iana) = &bcp47_tzid_data
                .alias
                .as_ref()
                .and_then(|s| s.split(' ').next())
            {
                canonical_tzids.insert(*bcp47_tzid, String::from(*iana));
            } else {
                debug_assert!(
                    false,
                    "Could not find canonical IANA for bcp47 time zone: {bcp47_tzid:?}"
                );
            }
        }
        Ok(canonical_tzids)
    }

    /// Returns a map from metazone long identifier to metazone BCP-47 ID.
    ///
    /// For example: "America_Central" to "amce"
    fn compute_meta_zone_ids_btreemap(&self) -> Result<BTreeMap<String, MetazoneId>, DataError> {
        let meta_zone_ids_resource = &self
            .cldr()?
            .core()
            .read_and_parse::<cldr_serde::time_zones::meta_zones::Resource>(
                "supplemental/metaZones.json",
            )?
            .supplemental
            .meta_zones
            .meta_zone_ids
            .0;

        let mut meta_zone_ids = BTreeMap::new();
        for (meta_zone_id, meta_zone_id_data) in meta_zone_ids_resource.iter() {
            meta_zone_ids.insert(meta_zone_id_data.long_id.to_string(), *meta_zone_id);
        }
        // TODO(#1781): Remove this special case once the short id is updated in CLDR
        meta_zone_ids.insert("Yukon".to_owned(), MetazoneId(tinystr!(4, "yuko")));
        Ok(meta_zone_ids)
    }

    fn compute_primary_zones(&self) -> Result<BTreeMap<TimeZoneBcp47Id, Region>, DataError> {
        let primary_zones = self
            .cldr()?
            .core()
            .read_and_parse::<cldr_serde::time_zones::primary_zones::Resource>(
                "supplemental/primaryZones.json",
            )?
            .supplemental
            .primary_zones
            .iter()
            .map(|(&region, iana)| (iana, region))
            .collect::<BTreeMap<_, _>>();

        let bcp47_tzids = self.compute_canonical_tzids_btreemap()?;

        let zone_tab = self.tzdb()?.zone_tab()?;
        let mut zones_per_region: BTreeMap<icu::locale::subtags::Region, usize> = BTreeMap::new();

        for &region in bcp47_tzids.values().flat_map(|iana| zone_tab.get(iana)) {
            *zones_per_region.entry(region).or_default() += 1;
        }

        Ok(bcp47_tzids
            .iter()
            // Montreal is meant to be deprecated, but pre-43 the deprecation
            // fallback was not set, which is why it might show up here.
            .filter(|(bcp47, _)| bcp47.0 != "camtr")
            .filter_map(|(bcp47, canonical_iana)| {
                Some((
                    *bcp47,
                    primary_zones.get(canonical_iana).copied().or_else(|| {
                        let region = *zone_tab.get(canonical_iana)?;
                        if zones_per_region.get(&region).copied().unwrap_or_default() > 1 {
                            return None;
                        }
                        Some(region)
                    })?,
                ))
            })
            .collect())
    }
}

macro_rules! impl_iterable_data_provider {
    ($($marker:ident),+) => {
        $(
            impl IterableDataProviderCached<$marker> for SourceDataProvider {
                fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
                    Ok(self
                        .cldr()?
                        .dates("gregorian")
                        .list_locales()?
                        .map(DataIdentifierCow::from_locale)
                        .collect())
                }
            }
        )+
    };
}

impl_iterable_data_provider!(
    TimeZoneEssentialsV1Marker,
    LocationsV1Marker,
    MetazoneGenericNamesLongV1Marker,
    MetazoneGenericNamesShortV1Marker,
    MetazoneSpecificNamesLongV1Marker,
    MetazoneSpecificNamesShortV1Marker
);

impl IterableDataProviderCached<MetazonePeriodV1Marker> for SourceDataProvider {
    fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
        Ok(HashSet::from_iter([Default::default()]))
    }
}

impl IterableDataProviderCached<ZoneOffsetPeriodV1Marker> for SourceDataProvider {
    fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> {
        Ok(HashSet::from_iter([Default::default()]))
    }
}

#[cfg(test)]
mod tests {
    use icu::timezone::ZoneVariant;
    use tinystr::tinystr;

    use super::*;

    #[test]
    fn basic_cldr_time_zones() {
        use icu::locale::langid;

        let provider = SourceDataProvider::new_testing();

        let time_zone_formats: DataResponse<TimeZoneEssentialsV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("en").into()),
                ..Default::default()
            })
            .unwrap();
        assert_eq!("GMT", time_zone_formats.payload.get().offset_zero);
        assert_eq!("GMT+?", time_zone_formats.payload.get().offset_unknown);

        let locations_root: DataResponse<LocationsV1Marker> =
            provider.load(Default::default()).unwrap();
        assert_eq!(
            "Pohnpei",
            locations_root
                .payload
                .get()
                .locations
                .get(&TimeZoneBcp47Id(tinystr!(8, "fmpni")))
                .unwrap()
        );
        assert_eq!(
            "Ireland",
            locations_root
                .payload
                .get()
                .locations
                .get(&TimeZoneBcp47Id(tinystr!(8, "iedub")))
                .unwrap()
        );

        let locations: DataResponse<LocationsV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("fr").into()),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(
            "Italie",
            locations
                .payload
                .get()
                .locations
                .get(&TimeZoneBcp47Id(tinystr!(8, "itrom")))
                .unwrap()
        );

        let generic_names_long: DataResponse<MetazoneGenericNamesLongV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("en").into()),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(
            "Australian Central Western Time",
            generic_names_long
                .payload
                .get()
                .defaults
                .get(&MetazoneId(tinystr!(4, "aucw")))
                .unwrap()
        );
        assert_eq!(
            "Coordinated Universal Time",
            generic_names_long
                .payload
                .get()
                .overrides
                .get(&TimeZoneBcp47Id(tinystr!(8, "utc")))
                .unwrap()
        );

        let specific_names_long: DataResponse<MetazoneSpecificNamesLongV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("en").into()),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(
            "Australian Central Western Standard Time",
            specific_names_long
                .payload
                .get()
                .defaults
                .get(&(MetazoneId(tinystr!(4, "aucw")), ZoneVariant::Standard))
                .unwrap()
        );
        assert_eq!(
            "Coordinated Universal Time",
            specific_names_long
                .payload
                .get()
                .overrides
                .get(&(TimeZoneBcp47Id(tinystr!(8, "utc")), ZoneVariant::Standard))
                .unwrap()
        );

        let generic_names_short: DataResponse<MetazoneGenericNamesShortV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("en").into()),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(
            "PT",
            generic_names_short
                .payload
                .get()
                .defaults
                .get(&MetazoneId(tinystr!(4, "ampa")))
                .unwrap()
        );
        assert_eq!(
            "UTC",
            generic_names_short
                .payload
                .get()
                .overrides
                .get(&TimeZoneBcp47Id(tinystr!(8, "utc")))
                .unwrap()
        );

        let specific_names_short: DataResponse<MetazoneSpecificNamesShortV1Marker> = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(&langid!("en").into()),
                ..Default::default()
            })
            .unwrap();
        assert_eq!(
            "PDT",
            specific_names_short
                .payload
                .get()
                .defaults
                .get(&(MetazoneId(tinystr!(4, "ampa")), ZoneVariant::Daylight))
                .unwrap()
        );
        assert_eq!(
            "UTC",
            specific_names_short
                .payload
                .get()
                .overrides
                .get(&(TimeZoneBcp47Id(tinystr!(8, "utc")), ZoneVariant::Standard))
                .unwrap()
        );

        let metazone_period: DataResponse<MetazonePeriodV1Marker> =
            provider.load(Default::default()).unwrap();
        assert_eq!(
            Some(MetazoneId(tinystr!(4, "mgmt"))),
            metazone_period
                .payload
                .get()
                .0
                .get_copied_2d(&TimeZoneBcp47Id(tinystr!(8, "gblon")), &962040)
                .unwrap()
        );
    }
}