use std::prelude::v1::*;
#[cfg(feature = "moment")]
use core::fmt;
#[cfg(feature = "moment")]
use core::fmt::Formatter;
use std::sync::Arc;
#[cfg(feature = "moment")]
#[derive(Clone)]
pub struct Moment {
generator: Arc<fn() -> chrono::DateTime<chrono::Utc>>
}
#[cfg(feature = "moment")]
impl Moment {
pub fn new(func: Option<fn() -> chrono::DateTime<chrono::Utc>>) -> Self {
let time_func = {
#[cfg(feature = "default")] {
match func {
Some(f) => f,
None => chrono::Utc::now
}
}
#[cfg(not(feature = "default"))] {
match func {
Some(f) => f,
None => panic!("No time function supplied")
}
}
};
Self {
generator: Arc::new(time_func)
}
}
pub fn now(&self) -> chrono::DateTime<chrono::Utc> {
(self.generator)()
}
pub fn get_function(&self) -> fn() -> chrono::DateTime<chrono::Utc> {
*self.generator.clone()
}
}
#[cfg(all(feature = "moment", feature = "default"))]
impl Default for Moment {
fn default() -> Self {
Self {
generator: Arc::new(chrono::Utc::now)
}
}
}
#[cfg(feature = "moment")]
impl core::fmt::Debug for Moment {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}