Debian Codegraph @

debian/rust-vendor/time/src/interop/timestamp_utcdatetime.rs

raw
1 use core::cmp::Ordering;
2 use core::ops::Sub;
3
4 use crate::{SignedDuration, Timestamp, UtcDateTime};
5
6 impl Sub<UtcDateTime> for Timestamp {
7 type Output = SignedDuration;
8
9 #[inline]
10 fn sub(self, rhs: UtcDateTime) -> Self::Output {
11 SignedDuration::new(
12 self.as_seconds() - rhs.unix_timestamp(),
13 self.nanosecond().cast_signed() - rhs.nanosecond().cast_signed(),
14 )
15 }
16 }
17
18 impl Sub<Timestamp> for UtcDateTime {
19 type Output = SignedDuration;
20
21 #[inline]
22 fn sub(self, rhs: Timestamp) -> Self::Output {
23 SignedDuration::new(
24 self.unix_timestamp() - rhs.as_seconds(),
25 self.nanosecond().cast_signed() - rhs.nanosecond().cast_signed(),
26 )
27 }
28 }
29
30 impl 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
38 impl PartialEq<Timestamp> for UtcDateTime {
39 #[inline]
40 fn eq(&self, other: &Timestamp) -> bool {
41 other == self
42 }
43 }
44
45 impl 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
53 impl 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
60 impl From<UtcDateTime> for Timestamp {
61 #[inline]
62 fn from(datetime: UtcDateTime) -> Self {
63 // Safety: The valid range of `Timestamp` and `UtcDateTime` are the same. Nanoseconds also
64 // have the same range.
65 unsafe {
66 Self::from_seconds(datetime.unix_timestamp())
67 .unwrap_unchecked()
68 .replace_nanosecond(datetime.nanosecond())
69 .unwrap_unchecked()
70 }
71 }
72 }
73
74 impl From<Timestamp> for UtcDateTime {
75 #[inline]
76 fn from(timestamp: Timestamp) -> Self {
77 // Safety: The valid range of `Timestamp` and `UtcDateTime` are the same. Nanoseconds also
78 // have the same range.
79 unsafe {
80 Self::from_unix_timestamp(timestamp.as_seconds())
81 .unwrap_unchecked()
82 .replace_nanosecond(timestamp.nanosecond())
83 .unwrap_unchecked()
84 }
85 }
86 }