| 1 |
use core::cmp::Ordering; |
| 2 |
use core::ops::Sub; |
| 3 |
use std::time::SystemTime; |
| 4 |
|
| 5 |
use crate::{SignedDuration, UtcDateTime}; |
| 6 |
|
| 7 |
impl Sub<SystemTime> for UtcDateTime { |
| 8 |
type Output = SignedDuration; |
| 9 |
|
| 10 |
/// # Panics |
| 11 |
/// |
| 12 |
/// This may panic if an overflow occurs. |
| 13 |
#[inline] |
| 14 |
#[track_caller] |
| 15 |
fn sub(self, rhs: SystemTime) -> Self::Output { |
| 16 |
self - Self::from(rhs) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
impl Sub<UtcDateTime> for SystemTime { |
| 21 |
type Output = SignedDuration; |
| 22 |
|
| 23 |
/// # Panics |
| 24 |
/// |
| 25 |
/// This may panic if an overflow occurs. |
| 26 |
#[inline] |
| 27 |
#[track_caller] |
| 28 |
fn sub(self, rhs: UtcDateTime) -> Self::Output { |
| 29 |
UtcDateTime::from(self) - rhs |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl PartialEq<SystemTime> for UtcDateTime { |
| 34 |
#[inline] |
| 35 |
fn eq(&self, rhs: &SystemTime) -> bool { |
| 36 |
self == &Self::from(*rhs) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl PartialEq<UtcDateTime> for SystemTime { |
| 41 |
#[inline] |
| 42 |
fn eq(&self, rhs: &UtcDateTime) -> bool { |
| 43 |
&UtcDateTime::from(*self) == rhs |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl PartialOrd<SystemTime> for UtcDateTime { |
| 48 |
#[inline] |
| 49 |
fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> { |
| 50 |
self.partial_cmp(&Self::from(*other)) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl PartialOrd<UtcDateTime> for SystemTime { |
| 55 |
#[inline] |
| 56 |
fn partial_cmp(&self, other: &UtcDateTime) -> Option<Ordering> { |
| 57 |
UtcDateTime::from(*self).partial_cmp(other) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
impl From<SystemTime> for UtcDateTime { |
| 62 |
#[inline] |
| 63 |
fn from(system_time: SystemTime) -> Self { |
| 64 |
match system_time.duration_since(SystemTime::UNIX_EPOCH) { |
| 65 |
Ok(duration) => Self::UNIX_EPOCH + duration, |
| 66 |
Err(err) => Self::UNIX_EPOCH - err.duration(), |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl From<UtcDateTime> for SystemTime { |
| 72 |
/// # Panics |
| 73 |
/// |
| 74 |
/// This may panic if the resulting `SystemTime` cannot be represented. |
| 75 |
#[inline] |
| 76 |
fn from(datetime: UtcDateTime) -> Self { |
| 77 |
let duration = datetime - UtcDateTime::UNIX_EPOCH; |
| 78 |
|
| 79 |
if duration.is_zero() { |
| 80 |
Self::UNIX_EPOCH |
| 81 |
} else if duration.is_positive() { |
| 82 |
Self::UNIX_EPOCH + duration.unsigned_abs() |
| 83 |
} else { |
| 84 |
debug_assert!(duration.is_negative()); |
| 85 |
Self::UNIX_EPOCH - duration.unsigned_abs() |
| 86 |
} |
| 87 |
} |
| 88 |
} |