use arrow_schema::ArrowError;
use chrono::{DateTime, FixedOffset, NaiveDateTime};
pub use private::{Tz, TzOffset};
fn parse_fixed_offset(tz: &str) -> Option<FixedOffset> {
let bytes = tz.as_bytes();
let mut values = match bytes.len() {
6 if bytes[3] == b':' => [bytes[1], bytes[2], bytes[4], bytes[5]],
5 => [bytes[1], bytes[2], bytes[3], bytes[4]],
3 => [bytes[1], bytes[2], b'0', b'0'],
_ => return None,
};
values.iter_mut().for_each(|x| *x = x.wrapping_sub(b'0'));
if values.iter().any(|x| *x > 9) {
return None;
}
let secs =
(values[0] * 10 + values[1]) as i32 * 60 * 60 + (values[2] * 10 + values[3]) as i32 * 60;
match bytes[0] {
b'+' => FixedOffset::east_opt(secs),
b'-' => FixedOffset::west_opt(secs),
_ => None,
}
}
#[cfg(feature = "chrono-tz")]
mod private {
use super::*;
use chrono::offset::TimeZone;
use chrono::{LocalResult, NaiveDate, NaiveDateTime, Offset};
use std::fmt::Display;
use std::str::FromStr;
#[derive(Debug, Copy, Clone)]
pub struct TzOffset {
tz: Tz,
offset: FixedOffset,
}
impl std::fmt::Display for TzOffset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.offset.fmt(f)
}
}
impl Offset for TzOffset {
fn fix(&self) -> FixedOffset {
self.offset
}
}
#[derive(Debug, Copy, Clone)]
pub struct Tz(TzInner);
#[derive(Debug, Copy, Clone)]
enum TzInner {
Timezone(chrono_tz::Tz),
Offset(FixedOffset),
}
impl FromStr for Tz {
type Err = ArrowError;
fn from_str(tz: &str) -> Result<Self, Self::Err> {
match parse_fixed_offset(tz) {
Some(offset) => Ok(Self(TzInner::Offset(offset))),
None => Ok(Self(TzInner::Timezone(tz.parse().map_err(|e| {
ArrowError::ParseError(format!("Invalid timezone \"{tz}\": {e}"))
})?))),
}
}
}
impl Display for Tz {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0 {
TzInner::Timezone(tz) => tz.fmt(f),
TzInner::Offset(offset) => offset.fmt(f),
}
}
}
macro_rules! tz {
($s:ident, $tz:ident, $b:block) => {
match $s.0 {
TzInner::Timezone($tz) => $b,
TzInner::Offset($tz) => $b,
}
};
}
impl TimeZone for Tz {
type Offset = TzOffset;
fn from_offset(offset: &Self::Offset) -> Self {
offset.tz
}
fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult<Self::Offset> {
tz!(self, tz, {
tz.offset_from_local_date(local).map(|x| TzOffset {
tz: *self,
offset: x.fix(),
})
})
}
fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<Self::Offset> {
tz!(self, tz, {
tz.offset_from_local_datetime(local).map(|x| TzOffset {
tz: *self,
offset: x.fix(),
})
})
}
fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset {
tz!(self, tz, {
TzOffset {
tz: *self,
offset: tz.offset_from_utc_date(utc).fix(),
}
})
}
fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset {
tz!(self, tz, {
TzOffset {
tz: *self,
offset: tz.offset_from_utc_datetime(utc).fix(),
}
})
}
}
#[inline]
pub(super) fn new_gap_info(local: &NaiveDateTime, tz: &Tz) -> Option<GapInfo> {
let TzInner::Timezone(ctz) = &tz.0 else {
return None;
};
chrono_tz::GapInfo::new(local, ctz).map(|gi| GapInfo {
begin: gi.begin.map(|(ndt, ctz)| {
(
ndt,
TzOffset {
tz: *tz,
offset: ctz.fix(),
},
)
}),
end: gi.end.map(|dt| dt.with_timezone(tz)),
})
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{Timelike, Utc};
#[test]
fn test_with_timezone() {
let vals = [
Utc.timestamp_millis_opt(37800000).unwrap(),
Utc.timestamp_millis_opt(86339000).unwrap(),
];
assert_eq!(10, vals[0].hour());
assert_eq!(23, vals[1].hour());
let tz: Tz = "America/Los_Angeles".parse().unwrap();
assert_eq!(2, vals[0].with_timezone(&tz).hour());
assert_eq!(15, vals[1].with_timezone(&tz).hour());
}
#[test]
fn test_using_chrono_tz_and_utc_naive_date_time() {
let sydney_tz = "Australia/Sydney".to_string();
let tz: Tz = sydney_tz.parse().unwrap();
let sydney_offset_without_dst = FixedOffset::east_opt(10 * 60 * 60).unwrap();
let sydney_offset_with_dst = FixedOffset::east_opt(11 * 60 * 60).unwrap();
let utc_just_before_sydney_dst_ends = NaiveDate::from_ymd_opt(2021, 4, 3)
.unwrap()
.and_hms_nano_opt(15, 30, 0, 0)
.unwrap();
assert_eq!(
tz.offset_from_utc_datetime(&utc_just_before_sydney_dst_ends)
.fix(),
sydney_offset_with_dst
);
let utc_just_after_sydney_dst_ends = NaiveDate::from_ymd_opt(2021, 4, 3)
.unwrap()
.and_hms_nano_opt(16, 30, 0, 0)
.unwrap();
assert_eq!(
tz.offset_from_utc_datetime(&utc_just_after_sydney_dst_ends)
.fix(),
sydney_offset_without_dst
);
let utc_just_before_sydney_dst_starts = NaiveDate::from_ymd_opt(2021, 10, 2)
.unwrap()
.and_hms_nano_opt(15, 30, 0, 0)
.unwrap();
assert_eq!(
tz.offset_from_utc_datetime(&utc_just_before_sydney_dst_starts)
.fix(),
sydney_offset_without_dst
);
let utc_just_after_sydney_dst_starts = NaiveDate::from_ymd_opt(2022, 10, 2)
.unwrap()
.and_hms_nano_opt(16, 30, 0, 0)
.unwrap();
assert_eq!(
tz.offset_from_utc_datetime(&utc_just_after_sydney_dst_starts)
.fix(),
sydney_offset_with_dst
);
}
fn naive(y: i32, mo: u32, d: u32, h: u32, mi: u32) -> NaiveDateTime {
NaiveDate::from_ymd_opt(y, mo, d)
.unwrap()
.and_hms_opt(h, mi, 0)
.unwrap()
}
fn utc(y: i32, mo: u32, d: u32, h: u32, mi: u32) -> chrono::DateTime<Utc> {
Utc.with_ymd_and_hms(y, mo, d, h, mi, 0).unwrap()
}
#[test]
fn test_gap_info_not_in_gap() {
let tz: Tz = "America/New_York".parse().unwrap();
assert!(GapInfo::new(&naive(2021, 3, 14, 1, 59), &tz).is_none());
assert!(GapInfo::new(&naive(2021, 3, 14, 3, 0), &tz).is_none());
assert!(GapInfo::new(&naive(2021, 11, 7, 1, 30), &tz).is_none());
}
#[test]
fn test_gap_info_america_new_york() {
let tz: Tz = "America/New_York".parse().unwrap();
for local in [
naive(2021, 3, 14, 2, 0),
naive(2021, 3, 14, 2, 30),
naive(2021, 3, 14, 2, 59),
] {
assert!(tz.from_local_datetime(&local).single().is_none());
let gap = GapInfo::new(&local, &tz).unwrap();
let (begin, begin_offset) = gap.begin.unwrap();
assert_eq!(begin, naive(2021, 3, 14, 2, 0));
assert_eq!(
begin_offset.fix(),
FixedOffset::west_opt(5 * 60 * 60).unwrap()
);
let end = gap.end.unwrap();
assert_eq!(end.naive_local(), naive(2021, 3, 14, 3, 0));
assert_eq!(
end.offset().fix(),
FixedOffset::west_opt(4 * 60 * 60).unwrap()
);
assert_eq!(end, utc(2021, 3, 14, 7, 0));
assert_eq!(end.timezone().to_string(), "America/New_York");
}
}
#[test]
fn test_gap_info_europe_london() {
let tz: Tz = "Europe/London".parse().unwrap();
let gap = GapInfo::new(&naive(2021, 3, 28, 1, 30), &tz).unwrap();
let (begin, begin_offset) = gap.begin.unwrap();
assert_eq!(begin, naive(2021, 3, 28, 1, 0));
assert_eq!(begin_offset.fix(), FixedOffset::east_opt(0).unwrap());
let end = gap.end.unwrap();
assert_eq!(end.naive_local(), naive(2021, 3, 28, 2, 0));
assert_eq!(end.offset().fix(), FixedOffset::east_opt(60 * 60).unwrap());
assert_eq!(end, utc(2021, 3, 28, 1, 0));
}
#[test]
fn test_gap_info_australia_sydney() {
let tz: Tz = "Australia/Sydney".parse().unwrap();
let gap = GapInfo::new(&naive(2021, 10, 3, 2, 30), &tz).unwrap();
let (begin, begin_offset) = gap.begin.unwrap();
assert_eq!(begin, naive(2021, 10, 3, 2, 0));
assert_eq!(
begin_offset.fix(),
FixedOffset::east_opt(10 * 60 * 60).unwrap()
);
let end = gap.end.unwrap();
assert_eq!(end.naive_local(), naive(2021, 10, 3, 3, 0));
assert_eq!(
end.offset().fix(),
FixedOffset::east_opt(11 * 60 * 60).unwrap()
);
assert_eq!(end, utc(2021, 10, 2, 16, 0));
}
#[test]
fn test_timezone_display() {
let test_cases = ["UTC", "America/Los_Angeles", "-08:00", "+05:30"];
for &case in &test_cases {
let tz: Tz = case.parse().unwrap();
assert_eq!(tz.to_string(), case);
}
}
}
}
#[cfg(not(feature = "chrono-tz"))]
mod private {
use super::*;
use chrono::offset::TimeZone;
use chrono::{LocalResult, NaiveDate, NaiveDateTime, Offset};
use std::str::FromStr;
#[derive(Debug, Copy, Clone)]
pub struct TzOffset(FixedOffset);
impl std::fmt::Display for TzOffset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Offset for TzOffset {
fn fix(&self) -> FixedOffset {
self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct Tz(FixedOffset);
impl FromStr for Tz {
type Err = ArrowError;
fn from_str(tz: &str) -> Result<Self, Self::Err> {
let offset = parse_fixed_offset(tz).ok_or_else(|| {
ArrowError::ParseError(format!(
"Invalid timezone \"{tz}\": only offset based timezones supported without chrono-tz feature"
))
})?;
Ok(Self(offset))
}
}
impl TimeZone for Tz {
type Offset = TzOffset;
fn from_offset(offset: &Self::Offset) -> Self {
Self(offset.0)
}
fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult<Self::Offset> {
self.0.offset_from_local_date(local).map(TzOffset)
}
fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<Self::Offset> {
self.0.offset_from_local_datetime(local).map(TzOffset)
}
fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset {
TzOffset(self.0.offset_from_utc_date(utc).fix())
}
fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset {
TzOffset(self.0.offset_from_utc_datetime(utc).fix())
}
}
#[inline]
pub(super) fn new_gap_info(_local: &NaiveDateTime, _tz: &Tz) -> Option<GapInfo> {
None
}
}
#[derive(Debug, Clone, Copy)]
pub struct GapInfo {
pub begin: Option<(NaiveDateTime, TzOffset)>,
pub end: Option<DateTime<Tz>>,
}
impl GapInfo {
pub fn new(local: &NaiveDateTime, tz: &Tz) -> Option<Self> {
private::new_gap_info(local, tz)
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{NaiveDate, Offset, TimeZone};
#[test]
fn test_with_offset() {
let t = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap();
let tz: Tz = "-00:00".parse().unwrap();
assert_eq!(tz.offset_from_utc_date(&t).fix().local_minus_utc(), 0);
let tz: Tz = "+00:00".parse().unwrap();
assert_eq!(tz.offset_from_utc_date(&t).fix().local_minus_utc(), 0);
let tz: Tz = "-10:00".parse().unwrap();
assert_eq!(
tz.offset_from_utc_date(&t).fix().local_minus_utc(),
-10 * 60 * 60
);
let tz: Tz = "+09:00".parse().unwrap();
assert_eq!(
tz.offset_from_utc_date(&t).fix().local_minus_utc(),
9 * 60 * 60
);
let tz = "+09".parse::<Tz>().unwrap();
assert_eq!(
tz.offset_from_utc_date(&t).fix().local_minus_utc(),
9 * 60 * 60
);
let tz = "+0900".parse::<Tz>().unwrap();
assert_eq!(
tz.offset_from_utc_date(&t).fix().local_minus_utc(),
9 * 60 * 60
);
let err = "+9:00".parse::<Tz>().unwrap_err().to_string();
assert!(err.contains("Invalid timezone"), "{}", err);
}
#[test]
fn test_gap_info_fixed_offset() {
let times = [
NaiveDate::from_ymd_opt(2000, 1, 1)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap(),
NaiveDate::from_ymd_opt(2021, 3, 14)
.unwrap()
.and_hms_opt(2, 30, 0)
.unwrap(),
NaiveDate::from_ymd_opt(2021, 3, 28)
.unwrap()
.and_hms_opt(1, 30, 0)
.unwrap(),
];
for tz in ["+00:00", "-05:00", "+01:00", "+09:30"] {
let tz: Tz = tz.parse().unwrap();
for t in × {
assert!(GapInfo::new(t, &tz).is_none(), "{tz:?} {t}");
}
}
}
}