1use std::ops::Add;
2use std::ops::AddAssign;
3use std::ops::Rem;
4use std::ops::Sub;
5use std::ops::SubAssign;
6
7use crate::Timestamp;
8
9trait Int {}
10impl Int for f64 {}
11impl Int for f32 {}
12impl Int for i64 {}
13impl Int for i32 {}
14impl Int for i16 {}
15impl Int for i8 {}
16impl Int for isize {}
17impl Int for u32 {}
18impl Int for u16 {}
19impl Int for u8 {}
20impl Int for usize {}
21
22impl<T: Into<i64> + Int> From<T> for Timestamp {
23 fn from(secs: T) -> Self {
25 Timestamp { seconds: secs.into(), nanos: 0 }
26 }
27}
28
29impl<T: Into<i64> + Int> Add<T> for Timestamp {
30 type Output = Self;
31
32 fn add(self, other: T) -> Timestamp {
34 Timestamp::new(self.seconds + other.into(), self.nanos)
35 }
36}
37
38impl<T: Into<i64> + Int> AddAssign<T> for Timestamp {
39 fn add_assign(&mut self, other: T) {
41 self.seconds += other.into();
42 }
43}
44
45impl<T: Into<i64> + Int> Sub<T> for Timestamp {
46 type Output = Self;
47
48 fn sub(self, other: T) -> Timestamp {
50 Timestamp::new(self.seconds - other.into(), self.nanos)
51 }
52}
53
54impl<T: Into<i64> + Int> SubAssign<T> for Timestamp {
55 fn sub_assign(&mut self, other: T) {
57 self.seconds -= other.into();
58 }
59}
60
61impl<T: Into<i64> + Int> Rem<T> for Timestamp {
62 type Output = Self;
63
64 fn rem(self, other: T) -> Timestamp {
66 Timestamp::new(self.seconds % other.into(), self.nanos)
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn test_from() {
76 let ts: Timestamp = 1335020400.into();
77 assert_eq!(ts.seconds, 1335020400);
78 assert_eq!(ts.nanos, 0);
79 }
80
81 #[test]
82 fn test_add() {
83 let ts = Timestamp::new(1335020400, 0) + 86400;
84 assert_eq!(ts.seconds, 1335020400 + 86400);
85 assert_eq!(ts.nanos, 0);
86 }
87
88 #[test]
89 fn test_add_assign() {
90 let mut ts = Timestamp::new(1335020400, 500_000_000);
91 ts += 86400;
92 assert_eq!(ts.seconds, 1335020400 + 86400);
93 assert_eq!(ts.nanos, 500_000_000);
94 }
95
96 #[test]
97 fn test_sub() {
98 let ts = Timestamp::new(1335020400, 0) - 86400;
99 assert_eq!(ts.seconds, 1335020400 - 86400);
100 assert_eq!(ts.nanos, 0);
101 }
102
103 #[test]
104 fn test_sub_assign() {
105 let mut ts = Timestamp::new(1335020400, 500_000_000);
106 ts -= 86400;
107 assert_eq!(ts.seconds, 1335020400 - 86400);
108 assert_eq!(ts.nanos, 500_000_000);
109 }
110}