Debian Codegraph @

debian/missing-sources/jiff/src/tz/db/bundled/enabled.rs

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