use std::time::{Duration, SystemTime};
use static_assertions::{const_assert, const_assert_eq};
use crate::bindings;
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NDITime(i64);
const NDI_TIME_DEFAULT: i64 = i64::MAX;
const_assert_eq!(NDI_TIME_DEFAULT, bindings::NDIlib_recv_timestamp_undefined);
const_assert_eq!(NDI_TIME_DEFAULT, bindings::NDIlib_send_timecode_synthesize);
impl Default for NDITime {
fn default() -> Self {
Self(NDI_TIME_DEFAULT)
}
}
impl NDITime {
#[inline]
pub fn to_ffi(self) -> i64 {
self.0
}
#[inline]
pub fn from_ffi(time: i64) -> Self {
Self(time)
}
pub fn to_utc(self) -> Option<SystemTime> {
fn to_duration(time: u64) -> Duration {
let secs = time / 10_000_000;
let sub_100ns = time % 10_000_000;
const_assert!(10_000_000 < i32::MAX);
let nanos = (sub_100ns * 100) as u32;
Duration::new(secs, nanos)
}
if self.is_default() {
None
} else {
Some(if self.0 >= 0 {
SystemTime::UNIX_EPOCH + to_duration(self.0 as u64)
} else {
SystemTime::UNIX_EPOCH - to_duration(self.0.saturating_abs() as u64)
})
}
}
pub fn is_default(self) -> bool {
self.0 == NDI_TIME_DEFAULT
}
pub const UNDEFINED: Self = Self(NDI_TIME_DEFAULT);
pub const SYNTHESIZE: Self = Self(NDI_TIME_DEFAULT);
}