polars_core/datatypes/temporal/
time_zone.rs1use polars_error::{PolarsResult, polars_bail};
2use polars_utils::pl_str::PlSmallStr;
3
4use crate::config;
5
6#[derive(Clone, Hash, PartialEq, Eq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
9pub struct TimeZone {
10 inner: PlSmallStr,
12}
13
14impl TimeZone {
15 pub const UTC: TimeZone = unsafe { TimeZone::from_static("UTC") };
16
17 #[inline(always)]
23 pub const unsafe fn from_static(tz: &'static str) -> Self {
24 Self {
25 inner: PlSmallStr::from_static(tz),
26 }
27 }
28
29 pub unsafe fn new_unchecked(zone_str: impl Into<PlSmallStr>) -> Self {
33 Self {
34 inner: zone_str.into(),
35 }
36 }
37
38 #[inline]
43 pub fn opt_try_new(zone_str: Option<impl Into<PlSmallStr>>) -> PolarsResult<Option<Self>> {
44 Self::new_impl(zone_str.map(|x| x.into()))
45 }
46
47 fn new_impl(zone_str: Option<PlSmallStr>) -> PolarsResult<Option<Self>> {
48 if zone_str.as_deref() == Some("*") {
50 return Ok(Some(Self {
51 inner: PlSmallStr::from_static("*"),
52 }));
53 }
54
55 let mut canonical_tz = Self::_canonical_timezone_impl(zone_str);
56
57 #[cfg(feature = "timezones")]
58 if let Some(tz) = canonical_tz.as_mut() {
59 if let Err(err) = Self::validate_time_zone(tz) {
60 match parse_fixed_offset(tz) {
61 Ok(v) => *tz = v,
62 Err(_) => {
63 if std::env::var("POLARS_IGNORE_TIMEZONE_PARSE_ERROR").as_deref() == Ok("1")
66 {
67 if config::verbose() {
68 eprintln!("WARN: {err}")
69 }
70 } else {
71 return Err(err.wrap_msg(|s| {
72 format!(
73 "{s}\n\nIf you would like to forcibly disable \
74 timezone validation, set \
75 POLARS_IGNORE_TIMEZONE_PARSE_ERROR=1.",
76 )
77 }));
78 }
79 },
80 }
81 }
82 }
83
84 Ok(canonical_tz.map(|inner| Self { inner }))
85 }
86
87 pub fn eq_none_as_utc(this: Option<&TimeZone>, other: Option<&TimeZone>) -> bool {
89 this.unwrap_or(&Self::UTC) == other.unwrap_or(&Self::UTC)
90 }
91
92 pub fn _canonical_timezone_impl(tz: Option<PlSmallStr>) -> Option<PlSmallStr> {
93 match tz.as_deref() {
94 Some("") | None => None,
95 #[cfg(feature = "timezones")]
96 Some("+00:00") | Some("00:00") | Some("utc") => Some(PlSmallStr::from_static("UTC")),
97 Some(_) => tz,
98 }
99 }
100
101 #[cfg(feature = "timezones")]
102 pub fn from_chrono(tz: &chrono_tz::Tz) -> Self {
103 use polars_utils::format_pl_smallstr;
104
105 Self {
106 inner: format_pl_smallstr!("{}", tz),
107 }
108 }
109
110 #[cfg(feature = "timezones")]
111 pub fn to_chrono(&self) -> PolarsResult<chrono_tz::Tz> {
112 parse_time_zone(self)
113 }
114
115 #[cfg(feature = "timezones")]
116 pub fn validate_time_zone(tz: &str) -> PolarsResult<()> {
117 parse_time_zone(tz).map(|_| ())
118 }
119}
120
121impl std::ops::Deref for TimeZone {
122 type Target = PlSmallStr;
123
124 fn deref(&self) -> &Self::Target {
125 &self.inner
126 }
127}
128
129impl std::fmt::Debug for TimeZone {
130 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131 std::fmt::Debug::fmt(&self.inner, f)
132 }
133}
134
135impl std::fmt::Display for TimeZone {
136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137 std::fmt::Display::fmt(&self.inner, f)
138 }
139}
140
141#[cfg(feature = "timezones")]
142static FIXED_OFFSET_PATTERN: &str = r#"(?x)
143 ^
144 (?P<sign>[-+])? # optional sign
145 (?P<hour>0[0-9]|1[0-4]) # hour (between 0 and 14)
146 :? # optional separator
147 00 # minute
148 $
149 "#;
150
151#[cfg(feature = "timezones")]
152polars_utils::regex_cache::cached_regex! {
153 static FIXED_OFFSET_RE = FIXED_OFFSET_PATTERN;
154}
155
156#[cfg(feature = "timezones")]
158pub fn parse_time_zone(tz: &str) -> PolarsResult<chrono_tz::Tz> {
159 use polars_utils::levenshtein::levenshtein;
160
161 if let Ok(tz_parsed) = tz.parse::<chrono_tz::Tz>() {
162 return Ok(tz_parsed);
163 }
164
165 let mut best: Option<(&str, usize)> = None;
166 for &candidate in chrono_tz::TZ_VARIANTS.iter() {
167 let score = levenshtein(tz, candidate.name());
168 if best.is_none() || score < best.unwrap().1 {
169 best = Some((candidate.name(), score));
170 }
171 }
172
173 polars_bail!(
174 ComputeError:
175 "unable to parse time zone: '{tz}'. Please check the \
176 Time Zone Database for a list of available time zones.\n\nHint: did you mean '{}' instead?",
177 best.unwrap().0,
178 )
179}
180
181#[cfg(feature = "timezones")]
191pub fn parse_fixed_offset(tz: &str) -> PolarsResult<PlSmallStr> {
192 use polars_utils::format_pl_smallstr;
193
194 if let Some(caps) = FIXED_OFFSET_RE.captures(tz) {
195 let sign = match caps.name("sign").map(|s| s.as_str()) {
196 Some("-") => "+",
197 _ => "-",
198 };
199 let hour = caps.name("hour").unwrap().as_str().parse::<i32>().unwrap();
200 let etc_tz = format_pl_smallstr!("Etc/GMT{}{}", sign, hour);
201 if etc_tz.parse::<chrono_tz::Tz>().is_ok() {
202 return Ok(etc_tz);
203 }
204 }
205
206 unable_to_parse_err(tz)
207}
208
209#[cfg(feature = "timezones")]
210fn unable_to_parse_err<T>(tz: &str) -> PolarsResult<T> {
211 polars_bail!(
212 ComputeError:
213 "unable to parse time zone: '{}'. Please check the \
214 Time Zone Database for a list of available time zones.",
215 tz
216 )
217}