use std::ops::{Add, AddAssign, Sub};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::units::{Duration, Seconds};
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TimePoint(Seconds);
impl TimePoint {
pub fn as_seconds(&self) -> Seconds {
self.0
}
pub fn as_secs_f64(&self) -> f64 {
self.as_seconds().as_f64()
}
pub fn from_secs_f64(seconds: f64) -> Self {
Self(Seconds::from(seconds))
}
}
impl PartialEq<Seconds> for TimePoint {
fn eq(&self, other: &Seconds) -> bool {
self.as_seconds() == *other
}
}
impl From<Seconds> for TimePoint {
fn from(value: Seconds) -> Self {
value.as_time_point()
}
}
impl Sub<Duration> for TimePoint {
type Output = Self;
fn sub(self, rhs: Duration) -> Self::Output {
Self::from(self.as_seconds() - rhs.as_seconds())
}
}
impl Add<Duration> for TimePoint {
type Output = Self;
fn add(self, rhs: Duration) -> Self::Output {
Self::from(self.as_seconds() + rhs.as_seconds())
}
}
impl Sub<Seconds> for TimePoint {
type Output = Self;
fn sub(self, rhs: Seconds) -> Self::Output {
Self::from(self.as_seconds() - rhs)
}
}
impl Add<Seconds> for TimePoint {
type Output = Self;
fn add(self, rhs: Seconds) -> Self::Output {
Self::from(self.as_seconds() + rhs)
}
}
impl AddAssign<Seconds> for TimePoint {
fn add_assign(&mut self, rhs: Seconds) {
self.0 += rhs;
}
}
impl Sub<Self> for TimePoint {
type Output = Duration;
fn sub(self, rhs: Self) -> Self::Output {
Duration::from(self.as_seconds() - rhs.as_seconds())
}
}