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(all(target_arch = "wasm32", target_os = "unknown"))]
fn now_timestamp() -> i64 {
let millis = js_sys::Date::now();
(millis / 1000.0) as i64
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
fn now_timestamp() -> i64 {
chrono::Utc::now().timestamp()
}
impl Times {
pub fn set_creation(&mut self, value: Option<NaiveDateTime>) {
self.creation = value;
}
pub fn set_last_modification(&mut self, value: Option<NaiveDateTime>) {
self.last_modification = value;
}
pub fn set_last_access(&mut self, value: Option<NaiveDateTime>) {
self.last_access = value;
}
pub fn set_expiry_time(&mut self, value: Option<NaiveDateTime>) {
self.expiry = value;
}
pub fn set_location_changed(&mut self, value: Option<NaiveDateTime>) {
self.location_changed = value;
}
pub fn set_expires(&mut self, value: bool) {
self.expires = Some(value);
}
pub fn set_usage_count(&mut self, value: usize) {
self.usage_count = Some(value);
}
pub fn get_last_modification(&self) -> Option<NaiveDateTime> {
self.last_modification
}
pub fn get_creation(&self) -> Option<NaiveDateTime> {
self.creation
}
pub fn get_last_access(&self) -> Option<NaiveDateTime> {
self.last_access
}
pub fn get_expiry_time(&self) -> Option<NaiveDateTime> {
self.expiry
}
pub fn get_location_changed(&self) -> Option<NaiveDateTime> {
self.location_changed
}
pub fn get_expires(&self) -> bool {
self.expires.unwrap_or(false)
}
pub fn get_usage_count(&self) -> usize {
self.usage_count.unwrap_or(0)
}
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),
}
}
}