From 300711affe7c45be59075c0d163601f48216a361 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Wed, 22 Apr 2020 00:52:08 +0200 Subject: [PATCH] Convert DateTime to NaiveDateTime Use of DateTime can lead to excessive inspection of the system timezone. Instead, propagate only a timezone-compensated NaiveDateTime throughout the widgets. --- src/app.rs | 2 +- src/config.rs | 21 +++++++++++++-------- src/main.rs | 5 +++-- src/widget.rs | 8 ++++---- src/widgets/calendar.rs | 12 ++++++------ src/widgets/clock.rs | 8 ++++---- src/widgets/date.rs | 8 ++++---- 7 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/app.rs b/src/app.rs index b0acf8c..fe74423 100644 --- a/src/app.rs +++ b/src/app.rs @@ -220,7 +220,7 @@ impl App { }; let inner = self.inner.lock().unwrap(); - let time = Local::now(); + let time = Local::now().naive_local(); if !inner.visible || inner.shell_surfaces.len() != *inner.configured_surfaces.lock().unwrap() diff --git a/src/config.rs b/src/config.rs index 7f217cc..960534e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,7 @@ use crate::cmd::Cmd; use crate::color::Color; use crate::widget; use crate::widgets; +use chrono::NaiveDateTime; use serde::{Deserialize, Serialize}; use std::default::Default; use std::sync::mpsc::Sender; @@ -63,9 +64,13 @@ pub enum Widget { } impl Widget { - pub fn construct(self, tx: Sender) -> Option> { + pub fn construct( + self, + time: &NaiveDateTime, + tx: Sender, + ) -> Option> { match self { - Widget::Margin { margins, widget } => match widget.construct(tx.clone()) { + Widget::Margin { margins, widget } => match widget.construct(time, tx.clone()) { Some(w) => Some(widget::Margin::new(margins, w)), None => None, }, @@ -73,14 +78,14 @@ impl Widget { width, height, widget, - } => match widget.construct(tx.clone()) { + } => match widget.construct(time, tx.clone()) { Some(w) => Some(widget::Fixed::new((width, height), w)), None => None, }, Widget::HorizontalLayout(widgets) => Some(widget::HorizontalLayout::new( widgets .into_iter() - .map(|x| x.construct(tx.clone())) + .map(|x| x.construct(time, tx.clone())) .filter(|x| x.is_some()) .map(|x| x.unwrap()) .collect(), @@ -88,17 +93,17 @@ impl Widget { Widget::VerticalLayout(widgets) => Some(widget::VerticalLayout::new( widgets .into_iter() - .map(|x| x.construct(tx.clone())) + .map(|x| x.construct(time, tx.clone())) .filter(|x| x.is_some()) .map(|x| x.unwrap()) .collect(), )), - Widget::Clock { font_size } => Some(widgets::clock::Clock::new(font_size)), - Widget::Date { font_size } => Some(widgets::date::Date::new(font_size)), + Widget::Clock { font_size } => Some(widgets::clock::Clock::new(time, font_size)), + Widget::Date { font_size } => Some(widgets::date::Date::new(time, font_size)), Widget::Calendar { font_size, sections, - } => Some(widgets::calendar::Calendar::new(font_size, sections)), + } => Some(widgets::calendar::Calendar::new(time, font_size, sections)), Widget::Launcher { font_size, length, diff --git a/src/main.rs b/src/main.rs index 4af1560..051195b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,8 +161,9 @@ fn main() { let tx_draw_mod = tx_draw.clone(); let (mod_tx, mod_rx) = channel(); std::thread::spawn(move || { + let n = Local::now().naive_local(); // Print, write to a file, or send to an HTTP server. - match config.widget.construct(tx_draw_mod) { + match config.widget.construct(&n, tx_draw_mod) { Some(w) => mod_tx.send(w).unwrap(), None => panic!("no widget configured"), } @@ -269,7 +270,7 @@ fn main() { app.get_widget().wait(&mut wait_ctx); if let Some(target_time) = wait_ctx.target_time { - let n = Local::now(); + let n = Local::now().naive_local(); let sleep = if target_time > n { target_time - n } else { diff --git a/src/widget.rs b/src/widget.rs index c4dd7b5..43c6d3b 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -1,13 +1,13 @@ use crate::buffer::Buffer; use crate::color::Color; -use chrono::{DateTime, Local}; +use chrono::NaiveDateTime; use nix::poll::PollFd; pub use smithay_client_toolkit::keyboard::{KeyState, ModifiersState}; pub struct DrawContext<'a> { pub buf: &'a mut Buffer<'a>, pub bg: &'a Color, - pub time: &'a DateTime, + pub time: &'a NaiveDateTime, pub force: bool, } @@ -32,11 +32,11 @@ impl DrawReport { pub struct WaitContext { pub fds: Vec, - pub target_time: Option>, + pub target_time: Option, } impl WaitContext { - pub fn set_time(&mut self, new_time: DateTime) { + pub fn set_time(&mut self, new_time: NaiveDateTime) { if let Some(ot) = self.target_time { if new_time < ot { self.target_time = Some(new_time); diff --git a/src/widgets/calendar.rs b/src/widgets/calendar.rs index dbbf85f..43d407c 100644 --- a/src/widgets/calendar.rs +++ b/src/widgets/calendar.rs @@ -3,10 +3,10 @@ use crate::color::Color; use crate::draw::{Font, DEJAVUSANS_MONO, ROBOTO_REGULAR}; use crate::widget::{DrawContext, DrawReport, KeyState, ModifiersState, WaitContext, Widget}; -use chrono::{Date, Datelike, Local}; +use chrono::{Datelike, NaiveDate, NaiveDateTime}; pub struct Calendar { - cur_date: Date, + cur_date: NaiveDate, dirty: bool, offset: f64, sections: u32, @@ -22,8 +22,8 @@ impl Calendar { &self, buf: &mut Buffer, background_color: &Color, - orig: &Date, - time: &Date, + orig: &NaiveDate, + time: &NaiveDate, ) -> Result<(i32, i32, i32, i32), ::std::io::Error> { let mut time = time.clone(); let mut y_off = 1; @@ -153,7 +153,7 @@ impl Calendar { } impl Calendar { - pub fn new(font_size: f32, sections: u32) -> Box { + pub fn new(time: &NaiveDateTime, font_size: f32, sections: u32) -> Box { let mut calendar_cache = Font::new(&DEJAVUSANS_MONO, font_size * 2.0); calendar_cache.add_str_to_cache("0123456789"); let mut month_cache = Font::new(&ROBOTO_REGULAR, font_size * 4.0); @@ -163,7 +163,7 @@ impl Calendar { let mut day_cache = Font::new(&DEJAVUSANS_MONO, font_size); day_cache.add_str_to_cache("MONTUEWDHFRISA"); Box::new(Calendar { - cur_date: Local::now().date(), + cur_date: time.date(), dirty: true, offset: 0.0, sections: sections, diff --git a/src/widgets/clock.rs b/src/widgets/clock.rs index 1620500..d6e6d50 100644 --- a/src/widgets/clock.rs +++ b/src/widgets/clock.rs @@ -2,21 +2,21 @@ use crate::color::Color; use crate::draw::{Font, ROBOTO_REGULAR}; use crate::widget::{DrawContext, DrawReport, KeyState, ModifiersState, WaitContext, Widget}; -use chrono::{DateTime, Duration, Local, Timelike}; +use chrono::{Duration, NaiveDateTime, Timelike}; pub struct Clock { - cur_time: DateTime, + cur_time: NaiveDateTime, clock_cache: Font, size: f32, } impl Clock { - pub fn new(size: f32) -> Box { + pub fn new(time: &NaiveDateTime, size: f32) -> Box { let mut clock_cache = Font::new(&ROBOTO_REGULAR, size); clock_cache.add_str_to_cache("0123456789:"); Box::new(Clock { - cur_time: Local::now(), + cur_time: time.clone(), clock_cache: clock_cache, size: size, }) diff --git a/src/widgets/date.rs b/src/widgets/date.rs index 05364bb..5cf273a 100644 --- a/src/widgets/date.rs +++ b/src/widgets/date.rs @@ -2,22 +2,22 @@ use crate::color::Color; use crate::draw::{Font, ROBOTO_REGULAR}; use crate::widget::{DrawContext, DrawReport, KeyState, ModifiersState, WaitContext, Widget}; -use chrono::{DateTime, Datelike, Local}; +use chrono::{Datelike, NaiveDateTime}; pub struct Date { - cur_time: DateTime, + cur_time: NaiveDateTime, date_cache: Font, size: f32, } impl Date { - pub fn new(size: f32) -> Box { + pub fn new(time: &NaiveDateTime, size: f32) -> Box { let mut date_cache = Font::new(&ROBOTO_REGULAR, size); date_cache .add_str_to_cache("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,/ "); Box::new(Date { - cur_time: Local::now(), + cur_time: time.clone(), date_cache: date_cache, size: size, })