use std::cmp::Ordering;
use chrono::{Datelike, NaiveDate, NaiveDateTime, NaiveTime};
use super::zone::{add_offset, zone_region};
use super::{
utc_time_nanos, CivilZonedFields, Temporal, TemporalKind, WallClockParts, ZoneId, ZonedParts,
};
struct ZonedView<'a> {
parts: ZonedParts<'a>,
source: ZonedSource<'a>,
}
enum ZonedSource<'a> {
Chrono { utc: NaiveDateTime },
Civil(&'a CivilZonedFields),
}
impl ZonedView<'_> {
fn wall(&self) -> WallClockParts {
match self.source {
ZonedSource::Chrono { utc } => local_datetime_parts(local_of(utc, self.parts.offset)),
ZonedSource::Civil(fields) => (fields.year, fields.month, fields.day, fields.time),
}
}
fn eq_identity(&self) -> (WallClockParts, ZoneId) {
(
self.wall(),
ZoneId::of_region(self.parts.region, self.parts.offset),
)
}
}
impl PartialEq for ZonedView<'_> {
fn eq(&self, other: &Self) -> bool {
self.eq_identity() == other.eq_identity()
}
}
#[derive(PartialEq)]
enum KindKey<'a> {
Date((i64, u32, u32)),
LocalTime(NaiveTime),
LocalDateTime(WallClockParts),
Time { utc_nanos: i64, offset: i32 },
Zoned(ZonedView<'a>),
Duration((i64, i64, i128)),
}
fn kind_key(t: &Temporal) -> KindKey<'_> {
match t {
Temporal::Date(d) => KindKey::Date(date_parts(*d)),
Temporal::CivilDate { year, month, day } => KindKey::Date((*year, *month, *day)),
Temporal::LocalTime(time) => KindKey::LocalTime(*time),
Temporal::LocalDateTime(dt) => KindKey::LocalDateTime(local_datetime_parts(*dt)),
Temporal::CivilDateTime {
year,
month,
day,
time,
} => KindKey::LocalDateTime((*year, *month, *day, *time)),
Temporal::Time { time, offset } => KindKey::Time {
utc_nanos: utc_time_nanos(*time, *offset),
offset: *offset,
},
Temporal::DateTime { utc, offset, zone } => KindKey::Zoned(ZonedView {
parts: ZonedParts {
instant: utc_epoch_nanos(*utc),
offset: *offset,
region: zone_region(zone.as_deref()),
},
source: ZonedSource::Chrono { utc: *utc },
}),
Temporal::CivilZoned(fields) => KindKey::Zoned(ZonedView {
parts: ZonedParts {
instant: fields.epoch_nanos(),
offset: fields.offset,
region: zone_region(fields.zone.as_deref()),
},
source: ZonedSource::Civil(fields),
}),
Temporal::Duration {
months,
days,
nanos,
} => KindKey::Duration((*months, *days, *nanos)),
}
}
fn compare_keys(a: KindKey<'_>, b: KindKey<'_>) -> Option<Ordering> {
match (a, b) {
(KindKey::Date(a), KindKey::Date(b)) => Some(a.cmp(&b)),
(KindKey::LocalTime(a), KindKey::LocalTime(b)) => Some(a.cmp(&b)),
(KindKey::LocalDateTime(a), KindKey::LocalDateTime(b)) => Some(a.cmp(&b)),
(
KindKey::Time {
utc_nanos: a,
offset: oa,
},
KindKey::Time {
utc_nanos: b,
offset: ob,
},
) => Some(a.cmp(&b).then_with(|| oa.cmp(&ob))),
(KindKey::Zoned(a), KindKey::Zoned(b)) => Some(
a.parts
.instant
.cmp(&b.parts.instant)
.then_with(|| a.parts.offset.cmp(&b.parts.offset))
.then_with(|| a.parts.region.cmp(&b.parts.region)),
),
_ => None,
}
}
fn utc_epoch_nanos(utc: NaiveDateTime) -> i128 {
let instant = utc.and_utc();
i128::from(instant.timestamp()) * 1_000_000_000 + i128::from(instant.timestamp_subsec_nanos())
}
impl Temporal {
pub fn equals(&self, other: &Temporal) -> bool {
kind_key(self) == kind_key(other)
}
pub fn datetime_eq_identity(&self) -> Option<(WallClockParts, ZoneId)> {
self.zoned_view().map(|view| view.eq_identity())
}
pub fn zoned_instant_identity(&self) -> Option<(i128, ZoneId)> {
self.zoned_parts()
.map(|parts| (parts.instant, ZoneId::of_region(parts.region, parts.offset)))
}
pub fn zoned_parts(&self) -> Option<ZonedParts<'_>> {
self.zoned_view().map(|view| view.parts)
}
fn zoned_view(&self) -> Option<ZonedView<'_>> {
match kind_key(self) {
KindKey::Zoned(view) => Some(view),
_ => None,
}
}
pub fn compare(&self, other: &Temporal) -> Option<Ordering> {
compare_keys(kind_key(self), kind_key(other))
}
pub fn same_instant(&self, other: &Temporal) -> bool {
match (self.kind(), other.kind()) {
(TemporalKind::DateTime, TemporalKind::DateTime)
| (TemporalKind::Time, TemporalKind::Time) => {
self.instant_ordering(other) == Some(Ordering::Equal)
}
_ => self.equals(other),
}
}
pub fn instant_ordering(&self, other: &Temporal) -> Option<Ordering> {
match (kind_key(self), kind_key(other)) {
(KindKey::Zoned(a), KindKey::Zoned(b)) => Some(a.parts.instant.cmp(&b.parts.instant)),
(KindKey::Time { utc_nanos: a, .. }, KindKey::Time { utc_nanos: b, .. }) => {
Some(a.cmp(&b))
}
(a, b) => compare_keys(a, b),
}
}
pub(crate) fn total_cmp(&self, other: &Temporal) -> Ordering {
match (kind_key(self), kind_key(other)) {
(KindKey::Duration(a), KindKey::Duration(b)) => a.cmp(&b),
(a, b) => compare_keys(a, b).unwrap_or(Ordering::Equal),
}
}
}
fn date_parts(d: NaiveDate) -> (i64, u32, u32) {
(i64::from(d.year()), d.month(), d.day())
}
fn local_datetime_parts(dt: NaiveDateTime) -> WallClockParts {
let (year, month, day) = date_parts(dt.date());
(year, month, day, dt.time())
}
fn local_of(utc: NaiveDateTime, offset: i32) -> NaiveDateTime {
add_offset(utc, offset).unwrap_or(utc)
}