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
use std::ops::{Add, Sub};
use speedy::{Readable, Writable};
use serde::{Deserialize, Serialize};
use super::duration::Duration;
#[derive(
Debug, PartialEq, Eq, PartialOrd, Ord, Readable, Writable, Clone, Copy, Serialize, Deserialize,
)]
pub struct Timestamp {
seconds: u32,
fraction: u32,
}
impl Timestamp {
pub const ZERO: Self = Self {
seconds: 0,
fraction: 0,
};
pub const INVALID: Self = Self {
seconds: 0xFFFF_FFFF,
fraction: 0xFFFF_FFFF,
};
pub const INFINITE: Self = Self {
seconds: 0x7FFF_FFFF,
fraction: 0xFFFF_FFFF,
};
pub fn now() -> Self {
Self::from_nanos(chrono::Utc::now().timestamp_nanos() as u64)
}
fn to_ticks(self) -> u64 {
(u64::from(self.seconds) << 32) + u64::from(self.fraction)
}
fn from_ticks(ticks: u64) -> Self {
Self {
seconds: (ticks >> 32) as u32,
fraction: ticks as u32,
}
}
fn from_nanos(nanos_since_unix_epoch: u64) -> Self {
Self {
seconds: (nanos_since_unix_epoch / 1_000_000_000) as u32,
fraction: (((nanos_since_unix_epoch % 1_000_000_000) << 32) / 1_000_000_000) as u32,
}
}
pub fn duration_since(&self, since: Self) -> Duration {
*self - since
}
}
impl Sub for Timestamp {
type Output = Duration;
fn sub(self, other: Self) -> Duration {
let a = self.to_ticks();
let b = other.to_ticks();
Duration::from_ticks(a.wrapping_sub(b) as i64)
}
}
impl Sub<Duration> for Timestamp {
type Output = Self;
fn sub(self, rhs: Duration) -> Self::Output {
if self == Self::INVALID {
Self::INVALID
} else {
let stamp_ticks = self.to_ticks() as i64;
let sub_ticks = rhs.to_ticks();
let new_stamp_ticks = stamp_ticks.wrapping_sub(sub_ticks);
Self::from_ticks(new_stamp_ticks as u64)
}
}
}
impl Add<Duration> for Timestamp {
type Output = Self;
fn add(self, rhs: Duration) -> Self::Output {
let lhs_ticks = self.to_ticks();
let rhs_ticks = rhs.to_ticks() as u64;
Self::from_ticks(lhs_ticks + rhs_ticks)
}
}
#[cfg(test)]
mod tests {
use super::*;
serialization_test!( type = Timestamp,
{
time_zero,
Timestamp::ZERO,
le = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
be = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
},
{
time_invalid,
Timestamp::INVALID,
le = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
be = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
},
{
time_infinite,
Timestamp::INFINITE,
le = [0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF],
be = [0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
},
{
time_current_empty_fraction,
Timestamp { seconds: 1_537_045_491, fraction: 0 },
le = [0xF3, 0x73, 0x9D, 0x5B, 0x00, 0x00, 0x00, 0x00],
be = [0x5B, 0x9D, 0x73, 0xF3, 0x00, 0x00, 0x00, 0x00]
},
{
time_from_wireshark,
Timestamp { seconds: 1_519_152_760, fraction: 1_328_210_046 },
le = [0x78, 0x6E, 0x8C, 0x5A, 0x7E, 0xE0, 0x2A, 0x4F],
be = [0x5A, 0x8C, 0x6E, 0x78, 0x4F, 0x2A, 0xE0, 0x7E]
});
}