use std::ops::{Add, Deref, Sub};
use chrono::{Duration, TimeDelta};
use crate::timestamp::TradeTimestamp;
use super::{BinIndex, TickTimestamp, Timestamped, UtcNs};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timestamp(UtcNs);
impl Timestamp {
pub(crate) fn now() -> Self {
UtcNs::now().into()
}
pub fn as_utc_ns(
&self,
) -> &UtcNs {
&self.0
}
pub fn into_utc_ns(
self,
) -> UtcNs {
self.0
}
}
impl From<UtcNs> for Timestamp {
fn from(
value: UtcNs,
) -> Self {
Self(value)
}
}
impl From<TradeTimestamp> for Timestamp {
fn from(
value: TradeTimestamp,
) -> Self {
*value
}
}
impl From<TickTimestamp> for Timestamp {
fn from(
value: TickTimestamp,
) -> Self {
*value
}
}
impl<const NS_LEN: u64> From<BinIndex<NS_LEN>> for Timestamp {
fn from(
value: BinIndex<NS_LEN>,
) -> Self {
value.timestamp()
}
}
impl Add<Duration> for Timestamp {
type Output = Self;
#[inline]
fn add(
mut self,
rhs: Duration,
) -> Self::Output {
self.0 = self.0 + rhs;
self
}
}
impl Add<&Duration> for Timestamp {
type Output = Self;
#[inline]
fn add(
self,
rhs: &Duration,
) -> Self::Output {
self + *rhs
}
}
impl Sub<Duration> for Timestamp {
type Output = Self;
#[inline]
fn sub(
mut self,
rhs: Duration,
) -> Self::Output {
self.0 = self.0 - rhs;
self
}
}
impl Sub<&Duration> for Timestamp {
type Output = Self;
#[inline]
fn sub(
self,
rhs: &Duration,
) -> Self::Output {
self - *rhs
}
}
impl Sub for Timestamp {
type Output = TimeDelta;
fn sub(
self,
rhs: Self,
) -> Self::Output {
self.0 - rhs.0
}
}
impl Sub<&Timestamp> for Timestamp {
type Output = TimeDelta;
fn sub(
self,
rhs: &Timestamp,
) -> Self::Output {
self.0 - rhs.0
}
}
impl Deref for Timestamp {
type Target = UtcNs;
fn deref(
&self,
) -> &Self::Target {
&self.0
}
}