Debian Codegraph @

debian/rust-vendor/jiff/src/tz/db/bundled/enabled.rs

raw
1 use crate::tz::{db::special_time_zone, TimeZone, TimeZoneNameIter};
2
3 #[derive(Clone)]
4 pub(crate) struct Database;
5
6 impl Database {
7 pub(crate) const fn new() -> Database {
8 Database
9 }
10
11 pub(crate) fn reset(&self) {
12 #[cfg(feature = "std")]
13 self::global::clear();
14 }
15
16 pub(crate) fn get(&self, name: &str) -> Option<TimeZone> {
17 #[cfg(feature = "std")]
18 if let Some(tz) = self::global::get(name) {
19 return Some(tz);
20 }
21 if let Some(tz) = special_time_zone(name) {
22 return Some(tz);
23 }
24 let (canonical_name, tzif) = lookup(name)?;
25 let tz = match TimeZone::tzif(canonical_name, tzif) {
26 Ok(tz) => tz,
27 Err(_err) => {
28 warn!(
29 "failed to parse TZif data from bundled \
30 tzdb for time zone `{canonical_name}` \
31 (this is likely a bug, please report it): {_err}"
32 );
33 return None;
34 }
35 };
36 #[cfg(feature = "std")]
37 self::global::add(canonical_name, &tz);
38 Some(tz)
39 }
40
41 pub(crate) fn available<'d>(&'d self) -> TimeZoneNameIter<'d> {
42 TimeZoneNameIter::from_iter(available())
43 }
44
45 pub(crate) fn is_definitively_empty(&self) -> bool {
46 false
47 }
48 }
49
50 impl core::fmt::Debug for Database {
51 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
52 f.write_str("Bundled(available)")
53 }
54 }
55
56 fn available() -> impl Iterator<Item = &'static str> {
57 #[cfg(feature = "tzdb-bundle-always")]
58 {
59 jiff_tzdb::available()
60 }
61 #[cfg(not(feature = "tzdb-bundle-always"))]
62 {
63 jiff_tzdb_platform::jiff_tzdb::available()
64 }
65 }
66
67 fn lookup(name: &str) -> Option<(&'static str, &'static [u8])> {
68 #[cfg(feature = "tzdb-bundle-always")]
69 {
70 jiff_tzdb::get(name)
71 }
72 #[cfg(not(feature = "tzdb-bundle-always"))]
73 {
74 jiff_tzdb_platform::jiff_tzdb::get(name)
75 }
76 }
77
78 /// A simple global cache of parsed time zones.
79 ///
80 /// When tzdb is bundled, all of the binary data is in the binary. But in order
81 /// to use it, it needs to be parsed. And parsing takes non-trivial work.
82 ///
83 /// When std is enabled, we can keep a global cache of parsed time zones.
84 /// Currently, it is very simple and can never contract. This probably isn't
85 /// a big deal in practice since even if all time zones are loaded into memory,
86 /// its total memory usage is likely insignificant in most environments where
87 /// `std` is supported.
88 #[cfg(feature = "std")]
89 mod global {
90 use std::{string::String, string::ToString, sync::RwLock, vec::Vec};
91
92 use crate::{tz::TimeZone, util::utf8};
93
94 static CACHED_ZONES: RwLock<CachedZones> =
95 RwLock::new(CachedZones { zones: Vec::new() });
96
97 /// Returns a cached time zone for the given name if it exists.
98 ///
99 /// If the time zone isn't in the cache, then this returns `None`.
100 pub(super) fn get(name: &str) -> Option<TimeZone> {
101 CACHED_ZONES.read().unwrap().get(name).cloned()
102 }
103
104 /// Associates the given time zone name with the given time zone in this
105 /// cache.
106 ///
107 /// If the given time zone is already cached, then this is a no-op.
108 ///
109 /// The only way a time zone can be remove from the cache is if it's
110 /// overwritten or if the cache is cleared entirely.
111 pub(super) fn add(name: &str, tz: &TimeZone) {
112 let mut cache = CACHED_ZONES.write().unwrap();
113 if let Err(i) = cache.get_zone_index(name) {
114 cache.zones.insert(
115 i,
116 CachedZone { name: name.to_string(), tz: tz.clone() },
117 );
118 }
119 }
120
121 /// Clear the entire global cache.
122 pub(super) fn clear() {
123 CACHED_ZONES.write().unwrap().clear();
124 }
125
126 #[derive(Debug)]
127 struct CachedZones {
128 zones: Vec<CachedZone>,
129 }
130
131 impl CachedZones {
132 fn get(&self, query: &str) -> Option<&TimeZone> {
133 self.get_zone_index(query).ok().map(|i| &self.zones[i].tz)
134 }
135
136 fn get_zone_index(&self, query: &str) -> Result<usize, usize> {
137 self.zones.binary_search_by(|entry| {
138 utf8::cmp_ignore_ascii_case(&entry.name, query)
139 })
140 }
141
142 fn clear(&mut self) {
143 self.zones.clear();
144 }
145 }
146
147 #[derive(Debug)]
148 struct CachedZone {
149 name: String,
150 tz: TimeZone,
151 }
152 }