use chrono::{DateTime, Local, Utc};
pub(crate) const TIMESPEC_WIRE_SIZE: usize = 16;
#[derive(Debug, Clone, Copy)]
pub struct TimeSpec {
pub sec: i64, pub nsec: i64, }
impl TimeSpec {
pub fn from_buffer(buffer: &[u8]) -> Option<Self> {
if buffer.len() < TIMESPEC_WIRE_SIZE {
return None;
}
let sec = i64::from_le_bytes(buffer[0..8].try_into().ok()?);
let nsec = i64::from_le_bytes(buffer[8..16].try_into().ok()?);
Some(Self { sec, nsec })
}
pub fn to_date_time(&self) -> DateTime<Local> {
self.to_utc_date_time().with_timezone(&Local)
}
pub fn to_utc_date_time(&self) -> DateTime<Utc> {
DateTime::from_timestamp(self.sec, self.nsec as u32).expect("Invalid TimeSpec")
}
}