use std::fmt;
use crate::Update;
use chrono::{DateTime, Local};
pub struct TimePlugin {
pub time: String,
pub format: String,
}
impl TimePlugin {
pub fn new(format: &str) -> Box<Self> {
let time: DateTime<Local> = Local::now();
let time = format!("{}", time.format(&format));
Box::new(Self {
time,
format: format.to_string(),
})
}
}
impl fmt::Display for TimePlugin {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.time)
}
}
impl Update for TimePlugin {
fn refresh(&mut self) {
let dt: DateTime<Local> = Local::now();
self.time = format!("{}", dt.format(&self.format));
}
}
impl Default for TimePlugin {
fn default() -> TimePlugin {
let dt: DateTime<Local> = Local::now();
let format = "%A %D %_I:%M:%S %P".to_string();
TimePlugin {
time: format!("{}", dt.format(&format)),
format,
}
}
}