time/interop/
timestamp_utcdatetime.rs1use core::cmp::Ordering;
2use core::ops::Sub;
3
4use crate::{Duration, Timestamp, UtcDateTime};
5
6impl Sub<UtcDateTime> for Timestamp {
7 type Output = Duration;
8
9 #[inline]
10 fn sub(self, rhs: UtcDateTime) -> Self::Output {
11 Duration::new(
12 self.as_seconds() - rhs.unix_timestamp(),
13 self.nanosecond().cast_signed() - rhs.nanosecond().cast_signed(),
14 )
15 }
16}
17
18impl Sub<Timestamp> for UtcDateTime {
19 type Output = Duration;
20
21 #[inline]
22 fn sub(self, rhs: Timestamp) -> Self::Output {
23 Duration::new(
24 self.unix_timestamp() - rhs.as_seconds(),
25 self.nanosecond().cast_signed() - rhs.nanosecond().cast_signed(),
26 )
27 }
28}
29
30impl PartialEq<UtcDateTime> for Timestamp {
31 #[expect(clippy::suspicious_operation_groupings, reason = "false positive")]
32 #[inline]
33 fn eq(&self, other: &UtcDateTime) -> bool {
34 self.as_seconds() == other.unix_timestamp() && self.nanosecond() == other.nanosecond()
35 }
36}
37
38impl PartialEq<Timestamp> for UtcDateTime {
39 #[inline]
40 fn eq(&self, other: &Timestamp) -> bool {
41 other == self
42 }
43}
44
45impl PartialOrd<UtcDateTime> for Timestamp {
46 #[inline]
47 fn partial_cmp(&self, other: &UtcDateTime) -> Option<Ordering> {
48 (self.as_seconds(), self.nanosecond())
49 .partial_cmp(&(other.unix_timestamp(), other.nanosecond()))
50 }
51}
52
53impl PartialOrd<Timestamp> for UtcDateTime {
54 #[inline]
55 fn partial_cmp(&self, other: &Timestamp) -> Option<Ordering> {
56 other.partial_cmp(self).map(Ordering::reverse)
57 }
58}
59
60impl From<UtcDateTime> for Timestamp {
61 #[inline]
62 fn from(datetime: UtcDateTime) -> Self {
63 unsafe {
66 Self::from_seconds(datetime.unix_timestamp())
67 .unwrap_unchecked()
68 .replace_nanosecond(datetime.nanosecond())
69 .unwrap_unchecked()
70 }
71 }
72}
73
74impl From<Timestamp> for UtcDateTime {
75 #[inline]
76 fn from(timestamp: Timestamp) -> Self {
77 unsafe {
80 Self::from_unix_timestamp(timestamp.as_seconds())
81 .unwrap_unchecked()
82 .replace_nanosecond(timestamp.nanosecond())
83 .unwrap_unchecked()
84 }
85 }
86}