Coverage Report

Created: 2025-07-18 06:22

/rust/registry/src/index.crates.io-6f17d22bba15001f/icu_timezone-1.5.0/src/metazone.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 crate::provider::{MetazoneId, TimeZoneBcp47Id};
6
7
use crate::error::TimeZoneError;
8
use crate::provider::MetazonePeriodV1Marker;
9
use icu_calendar::DateTime;
10
use icu_calendar::Iso;
11
use icu_provider::prelude::*;
12
use zerovec::ule::AsULE;
13
14
/// [`MetazoneCalculator`] uses data from the [data provider] to calculate metazone id.
15
///
16
/// [data provider]: icu_provider
17
#[derive(Debug)]
18
pub struct MetazoneCalculator {
19
    pub(super) metazone_period: DataPayload<MetazonePeriodV1Marker>,
20
}
21
22
#[cfg(feature = "compiled_data")]
23
impl Default for MetazoneCalculator {
24
0
    fn default() -> Self {
25
0
        Self::new()
26
0
    }
27
}
28
29
impl MetazoneCalculator {
30
    /// Constructs a `MetazoneCalculator` using compiled data.
31
    ///
32
    /// ✨ *Enabled with the `compiled_data` Cargo feature.*
33
    ///
34
    /// [📚 Help choosing a constructor](icu_provider::constructors)
35
    #[cfg(feature = "compiled_data")]
36
    #[inline]
37
0
    pub const fn new() -> Self {
38
0
        MetazoneCalculator {
39
0
            metazone_period: DataPayload::from_static_ref(
40
0
                crate::provider::Baked::SINGLETON_TIME_ZONE_METAZONE_PERIOD_V1,
41
0
            ),
42
0
        }
43
0
    }
Unexecuted instantiation: <icu_timezone::metazone::MetazoneCalculator>::new
Unexecuted instantiation: <icu_timezone::metazone::MetazoneCalculator>::new
44
45
    icu_provider::gen_any_buffer_data_constructors!(locale: skip, options: skip, error: TimeZoneError,
46
        #[cfg(skip)]
47
        functions: [
48
            new,
49
            try_new_with_any_provider,
50
            try_new_with_buffer_provider,
51
            try_new_unstable,
52
            Self,
53
        ]
54
    );
55
56
    #[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::new)]
57
0
    pub fn try_new_unstable(
58
0
        provider: &(impl DataProvider<MetazonePeriodV1Marker> + ?Sized),
59
0
    ) -> Result<Self, TimeZoneError> {
60
0
        let metazone_period = provider.load(Default::default())?.take_payload()?;
61
0
        Ok(Self { metazone_period })
62
0
    }
Unexecuted instantiation: <icu_timezone::metazone::MetazoneCalculator>::try_new_unstable::<icu_provider::any::DowncastingAnyProvider<icu_provider_adapters::empty::EmptyDataProvider>>
Unexecuted instantiation: <icu_timezone::metazone::MetazoneCalculator>::try_new_unstable::<_>
63
64
    /// Calculate metazone id from timezone id and local datetime.
65
    ///
66
    /// # Examples
67
    ///
68
    /// ```
69
    /// use icu::calendar::DateTime;
70
    /// use icu::timezone::provider::{MetazoneId, TimeZoneBcp47Id};
71
    /// use icu::timezone::MetazoneCalculator;
72
    /// use tinystr::tinystr;
73
    ///
74
    /// let mzc = MetazoneCalculator::new();
75
    ///
76
    /// assert_eq!(
77
    ///     mzc.compute_metazone_from_time_zone(
78
    ///         TimeZoneBcp47Id(tinystr!(8, "gugum")),
79
    ///         &DateTime::try_new_iso_datetime(1969, 1, 1, 0, 0, 0).unwrap()
80
    ///     ),
81
    ///     None
82
    /// );
83
    ///
84
    /// assert_eq!(
85
    ///     mzc.compute_metazone_from_time_zone(
86
    ///         TimeZoneBcp47Id(tinystr!(8, "gugum")),
87
    ///         &DateTime::try_new_iso_datetime(1970, 1, 1, 0, 0, 0).unwrap()
88
    ///     ),
89
    ///     Some(MetazoneId(tinystr!(4, "guam")))
90
    /// );
91
    ///
92
    /// assert_eq!(
93
    ///     mzc.compute_metazone_from_time_zone(
94
    ///         TimeZoneBcp47Id(tinystr!(8, "gugum")),
95
    ///         &DateTime::try_new_iso_datetime(1975, 1, 1, 0, 0, 0).unwrap()
96
    ///     ),
97
    ///     Some(MetazoneId(tinystr!(4, "guam")))
98
    /// );
99
    ///
100
    /// assert_eq!(
101
    ///     mzc.compute_metazone_from_time_zone(
102
    ///         TimeZoneBcp47Id(tinystr!(8, "gugum")),
103
    ///         &DateTime::try_new_iso_datetime(2000, 12, 22, 15, 0, 0).unwrap()
104
    ///     ),
105
    ///     Some(MetazoneId(tinystr!(4, "cham")))
106
    /// );
107
    /// ```
108
0
    pub fn compute_metazone_from_time_zone(
109
0
        &self,
110
0
        time_zone_id: TimeZoneBcp47Id,
111
0
        local_datetime: &DateTime<Iso>,
112
0
    ) -> Option<MetazoneId> {
113
0
        match self.metazone_period.get().0.get0(&time_zone_id) {
114
0
            Some(cursor) => {
115
0
                let mut metazone_id = None;
116
0
                let minutes_since_local_unix_epoch =
117
0
                    local_datetime.minutes_since_local_unix_epoch();
118
0
                for (minutes, id) in cursor.iter1() {
119
0
                    if minutes_since_local_unix_epoch >= i32::from_unaligned(*minutes) {
120
0
                        metazone_id = id.get()
121
                    } else {
122
0
                        break;
123
                    }
124
                }
125
0
                metazone_id
126
            }
127
0
            None => None,
128
        }
129
0
    }
130
}