use core::cmp::Ordering; use core::ops::Sub; use std::time::SystemTime; use crate::{Duration, OffsetDateTime}; impl Sub for OffsetDateTime { type Output = Duration; /// # Panics /// /// This may panic if an overflow occurs. fn sub(self, rhs: SystemTime) -> Self::Output { self - Self::from(rhs) } } impl Sub for SystemTime { type Output = Duration; /// # Panics /// /// This may panic if an overflow occurs. fn sub(self, rhs: OffsetDateTime) -> Self::Output { OffsetDateTime::from(self) - rhs } } impl PartialEq for OffsetDateTime { fn eq(&self, rhs: &SystemTime) -> bool { self == &Self::from(*rhs) } } impl PartialEq for SystemTime { fn eq(&self, rhs: &OffsetDateTime) -> bool { &OffsetDateTime::from(*self) == rhs } } impl PartialOrd for OffsetDateTime { fn partial_cmp(&self, other: &SystemTime) -> Option { self.partial_cmp(&Self::from(*other)) } } impl PartialOrd for SystemTime { fn partial_cmp(&self, other: &OffsetDateTime) -> Option { OffsetDateTime::from(*self).partial_cmp(other) } } impl From for OffsetDateTime { fn from(system_time: SystemTime) -> Self { match system_time.duration_since(SystemTime::UNIX_EPOCH) { Ok(duration) => Self::UNIX_EPOCH + duration, Err(err) => Self::UNIX_EPOCH - err.duration(), } } } impl From for SystemTime { fn from(datetime: OffsetDateTime) -> Self { let duration = datetime - OffsetDateTime::UNIX_EPOCH; if duration.is_zero() { Self::UNIX_EPOCH } else if duration.is_positive() { Self::UNIX_EPOCH + duration.unsigned_abs() } else { debug_assert!(duration.is_negative()); Self::UNIX_EPOCH - duration.unsigned_abs() } } }