use crate::{ATTOS_PER_SEC, Dt, GregorianTime, Scale};
mod arithmetic;
mod constructors;
mod formatting;
mod from_str;
mod ops;
pub mod time_units;
#[cfg(feature = "alloc")]
mod to_str;
#[cfg(feature = "hifitime")]
mod from_hifitime;
#[cfg(feature = "hifitime")]
mod to_hifitime;
#[cfg(feature = "chrono")]
mod from_chrono;
#[cfg(feature = "chrono")]
mod to_chrono;
#[cfg(feature = "jiff")]
mod from_jiff;
#[cfg(feature = "jiff")]
mod to_jiff;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "js", derive(tsify::Tsify))]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TSpan {
pub sec: i64,
pub attos: u64,
}
impl TSpan {
#[inline]
pub const fn sec(&self) -> i64 {
self.sec
}
#[inline]
pub const fn attos(&self) -> u64 {
self.attos
}
#[inline]
pub const fn carry_over(&mut self) -> &mut Self {
if self.attos >= ATTOS_PER_SEC {
self.sec += (self.attos / ATTOS_PER_SEC) as i64;
self.attos %= ATTOS_PER_SEC;
}
self
}
#[inline]
pub const fn to_tai(&self, current: Scale) -> Dt {
Dt::from(self.sec, self.attos, current)
}
#[inline]
pub const fn to(&self, current: Scale, target: Scale) -> TSpan {
Dt::from(self.sec, self.attos, current).to(target)
}
#[inline]
pub const fn to_gregorian_time(&self, current: Scale) -> GregorianTime {
Dt::from(self.sec, self.attos, current).to_gregorian_time()
}
#[inline]
pub const fn to_epoch(&self, epoch: Dt, current: Scale) -> Self {
Dt::from(self.sec, self.attos, current).to_epoch(epoch, current)
}
}
impl Default for TSpan {
fn default() -> Self {
Self::ZERO
}
}
#[cfg(feature = "wire")]
impl TSpan {
pub const WIRE_VERSION: u8 = 1;
pub const WIRE_SIZE: usize = 17;
pub fn to_wire_bytes(&self) -> [u8; Self::WIRE_SIZE] {
let mut buf = [0u8; Self::WIRE_SIZE];
buf[0] = Self::WIRE_VERSION;
buf[1..9].copy_from_slice(&self.sec.to_le_bytes());
buf[9..17].copy_from_slice(&self.attos.to_le_bytes());
buf
}
pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self> {
if bytes.len() != Self::WIRE_SIZE {
return None;
}
if bytes[0] != Self::WIRE_VERSION {
return None;
}
let sec = i64::from_le_bytes([
bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8],
]);
let attos = u64::from_le_bytes([
bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15], bytes[16],
]);
Some(Self::new(sec, attos))
}
}