Have DateTimeWidget include the time zone in its type. #1
@ -25,7 +25,7 @@ fn main() {
|
|||||||
// Realtime upload/download rate for a interface
|
// Realtime upload/download rate for a interface
|
||||||
bar.push(NetworkSpeedWidget::new(&opts.nic, 6));
|
bar.push(NetworkSpeedWidget::new(&opts.nic, 6));
|
||||||
|
|
||||||
let mut dt = DateTimeWidget::tz("%H:%M %Z".to_string(), "Europe/London".to_string());
|
let mut dt = DateTimeWidget::tz("%H:%M %Z".to_string(), chrono_tz::Europe::London);
|
||||||
dt.set_colors(&vec![
|
dt.set_colors(&vec![
|
||||||
TimeColor {
|
TimeColor {
|
||||||
start: NaiveTime::from_hms(0, 0, 0),
|
start: NaiveTime::from_hms(0, 0, 0),
|
||||||
|
|||||||
@ -1,18 +1,16 @@
|
|||||||
use chrono::NaiveTime;
|
use std::fmt::Display;
|
||||||
use chrono_tz::Tz;
|
use chrono::{Local, Utc, NaiveTime, offset::TimeZone};
|
||||||
|
|
||||||
use i3monkit::Block;
|
use i3monkit::Block;
|
||||||
use i3monkit::ColorRGB;
|
use i3monkit::ColorRGB;
|
||||||
use i3monkit::{Widget, WidgetUpdate};
|
use i3monkit::{Widget, WidgetUpdate};
|
||||||
|
|
||||||
/// The widget that shows local time
|
/// The widget that shows local time
|
||||||
pub struct DateTimeWidget {
|
pub struct DateTimeWidget<Tz> {
|
||||||
/// fmt to format the datetime, see
|
/// fmt to format the datetime, see
|
||||||
/// https://docs.rs/chrono/*/chrono/format/strftime/index.html for documentation.
|
/// https://docs.rs/chrono/*/chrono/format/strftime/index.html for documentation.
|
||||||
fmt: String,
|
fmt: String,
|
||||||
/// tz string, values from https://docs.rs/chrono-tz/*/chrono_tz/
|
tz: Tz,
|
||||||
/// None will use local time.
|
|
||||||
tz: Option<String>,
|
|
||||||
colors: Option<Vec<TimeColor>>,
|
colors: Option<Vec<TimeColor>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,19 +20,21 @@ pub struct TimeColor {
|
|||||||
pub color: ColorRGB,
|
pub color: ColorRGB,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DateTimeWidget {
|
impl DateTimeWidget<Local> {
|
||||||
// Create a new time widget
|
|
||||||
pub fn new(fmt: String) -> Self {
|
pub fn new(fmt: String) -> Self {
|
||||||
|
DateTimeWidget::tz(fmt, Local)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Tz> DateTimeWidget<Tz>
|
||||||
|
where
|
||||||
|
Tz: TimeZone,
|
||||||
|
Tz::Offset: Display,
|
||||||
|
|
|||||||
|
{
|
||||||
|
pub fn tz(fmt: String, tz: Tz) -> Self {
|
||||||
DateTimeWidget {
|
DateTimeWidget {
|
||||||
fmt,
|
fmt,
|
||||||
tz: None,
|
tz,
|
||||||
colors: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn tz(fmt: String, tz: String) -> Self {
|
|
||||||
DateTimeWidget {
|
|
||||||
fmt,
|
|
||||||
tz: Some(tz),
|
|
||||||
colors: None,
|
colors: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -45,19 +45,15 @@ impl DateTimeWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widget for DateTimeWidget {
|
impl<Tz> Widget for DateTimeWidget<Tz>
|
||||||
|
where
|
||||||
|
Tz: TimeZone,
|
||||||
|
Tz::Offset: Display,
|
||||||
|
{
|
||||||
fn update(&mut self) -> Option<WidgetUpdate> {
|
fn update(&mut self) -> Option<WidgetUpdate> {
|
||||||
let (time, time_string) = match &self.tz {
|
let (time, time_string) = {
|
||||||
Some(tz) => {
|
let now = Utc::now().with_timezone(&self.tz);
|
||||||
let tz: Tz = tz.parse().unwrap();
|
|
||||||
let now = chrono::Local::now();
|
|
||||||
let now = now.with_timezone(&tz);
|
|
||||||
(now.time(), now.format(&self.fmt).to_string())
|
(now.time(), now.format(&self.fmt).to_string())
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let now = chrono::Local::now();
|
|
||||||
(now.time(), now.format(&self.fmt).to_string())
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let color = if let Some(colors) = &self.colors {
|
let color = if let Some(colors) = &self.colors {
|
||||||
colors
|
colors
|
||||||
@ -81,7 +77,7 @@ impl Widget for DateTimeWidget {
|
|||||||
data.color(color);
|
data.color(color);
|
||||||
}
|
}
|
||||||
Some(WidgetUpdate {
|
Some(WidgetUpdate {
|
||||||
refresh_interval: std::time::Duration::new(1, 0),
|
refresh_interval: std::time::Duration::from_secs(1),
|
||||||
data: Some(data),
|
data: Some(data),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user
I don't get why "Tz::Offset: Display" is used here, can you explain that?
Well I think it may not actually be necessary here (and should probably be removed), but it is necessary on the Widget impl. Short answer for why it's necessary there is because the compiler said so. Longer answer is because the Widget impl formats the time, which requires displaying the timezone which can only be done if the timezone's offset has a Display impl.