1use crate::TimeZone;
53use crate::generated;
54use core::cmp::Ordering;
55
56pub const TZDATA_VERSION: &str = generated::TZDATA_VERSION;
59
60pub(crate) struct Table {
63 pub(crate) names: &'static str,
65 pub(crate) name_offsets: &'static [u16],
67 pub(crate) zone_string: &'static [u8],
69 pub(crate) strings: &'static str,
71 pub(crate) string_offsets: &'static [u16],
73}
74
75impl Table {
76 fn count(&self) -> usize {
77 self.name_offsets.len() - 1
78 }
79
80 fn name(&self, index: usize) -> Option<&'static str> {
81 let start = usize::from(*self.name_offsets.get(index)?);
82 let end = usize::from(*self.name_offsets.get(index + 1)?);
83 self.names.get(start..end - 1)
84 }
85
86 fn time_zone_string(&self, index: usize) -> Option<&'static str> {
87 let string = usize::from(*self.zone_string.get(index)?);
88 let start = usize::from(*self.string_offsets.get(string)?);
89 let end = usize::from(*self.string_offsets.get(string + 1)?);
90 self.strings.get(start..end - 1)
91 }
92
93 fn lookup(&self, name: &str) -> Option<usize> {
96 let mut low = 0;
97 let mut high = self.count();
98 while low < high {
99 let middle = (low + high) / 2;
100 match compare(self.name(middle)?.as_bytes(), name.as_bytes()) {
101 Ordering::Less => low = middle + 1,
102 Ordering::Greater => high = middle,
103 Ordering::Equal => return Some(middle),
104 }
105 }
106 None
107 }
108}
109
110const TABLES: &[&Table] = &[
112 #[cfg(all(feature = "iana-africa", not(feature = "iana-legacy-aliases")))]
113 &generated::africa::TABLE,
114 #[cfg(all(feature = "iana-africa", feature = "iana-legacy-aliases"))]
115 &generated::africa::TABLE_WITH_LEGACY_ALIASES,
116 #[cfg(all(feature = "iana-america", not(feature = "iana-legacy-aliases")))]
117 &generated::america::TABLE,
118 #[cfg(all(feature = "iana-america", feature = "iana-legacy-aliases"))]
119 &generated::america::TABLE_WITH_LEGACY_ALIASES,
120 #[cfg(all(feature = "iana-antarctica", not(feature = "iana-legacy-aliases")))]
121 &generated::antarctica::TABLE,
122 #[cfg(all(feature = "iana-antarctica", feature = "iana-legacy-aliases"))]
123 &generated::antarctica::TABLE_WITH_LEGACY_ALIASES,
124 #[cfg(all(feature = "iana-arctic", not(feature = "iana-legacy-aliases")))]
125 &generated::arctic::TABLE,
126 #[cfg(all(feature = "iana-arctic", feature = "iana-legacy-aliases"))]
127 &generated::arctic::TABLE_WITH_LEGACY_ALIASES,
128 #[cfg(all(feature = "iana-asia", not(feature = "iana-legacy-aliases")))]
129 &generated::asia::TABLE,
130 #[cfg(all(feature = "iana-asia", feature = "iana-legacy-aliases"))]
131 &generated::asia::TABLE_WITH_LEGACY_ALIASES,
132 #[cfg(all(feature = "iana-atlantic", not(feature = "iana-legacy-aliases")))]
133 &generated::atlantic::TABLE,
134 #[cfg(all(feature = "iana-atlantic", feature = "iana-legacy-aliases"))]
135 &generated::atlantic::TABLE_WITH_LEGACY_ALIASES,
136 #[cfg(all(feature = "iana-australia", not(feature = "iana-legacy-aliases")))]
137 &generated::australia::TABLE,
138 #[cfg(all(feature = "iana-australia", feature = "iana-legacy-aliases"))]
139 &generated::australia::TABLE_WITH_LEGACY_ALIASES,
140 #[cfg(all(feature = "iana-etc", not(feature = "iana-legacy-aliases")))]
141 &generated::etc::TABLE,
142 #[cfg(all(feature = "iana-etc", feature = "iana-legacy-aliases"))]
143 &generated::etc::TABLE_WITH_LEGACY_ALIASES,
144 #[cfg(all(feature = "iana-europe", not(feature = "iana-legacy-aliases")))]
145 &generated::europe::TABLE,
146 #[cfg(all(feature = "iana-europe", feature = "iana-legacy-aliases"))]
147 &generated::europe::TABLE_WITH_LEGACY_ALIASES,
148 #[cfg(all(feature = "iana-indian", not(feature = "iana-legacy-aliases")))]
149 &generated::indian::TABLE,
150 #[cfg(all(feature = "iana-indian", feature = "iana-legacy-aliases"))]
151 &generated::indian::TABLE_WITH_LEGACY_ALIASES,
152 #[cfg(all(feature = "iana-pacific", not(feature = "iana-legacy-aliases")))]
153 &generated::pacific::TABLE,
154 #[cfg(all(feature = "iana-pacific", feature = "iana-legacy-aliases"))]
155 &generated::pacific::TABLE_WITH_LEGACY_ALIASES,
156];
157
158pub fn zone_count() -> usize {
160 TABLES.iter().map(|table| table.count()).sum()
161}
162
163pub fn zone_name(index: usize) -> Option<&'static str> {
165 let mut index = index;
166 for table in TABLES {
167 if index < table.count() {
168 return table.name(index);
169 }
170 index -= table.count();
171 }
172 None
173}
174
175pub fn contains(name: &str) -> bool {
177 time_zone_string(name).is_some()
178}
179
180pub fn time_zone_string(name: &str) -> Option<&'static str> {
183 TABLES.iter().find_map(|table| {
184 let index = table.lookup(name)?;
185 table.time_zone_string(index)
186 })
187}
188
189fn compare(left: &[u8], right: &[u8]) -> Ordering {
191 let shared = left.len().min(right.len());
192 for index in 0..shared {
193 match left[index].cmp(&right[index]) {
194 Ordering::Equal => {}
195 other => return other,
196 }
197 }
198 left.len().cmp(&right.len())
199}
200
201impl TimeZone {
202 pub fn from_iana_name(name: &str) -> Option<TimeZone> {
208 TimeZone::parse(time_zone_string(name)?).ok()
209 }
210}
211
212#[cfg(test)]
213mod tests {
214 #[test]
215 fn lookups_follow_the_enabled_regions() {
216 let europe = cfg!(feature = "iana-europe");
217 assert_eq!(super::contains("Europe/Paris"), europe);
218 assert_eq!(super::time_zone_string("Europe/Paris").is_some(), europe);
219 if super::TABLES.is_empty() {
220 assert_eq!(super::zone_count(), 0);
221 assert_eq!(super::zone_name(0), None);
222 }
223 }
224
225 #[test]
226 fn each_table_is_strictly_sorted() {
227 for table in super::TABLES {
228 let mut previous: Option<&str> = None;
229 for index in 0..table.count() {
230 let name = table.name(index).unwrap();
231 if let Some(previous) = previous {
232 assert!(previous < name, "{previous} !< {name}");
233 }
234 previous = Some(name);
235 }
236 }
237 }
238
239 #[test]
240 fn utc_const_matches_parse() {
241 assert_eq!(
242 crate::TimeZone::utc(),
243 crate::TimeZone::parse("UTC0").unwrap()
244 );
245 }
246}