proto_types/timestamp/
timestamp_operations.rs1use std::{
2 cmp::Ordering,
3 ops::{Add, Sub},
4};
5
6use crate::{Duration, Timestamp};
7
8impl<'b> Sub<&'b Duration> for &Timestamp {
9 type Output = Timestamp;
10
11 fn sub(self, rhs: &'b Duration) -> Self::Output {
12 let duration = rhs.normalized();
13
14 let mut new = Timestamp {
15 seconds: self.seconds - duration.seconds,
16 nanos: self.nanos - duration.nanos,
17 };
18
19 new.normalize();
20
21 new
22 }
23}
24
25impl Sub<Duration> for Timestamp {
26 type Output = Timestamp;
27 fn sub(self, rhs: Duration) -> Self::Output {
28 <&Timestamp as Sub<&Duration>>::sub(&self, &rhs)
29 }
30}
31
32impl<'b> Sub<&'b Duration> for Timestamp {
33 type Output = Timestamp;
34 fn sub(self, rhs: &'b Duration) -> Self::Output {
35 <&Timestamp as Sub<&Duration>>::sub(&self, rhs)
36 }
37}
38
39impl<'a> Sub<Duration> for &'a Timestamp {
40 type Output = Timestamp;
41 fn sub(self, rhs: Duration) -> Self::Output {
42 <&'a Timestamp as Sub<&Duration>>::sub(self, &rhs)
43 }
44}
45
46impl<'b> Add<&'b Duration> for &Timestamp {
47 type Output = Timestamp;
48
49 fn add(self, rhs: &'b Duration) -> Self::Output {
50 let duration = rhs.normalized();
51
52 let mut new = Timestamp {
53 seconds: self.seconds + duration.seconds,
54 nanos: self.nanos + duration.nanos,
55 };
56
57 new.normalize();
58
59 new
60 }
61}
62
63impl<'b> Add<&'b Duration> for Timestamp {
64 type Output = Timestamp;
65 fn add(self, rhs: &'b Duration) -> Self::Output {
66 <&Timestamp as Add<&Duration>>::add(&self, rhs)
67 }
68}
69
70impl Add<Duration> for &Timestamp {
71 type Output = Timestamp;
72 fn add(self, rhs: Duration) -> Self::Output {
73 <Self as Add<&Duration>>::add(self, &rhs)
74 }
75}
76
77impl Add<Duration> for Timestamp {
78 type Output = Timestamp;
79
80 fn add(self, rhs: Duration) -> Self::Output {
81 &self + &rhs
82 }
83}
84
85impl PartialOrd for Timestamp {
86 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
87 Some(self.cmp(other))
88 }
89}
90
91impl Ord for Timestamp {
92 fn cmp(&self, other: &Self) -> Ordering {
93 (self.seconds, self.nanos).cmp(&(other.seconds, other.nanos))
94 }
95}