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    #[inline]
13    #[track_caller]
14    fn sub(self, rhs: OffsetDateTime) -> Self::Output {
15        OffsetDateTime::from(self) - rhs
16    }
17}
18
19impl Sub<UtcDateTime> for OffsetDateTime {
20    type Output = Duration;
21
22    /// # Panics
23    ///
24    /// This may panic if an overflow occurs.
25    #[inline]
26    #[track_caller]
27    fn sub(self, rhs: UtcDateTime) -> Self::Output {
28        self - Self::from(rhs)
29    }
30}
31
32impl PartialEq<OffsetDateTime> for UtcDateTime {
33    #[inline]
34    fn eq(&self, other: &OffsetDateTime) -> bool {
35        OffsetDateTime::from(*self) == *other
36    }
37}
38
39impl PartialEq<UtcDateTime> for OffsetDateTime {
40    #[inline]
41    fn eq(&self, other: &UtcDateTime) -> bool {
42        *self == Self::from(*other)
43    }
44}
45
46impl PartialOrd<OffsetDateTime> for UtcDateTime {
47    #[inline]
48    fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering> {
49        OffsetDateTime::from(*self).partial_cmp(other)
50    }
51}
52
53impl PartialOrd<UtcDateTime> for OffsetDateTime {
54    #[inline]
55    fn partial_cmp(&self, other: &UtcDateTime) -> Option<Ordering> {
56        self.partial_cmp(&Self::from(*other))
57    }
58}
59
60impl From<OffsetDateTime> for UtcDateTime {
61    /// # Panics
62    ///
63    /// This may panic if an overflow occurs.
64    #[inline]
65    #[track_caller]
66    fn from(datetime: OffsetDateTime) -> Self {
67        datetime.to_utc()
68    }
69}
70
71impl From<UtcDateTime> for OffsetDateTime {
72    /// # Panics
73    ///
74    /// This may panic if an overflow occurs.
75    #[inline]
76    #[track_caller]
77    fn from(datetime: UtcDateTime) -> Self {
78        datetime.as_primitive().assume_utc()
79    }
80}