use core::ops::{Add, Sub};
use crate::duration::Duration;
use crate::math;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Epoch {
sec: i64,
nanos: u32,
}
impl Epoch {
pub fn from_parts(sec: i64, nanos: i64) -> Self {
Self::checked_from_parts(sec, nanos).expect("epoch overflow")
}
pub fn checked_from_parts(sec: i64, nanos: i64) -> Option<Self> {
let total = (sec as i128)
.checked_mul(1_000_000_000)?
.checked_add(nanos as i128)?;
Self::from_nanoseconds(total)
}
pub fn from_tai_seconds(seconds: f64) -> Self {
Self::checked_from_tai_seconds(seconds).expect("invalid or out-of-range epoch")
}
pub fn checked_from_tai_seconds(seconds: f64) -> Option<Self> {
if !seconds.is_finite() {
return None;
}
let whole = math::floor(seconds);
if whole < i64::MIN as f64 || whole >= i64::MAX as f64 {
return None;
}
let frac = seconds - whole;
let nanos = math::round(frac * 1_000_000_000.0) as i64;
Self::checked_from_parts(whole as i64, nanos)
}
pub fn tai_seconds(&self) -> f64 {
self.sec as f64 + self.nanos as f64 * 1e-9
}
pub fn sec(&self) -> i64 {
self.sec
}
pub fn nanos(&self) -> u32 {
self.nanos
}
pub fn checked_add(self, rhs: Duration) -> Option<Self> {
Self::from_nanoseconds(
self.total_nanoseconds()
.checked_add(rhs.total_nanoseconds())?,
)
}
pub fn checked_sub(self, rhs: Duration) -> Option<Self> {
Self::from_nanoseconds(
self.total_nanoseconds()
.checked_sub(rhs.total_nanoseconds())?,
)
}
fn total_nanoseconds(&self) -> i128 {
self.sec as i128 * 1_000_000_000 + self.nanos as i128
}
fn from_nanoseconds(total: i128) -> Option<Self> {
let sec = total.div_euclid(1_000_000_000);
let nanos = total.rem_euclid(1_000_000_000);
if sec < i64::MIN as i128 || sec > i64::MAX as i128 {
return None;
}
Some(Self {
sec: sec as i64,
nanos: nanos as u32,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct UtcDateTime {
pub year: i32,
pub month: u8,
pub day: u8,
pub hour: u8,
pub minute: u8,
pub second: f64,
}
impl UtcDateTime {
pub fn new(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: f64) -> Self {
Self {
year,
month,
day,
hour,
minute,
second,
}
}
}
impl Add<Duration> for Epoch {
type Output = Epoch;
fn add(self, rhs: Duration) -> Self::Output {
self.checked_add(rhs).expect("epoch overflow")
}
}
impl Sub<Duration> for Epoch {
type Output = Epoch;
fn sub(self, rhs: Duration) -> Self::Output {
self.checked_sub(rhs).expect("epoch overflow")
}
}
impl Sub<Epoch> for Epoch {
type Output = Duration;
fn sub(self, rhs: Epoch) -> Self::Output {
let total = self
.total_nanoseconds()
.checked_sub(rhs.total_nanoseconds())
.expect("duration overflow");
Duration::from_nanoseconds(total).expect("duration overflow")
}
}