apple_quant_algorithmic/timestamp/
point.rs1use std::ops::{Add, Deref, Sub};
2
3use chrono::{Duration, TimeDelta};
4
5use crate::timestamp::TradeTimestamp;
6
7use super::{BinIndex, TickTimestamp, Timestamped, UtcNs};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Timestamp(UtcNs);
11
12impl Timestamp {
13 pub(crate) fn now() -> Self {
14 UtcNs::now().into()
15 }
16
17 pub fn as_utc_ns(
18 &self,
19 ) -> &UtcNs {
20 &self.0
21 }
22
23 pub fn into_utc_ns(
24 self,
25 ) -> UtcNs {
26 self.0
27 }
28}
29
30impl From<UtcNs> for Timestamp {
31 fn from(
32 value: UtcNs,
33 ) -> Self {
34 Self(value)
35 }
36}
37
38impl From<TradeTimestamp> for Timestamp {
39 fn from(
40 value: TradeTimestamp,
41 ) -> Self {
42 *value
43 }
44}
45
46impl From<TickTimestamp> for Timestamp {
47 fn from(
48 value: TickTimestamp,
49 ) -> Self {
50 *value
51 }
52}
53
54impl<const NS_LEN: u64> From<BinIndex<NS_LEN>> for Timestamp {
55 fn from(
56 value: BinIndex<NS_LEN>,
57 ) -> Self {
58 value.timestamp()
59 }
60}
61
62impl Add<Duration> for Timestamp {
63 type Output = Self;
64
65 #[inline]
66 fn add(
67 mut self,
68 rhs: Duration,
69 ) -> Self::Output {
70 self.0 = self.0 + rhs;
71 self
72 }
73}
74
75impl Add<&Duration> for Timestamp {
76 type Output = Self;
77
78 #[inline]
79 fn add(
80 self,
81 rhs: &Duration,
82 ) -> Self::Output {
83 self + *rhs
84 }
85}
86
87impl Sub<Duration> for Timestamp {
88 type Output = Self;
89
90 #[inline]
91 fn sub(
92 mut self,
93 rhs: Duration,
94 ) -> Self::Output {
95 self.0 = self.0 - rhs;
96 self
97 }
98}
99
100impl Sub<&Duration> for Timestamp {
101 type Output = Self;
102
103 #[inline]
104 fn sub(
105 self,
106 rhs: &Duration,
107 ) -> Self::Output {
108 self - *rhs
109 }
110}
111
112impl Sub for Timestamp {
113 type Output = TimeDelta;
114
115 fn sub(
116 self,
117 rhs: Self,
118 ) -> Self::Output {
119 self.0 - rhs.0
120 }
121}
122
123impl Sub<&Timestamp> for Timestamp {
124 type Output = TimeDelta;
125
126 fn sub(
127 self,
128 rhs: &Timestamp,
129 ) -> Self::Output {
130 self.0 - rhs.0
131 }
132}
133
134impl Deref for Timestamp {
135 type Target = UtcNs;
136
137 fn deref(
138 &self,
139 ) -> &Self::Target {
140 &self.0
141 }
142}