1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
use serde_derive::{Deserialize, Serialize}; use std::cmp; use std::ops; use std::time; const BILLION: i64 = 1_000_000_000; #[derive(Copy, Clone, Default, Serialize, Deserialize, Debug)] pub struct Time { pub sec: u32, pub nsec: u32, } impl Time { #[inline] pub fn new() -> Time { Self::default() } #[inline] pub fn from_nanos(t: i64) -> Time { Time { sec: (t / BILLION) as u32, nsec: (t % BILLION) as u32, } } #[inline] pub fn nanos(self) -> i64 { i64::from(self.sec) * BILLION + i64::from(self.nsec) } #[inline] pub fn seconds(self) -> f64 { f64::from(self.sec) + f64::from(self.nsec) / BILLION as f64 } } impl cmp::PartialEq for Time { fn eq(&self, other: &Self) -> bool { self.nanos() == other.nanos() } } impl cmp::PartialOrd for Time { fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { self.nanos().partial_cmp(&other.nanos()) } } impl cmp::Eq for Time {} impl cmp::Ord for Time { fn cmp(&self, other: &Self) -> cmp::Ordering { self.nanos().cmp(&other.nanos()) } } #[derive(Copy, Clone, Default, Serialize, Deserialize, Debug)] pub struct Duration { pub sec: i32, pub nsec: i32, } impl Duration { #[inline] pub fn new() -> Duration { Self::default() } #[inline] pub fn from_nanos(t: i64) -> Duration { Duration { sec: (t / BILLION) as i32, nsec: (t % BILLION) as i32, } } #[inline] pub fn from_seconds(sec: i32) -> Duration { Duration { sec, nsec: 0 } } #[inline] fn nanos(self) -> i64 { i64::from(self.sec) * BILLION + i64::from(self.nsec) } #[inline] pub fn seconds(self) -> f64 { f64::from(self.sec) + f64::from(self.nsec) / BILLION as f64 } } impl cmp::PartialEq for Duration { fn eq(&self, other: &Self) -> bool { self.nanos() == other.nanos() } } impl cmp::PartialOrd for Duration { fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { self.nanos().partial_cmp(&other.nanos()) } } impl cmp::Eq for Duration {} impl cmp::Ord for Duration { fn cmp(&self, other: &Self) -> cmp::Ordering { self.nanos().cmp(&other.nanos()) } } impl ops::Add<Duration> for Time { type Output = Time; fn add(self, rhs: Duration) -> Self::Output { Time::from_nanos(self.nanos() + rhs.nanos()) } } impl ops::Add<Duration> for Duration { type Output = Duration; fn add(self, rhs: Duration) -> Self::Output { Duration::from_nanos(self.nanos() + rhs.nanos()) } } impl ops::Sub<Time> for Time { type Output = Duration; fn sub(self, rhs: Time) -> Self::Output { Duration::from_nanos(self.nanos() - rhs.nanos()) } } impl ops::Sub<Duration> for Time { type Output = Time; fn sub(self, rhs: Duration) -> Self::Output { Time::from_nanos(self.nanos() - rhs.nanos()) } } impl ops::Sub<Duration> for Duration { type Output = Duration; fn sub(self, rhs: Duration) -> Self::Output { Duration::from_nanos(self.nanos() - rhs.nanos()) } } impl ops::Neg for Duration { type Output = Duration; fn neg(self) -> Self::Output { Duration { sec: -self.sec, nsec: -self.nsec, } } } impl From<time::Duration> for Duration { fn from(std_duration: time::Duration) -> Self { Duration { sec: std_duration.as_secs() as i32, nsec: std_duration.subsec_nanos() as i32, } } } #[cfg(test)] mod tests { use super::{Duration, Time}; use std::time; #[test] fn from_nanos_works() { let time = Time::from_nanos(123456789987654321); assert_eq!(time.sec, 123456789); assert_eq!(time.nsec, 987654321); let time = Duration::from_nanos(123456789987654321); assert_eq!(time.sec, 123456789); assert_eq!(time.nsec, 987654321); } #[test] fn nanos_works() { let time = Time { sec: 123456789, nsec: 987654321, }; assert_eq!(time.nanos(), 123456789987654321); let time = Duration { sec: 123456789, nsec: 987654321, }; assert_eq!(time.nanos(), 123456789987654321); } #[test] fn duration_works_with_negative() { let time = Duration::from_nanos(-123456789987654321); assert_eq!(time.sec, -123456789); assert_eq!(time.nsec, -987654321); assert_eq!(time.nanos(), -123456789987654321); } #[test] fn convert_works() { let std_duration = time::Duration::new(123, 456); let msg_duration = Duration::from(std_duration); assert_eq!(msg_duration.sec, 123); assert_eq!(msg_duration.nsec, 456); let std_duration2 = time::Duration::new(9876, 54321); let msg_duration2: Duration = std_duration2.into(); assert_eq!(msg_duration2.sec, 9876); assert_eq!(msg_duration2.nsec, 54321); } }