use nix::sys::time::TimeSpec;
use serde::{Deserialize, Serialize};
use super::inner::{Diff, Time};
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
pub struct Utc;
impl super::inner::Type for Utc {}
impl super::inner::NanoType for Utc {
const INSTANT_PREFIX: &'static str = "Instant";
const DURATION_PREFIX: &'static str = "Duration";
}
pub type Instant = Time<Utc>;
pub type Duration = Diff<Utc>;
impl From<Instant> for TimeSpec {
fn from(value: Instant) -> Self {
let seconds = value.as_seconds_trunc();
let nanoseconds = value.as_nanos_trunc() % 1_000_000_000;
TimeSpec::new(seconds, nanoseconds)
}
}
#[cfg(test)]
mod test {
use super::*;
use rstest::rstest;
#[rstest]
#[case(Instant::from_nanos(0), TimeSpec::new(0, 0))]
#[case(Instant::from_nanos(1), TimeSpec::new(0, 1))]
#[case(Instant::from_nanos(1_000), TimeSpec::new(0, 1_000))]
#[case(Instant::from_nanos(1_000_000_000), TimeSpec::new(1, 0))]
fn timespec_from_instant(#[case] instant: Instant, #[case] expected: TimeSpec) {
assert_eq!(TimeSpec::from(instant), expected);
}
#[rstest]
#[case::zero(Instant::new(i64::MIN))]
#[case::max(Instant::new(i64::MAX))]
fn timespec_from_instant_should_not_panic(#[case] instant: Instant) {
let _ = TimeSpec::from(instant);
}
}