use crate::units::DurationSeconds;
use std::time::{Duration, SystemTime};
#[derive(Debug, Clone)]
pub struct Timeline(pub(crate) SystemTime);
#[derive(Debug, Clone)]
pub struct TimeOffset(pub(crate) Duration);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
any(test, feature = "property-test-api"),
derive(test_strategy::Arbitrary)
)]
pub struct TimeOffsetSeconds(pub(crate) DurationSeconds);
impl From<SystemTime> for Timeline {
fn from(s: SystemTime) -> Self {
Timeline(s)
}
}
impl From<DurationSeconds> for TimeOffsetSeconds {
fn from(v: DurationSeconds) -> Self {
TimeOffsetSeconds(v)
}
}
impl From<TimeOffsetSeconds> for TimeOffset {
fn from(v: TimeOffsetSeconds) -> TimeOffset {
TimeOffset(v.0.into())
}
}
impl From<TimeOffsetSeconds> for u64 {
fn from(v: TimeOffsetSeconds) -> Self {
v.0.into()
}
}
impl Timeline {
pub fn new(start_time: SystemTime) -> Self {
Timeline(start_time)
}
pub fn differential(&self, t: &SystemTime) -> Option<TimeOffset> {
match t.duration_since(self.0) {
Ok(duration) => Some(TimeOffset(duration)),
Err(_) => None,
}
}
pub fn advance(&self, d: Duration) -> Self {
Timeline(self.0 + d)
}
}