use std::ops::{Deref, DerefMut};
use std::time::UNIX_EPOCH;
use chrono::{DateTime, NaiveDateTime, Utc};
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct DateTimeUtc(DateTime<Utc>);
impl Default for DateTimeUtc {
fn default() -> Self {
Self(DateTime::<Utc>::from(UNIX_EPOCH))
}
}
impl Deref for DateTimeUtc {
type Target = DateTime<Utc>;
fn deref(&self) -> &DateTime<Utc> {
&self.0
}
}
impl DerefMut for DateTimeUtc {
fn deref_mut(&mut self) -> &mut DateTime<Utc> {
&mut self.0
}
}
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
impl From<f64> for DateTimeUtc {
fn from(f: f64) -> Self {
let timestamp = f + 978_307_200_f64;
let seconds = timestamp.trunc() as i64;
let nanoseconds = timestamp.fract() * 1_000_000_000.0;
let datetime = NaiveDateTime::from_timestamp_opt(seconds, nanoseconds as u32).unwrap();
DateTimeUtc(DateTime::from_utc(datetime, Utc))
}
}