forked from wathiede/i3xs
Do time color calculation with TZ aware time value.
This commit is contained in:
parent
c4920a1e6e
commit
4790d35fa7
@ -30,13 +30,10 @@ 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(
|
let mut dt = DateTimeWidget::tz("%H:%M %Z".to_string(), "Europe/London".to_string());
|
||||||
"%H:%M %Z".to_string(),
|
|
||||||
"Europe/London".to_string(),
|
|
||||||
);
|
|
||||||
dt.set_colors(&vec![
|
dt.set_colors(&vec![
|
||||||
TimeColor {
|
TimeColor {
|
||||||
start: NaiveTime::from_hms(13, 0, 0),
|
start: NaiveTime::from_hms(15, 0, 0),
|
||||||
color: ColorRGB::yellow(),
|
color: ColorRGB::yellow(),
|
||||||
},
|
},
|
||||||
TimeColor {
|
TimeColor {
|
||||||
|
|||||||
@ -47,27 +47,28 @@ impl DateTimeWidget {
|
|||||||
|
|
||||||
impl Widget for DateTimeWidget {
|
impl Widget for DateTimeWidget {
|
||||||
fn update(&mut self) -> Option<WidgetUpdate> {
|
fn update(&mut self) -> Option<WidgetUpdate> {
|
||||||
let now = chrono::Local::now();
|
let (time, time_string) = match &self.tz {
|
||||||
|
Some(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())
|
||||||
|
}
|
||||||
|
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
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|tc| tc.start < now.time())
|
.filter(|tc| tc.start < time)
|
||||||
.last()
|
.last()
|
||||||
.map(|tc| tc.color.clone())
|
.map(|tc| tc.color.clone())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let time_string = match &self.tz {
|
|
||||||
Some(tz) => {
|
|
||||||
let tz: Tz = tz.parse().unwrap();
|
|
||||||
let now = now.with_timezone(&tz);
|
|
||||||
//TODO(wathiede): convert to timezone.
|
|
||||||
now.format(&self.fmt).to_string()
|
|
||||||
}
|
|
||||||
None => now.format(&self.fmt).to_string(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut data = Block::new().append_full_text(&time_string).clone();
|
let mut data = Block::new().append_full_text(&time_string).clone();
|
||||||
if let Some(color) = color {
|
if let Some(color) = color {
|
||||||
data.color(color);
|
data.color(color);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user