Skip to main content

apple_quant_algorithmic/timestamp/bin/
index.rs

1use crate::timestamp::{Timestamp, TimestampType, Timestamped, UtcNs};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub struct BinIndex<const NS_LEN: u64>(u64);
5
6impl<const NS_LEN: u64> BinIndex<NS_LEN> {
7	pub(super) fn from_timestamped(
8		timestamped: impl Timestamped,
9	) -> Self {
10		let timestamp = timestamped.timestamp();
11		let ns = timestamp.as_timestamp_type();
12		let idx = ns / NS_LEN as TimestampType;
13		let idx = idx as u64;
14
15		Self(idx)
16	}
17
18	pub fn as_timestamp(
19		&self,
20	) -> Timestamp {
21		(*self).into()
22	}
23
24	pub fn into_timestamp(
25		self,
26	) -> Timestamp {
27		self.into()
28	}
29}
30
31impl<const NS_LEN: u64> Timestamped for BinIndex<NS_LEN> {
32	fn timestamp(
33		&self,
34	) -> Timestamp {
35		let idx = self.0 as TimestampType;
36		let utc_ns = idx * NS_LEN as TimestampType;
37
38		let utc_ns = unsafe {
39			UtcNs::new_unchecked(utc_ns)
40		};
41
42		utc_ns.into()
43	}
44}