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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use chrono::{DateTime, Local, TimeZone, Utc};
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::ops::{Add, Div, Mul, Sub};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Ticks {
val: u64,
}
impl Ticks {
pub const PER_SECOND: u64 = 10_000_000;
pub const PER_MILLISECOND: u64 = Self::PER_SECOND;
pub const PER_MICROSECOND: u64 = Self::PER_SECOND / 1_000_000;
pub const PER_MINUTE: u64 = 60 * Self::PER_SECOND;
pub const PER_HOUR: u64 = 60 * Self::PER_MINUTE;
pub const PER_DAY: u64 = 24 * Self::PER_HOUR;
pub const LEAP_SECOND_FLAG: u64 = 1 << 63;
pub const LEAP_SECOND_DIRECTION: u64 = 1 << 62;
pub const VALUE_MASK: u64 = !Self::LEAP_SECOND_FLAG & !Self::LEAP_SECOND_DIRECTION;
pub const UNIX_BASE_OFFSET: u64 = 621_355_968_000_000_000;
pub fn new(val: u64) -> Self {
Self { val }
}
pub fn timestamp_value(&self) -> u64 {
self.val & Self::VALUE_MASK
}
pub fn from_datetime<Tz: TimeZone>(dt: DateTime<Tz>) -> Self
where
DateTime<Tz>: From<SystemTime>,
{
let duration = dt
.signed_duration_since::<Tz>(UNIX_EPOCH.into())
.to_std()
.unwrap_or_default();
Self {
val: Self::from_duration(duration).val + Self::UNIX_BASE_OFFSET,
}
}
pub fn from_duration(duration: Duration) -> Self {
Self {
val: duration.as_secs() * Self::PER_SECOND + (duration.subsec_nanos() as u64) / 100,
}
}
pub fn to_datetime(&self) -> DateTime<Utc> {
let value = self.timestamp_value() - Self::UNIX_BASE_OFFSET;
let secs = value / Self::PER_SECOND;
let nanos = (value % Self::PER_SECOND) * 100;
let duration = Duration::new(secs, nanos as u32);
DateTime::<Utc>::from(UNIX_EPOCH + duration)
}
pub fn is_leap_second(&self) -> bool {
(self.val & Self::LEAP_SECOND_FLAG) > 0
}
pub fn set_leap_second(&self) -> Self {
Self {
val: self.val | Self::LEAP_SECOND_FLAG,
}
}
pub fn apply_leap_second(&mut self) {
self.val |= Self::LEAP_SECOND_FLAG;
}
pub fn is_negative_leap_second(&self) -> bool {
self.is_leap_second() && (self.val & Self::LEAP_SECOND_DIRECTION) > 0
}
pub fn set_negative_leap_second(&self) -> Self {
Self {
val: self.val | Self::LEAP_SECOND_FLAG | Self::LEAP_SECOND_DIRECTION,
}
}
pub fn apply_negative_leap_second(&mut self) {
self.val |= Self::LEAP_SECOND_FLAG | Self::LEAP_SECOND_DIRECTION;
}
pub fn now() -> Self {
let now: DateTime<Local> = Local::now();
Self::from_datetime(now)
}
pub fn utc_now() -> Self {
let now: DateTime<Utc> = Utc::now();
Self::from_datetime(now)
}
pub fn to_string(&self) -> String {
let result = self
.to_datetime()
.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true)
.replace("T", " ");
result[..result.len() - 1].to_string()
}
pub fn to_short_string(&self) -> String {
let datetime_str = self.to_string();
let result = datetime_str.split(" ").nth(1).unwrap_or("").to_string();
result[..result.len() - 6].to_string()
}
}
impl Display for Ticks {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{}", self.to_string())
}
}
impl Add for Ticks {
type Output = Ticks;
fn add(self, rhs: Ticks) -> Self {
Ticks {
val: self.val + rhs.val,
}
}
}
impl Sub for Ticks {
type Output = Ticks;
fn sub(self, rhs: Ticks) -> Self {
Ticks {
val: self.val - rhs.val,
}
}
}
impl Mul for Ticks {
type Output = Ticks;
fn mul(self, rhs: Ticks) -> Self {
Ticks {
val: self.val * rhs.val,
}
}
}
impl Div for Ticks {
type Output = Ticks;
fn div(self, rhs: Ticks) -> Self {
Ticks {
val: self.val / rhs.val,
}
}
}
#[cfg(test)]
mod tests {
use super::Ticks;
use chrono::{DateTime, TimeZone, Timelike, Utc};
use lazy_static::lazy_static;
const TEST_TICK_VAL: u64 = 637669683993391278;
lazy_static! {
static ref TEST_DATETIME: DateTime<Utc> = init_test_datetime();
static ref TEST_TICKS: Ticks = Ticks { val: TEST_TICK_VAL };
static ref TEST_DATETIME_VAL: String = "2021-09-11 14:46:39.339127800".to_string();
}
fn init_test_datetime() -> DateTime<Utc> {
Utc.with_ymd_and_hms(2021, 9, 11, 14, 46, 39)
.unwrap()
.with_nanosecond(339127800)
.unwrap()
}
#[test]
fn test_ticks_from_datetime() {
let ticks = Ticks::from_datetime(*TEST_DATETIME);
assert_eq!(ticks.val, TEST_TICK_VAL);
}
#[test]
fn test_ticks_to_datetime() {
let dt = TEST_TICKS.to_datetime();
assert_eq!(dt, *TEST_DATETIME);
}
#[test]
fn test_ticks_is_leap_second() {
let ticks = *TEST_TICKS;
let leap_second_ticks = ticks.set_leap_second();
assert!(!ticks.is_leap_second());
assert!(leap_second_ticks.is_leap_second());
}
#[test]
fn test_ticks_set_leap_second() {
let ticks = *TEST_TICKS;
let leap_second_ticks = ticks.set_leap_second();
assert_eq!(leap_second_ticks.val, ticks.val | Ticks::LEAP_SECOND_FLAG);
}
#[test]
fn test_ticks_is_negative_leap_second() {
let ticks = *TEST_TICKS;
let negative_leap_second_ticks = ticks.set_negative_leap_second();
assert!(!ticks.is_negative_leap_second());
assert!(negative_leap_second_ticks.is_negative_leap_second());
}
#[test]
fn test_ticks_set_negative_leap_second() {
let ticks = *TEST_TICKS;
let negative_leap_second_ticks = ticks.set_negative_leap_second();
assert_eq!(
negative_leap_second_ticks.val,
ticks.val | Ticks::LEAP_SECOND_FLAG | Ticks::LEAP_SECOND_DIRECTION
);
}
#[test]
fn test_ticks_to_string() {
let ticks = *TEST_TICKS;
let string_representation = ticks.to_string();
assert_eq!(string_representation, *TEST_DATETIME_VAL);
}
#[test]
fn test_ticks_to_short_string() {
let ticks = *TEST_TICKS;
let short_string_representation = ticks.to_short_string();
assert_eq!(short_string_representation, "14:46:39.339");
}
}