use chrono::NaiveDateTime;
#[derive(Debug, Default, PartialEq, Eq, Clone)]
#[non_exhaustive]
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
pub struct Times {
pub creation: Option<NaiveDateTime>,
pub last_modification: Option<NaiveDateTime>,
pub last_access: Option<NaiveDateTime>,
pub expiry: Option<NaiveDateTime>,
pub location_changed: Option<NaiveDateTime>,
pub expires: Option<bool>,
pub usage_count: Option<usize>,
}
#[cfg(not(target_arch = "wasm32"))]
fn now_timestamp() -> i64 {
chrono::Utc::now().timestamp()
}
#[cfg(target_arch = "wasm32")]
fn now_timestamp() -> i64 {
let millis = js_sys::Date::now();
(millis / 1000.0) as i64
}
impl Times {
pub fn now() -> NaiveDateTime {
let now = now_timestamp();
chrono::DateTime::from_timestamp(now, 0).unwrap().naive_utc()
}
pub fn epoch() -> NaiveDateTime {
chrono::DateTime::from_timestamp(0, 0).unwrap().naive_utc()
}
pub fn new() -> Self {
let now = Times::now();
Times {
creation: Some(now),
last_modification: Some(now),
last_access: Some(now),
expiry: None,
location_changed: Some(now),
expires: Some(false),
usage_count: Some(0),
}
}
}