| 1 |
use super::Block;
|
| 2 |
use crate::errors::BlockError;
|
| 3 |
use chrono::Local;
|
| 4 |
use std::time::Duration;
|
| 5 |
|
| 6 |
pub struct DateTime {
|
| 7 |
format_template: String,
|
| 8 |
time_format: String,
|
| 9 |
interval: Duration,
|
| 10 |
color: u32,
|
| 11 |
}
|
| 12 |
|
| 13 |
impl DateTime {
|
| 14 |
pub fn new(format_template: &str, time_format: &str, interval_secs: u64, color: u32) -> Self {
|
| 15 |
Self {
|
| 16 |
format_template: format_template.to_string(),
|
| 17 |
time_format: time_format.to_string(),
|
| 18 |
interval: Duration::from_secs(interval_secs),
|
| 19 |
color,
|
| 20 |
}
|
| 21 |
}
|
| 22 |
}
|
| 23 |
|
| 24 |
impl Block for DateTime {
|
| 25 |
fn content(&mut self) -> Result<String, BlockError> {
|
| 26 |
let now = Local::now();
|
| 27 |
let time_str = now.format(&self.time_format).to_string();
|
| 28 |
Ok(self.format_template.replace("{}", &time_str))
|
| 29 |
}
|
| 30 |
|
| 31 |
fn interval(&self) -> Duration {
|
| 32 |
self.interval
|
| 33 |
}
|
| 34 |
|
| 35 |
fn color(&self) -> u32 {
|
| 36 |
self.color
|
| 37 |
}
|
| 38 |
}
|