time/interop/
offsetdatetime_utcdatetime.rs

1use core::cmp::Ordering;
2use core::ops::Sub;
3
4use crate::{Duration, OffsetDateTime, UtcDateTime};
5
6impl Sub<OffsetDateTime> for UtcDateTime {
7    type Output = Duration;
8
9    /// # Panics
10    ///
11    /// This may panic if an overflow occurs.
12    fn sub(self, rhs: OffsetDateTime) -> Self::Output {
13        OffsetDateTime::from(self) - rhs
14    }
15}
16
17impl Sub<UtcDateTime> for OffsetDateTime {
18    type Output = Duration;
19
20    /// # Panics
21    ///
22    /// This may panic if an overflow occurs.
23    fn sub(self, rhs: UtcDateTime) -> Self::Output {
24        self - Self::from(rhs)
25    }
26}
27
28impl PartialEq<OffsetDateTime> for UtcDateTime {
29    fn eq(&self, other: &OffsetDateTime) -> bool {
30        OffsetDateTime::from(*self) == *other
31    }
32}
33
34impl PartialEq<UtcDateTime> for OffsetDateTime {
35    fn eq(&self, other: &UtcDateTime) -> bool {
36        *self == Self::from(*other)
37    }
38}
39
40impl PartialOrd<OffsetDateTime> for UtcDateTime {
41    fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering> {
42        OffsetDateTime::from(*self).partial_cmp(other)
43    }
44}
45
46impl PartialOrd<UtcDateTime> for OffsetDateTime {
47    fn partial_cmp(&self, other: &UtcDateTime) -> Option<Ordering> {
48        self.partial_cmp(&Self::from(*other))
49    }
50}
51
52impl From<OffsetDateTime> for UtcDateTime {
53    /// # Panics
54    ///
55    /// This may panic if an overflow occurs.
56    fn from(datetime: OffsetDateTime) -> Self {
57        datetime.to_utc()
58    }
59}
60
61impl From<UtcDateTime> for OffsetDateTime {
62    /// # Panics
63    ///
64    /// This may panic if an overflow occurs.
65    fn from(datetime: UtcDateTime) -> Self {
66        datetime.as_primitive().assume_utc()
67    }
68}