#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timescale(u64);
impl Timescale {
pub const NANOSECOND: Self = Self(1_000_000_000);
pub const fn new(v: u64) -> Self {
Self(v)
}
}
#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Time(pub i64);
impl re_byte_size::SizeBytes for Time {
fn heap_size_bytes(&self) -> u64 {
0
}
fn is_pod() -> bool {
true
}
}
const fn const_round_f64(v: f64) -> i64 {
if v > 0.0 {
(v + 0.5) as i64
} else {
(v - 0.5) as i64
}
}
impl Time {
pub const ZERO: Self = Self(0);
pub const MAX: Self = Self(i64::MAX);
pub const MIN: Self = Self(i64::MIN);
#[inline]
pub fn new(v: i64) -> Self {
Self(v)
}
#[inline]
pub const fn from_secs(secs_since_start: f64, timescale: Timescale) -> Self {
Self(const_round_f64(secs_since_start * timescale.0 as f64))
}
#[inline]
pub const fn from_millis(millis_since_start: f64, timescale: Timescale) -> Self {
Self::from_secs(millis_since_start / 1e3, timescale)
}
#[inline]
pub const fn from_micros(micros_since_start: f64, timescale: Timescale) -> Self {
Self::from_secs(micros_since_start / 1e6, timescale)
}
#[inline]
pub const fn from_nanos(nanos_since_start: i64, timescale: Timescale) -> Self {
Self::from_secs(nanos_since_start as f64 / 1e9, timescale)
}
#[inline]
pub fn duration(self, timescale: Timescale) -> std::time::Duration {
std::time::Duration::from_nanos(self.into_nanos(timescale) as _)
}
#[inline]
pub fn into_secs(self, timescale: Timescale) -> f64 {
self.0 as f64 / timescale.0 as f64
}
#[inline]
pub fn into_millis(self, timescale: Timescale) -> f64 {
self.into_secs(timescale) * 1e3
}
#[inline]
pub fn into_micros(self, timescale: Timescale) -> f64 {
self.into_secs(timescale) * 1e6
}
#[inline]
pub fn into_nanos(self, timescale: Timescale) -> i64 {
(self.into_secs(timescale) * 1e9).round() as i64
}
}
impl std::fmt::Debug for Time {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::ops::Add for Time {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
Self(self.0.saturating_add(rhs.0))
}
}
impl std::ops::Sub for Time {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0.saturating_sub(rhs.0))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_const_round_f64() {
assert_eq!(const_round_f64(1.5), 2);
assert_eq!(const_round_f64(2.5), 3);
assert_eq!(const_round_f64(1.499999999), 1);
assert_eq!(const_round_f64(2.499999999), 2);
assert_eq!(const_round_f64(-1.5), -2);
assert_eq!(const_round_f64(-2.5), -3);
assert_eq!(const_round_f64(-1.499999999), -1);
assert_eq!(const_round_f64(-2.499999999), -2);
}
}