Convert DateTime<Local> to NaiveDateTime

Use of DateTime<Local> can lead to excessive inspection of the system
timezone.

Instead, propagate only a timezone-compensated NaiveDateTime throughout
the widgets.
This commit is contained in:
Kenny Levinsen
2020-04-22 00:52:08 +02:00
parent c380d20794
commit 300711affe
7 changed files with 35 additions and 29 deletions
+1 -1
View File
@@ -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()
+13 -8
View File
@@ -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<Cmd>) -> Option<Box<dyn widget::Widget + Send>> {
pub fn construct(
self,
time: &NaiveDateTime,
tx: Sender<Cmd>,
) -> Option<Box<dyn widget::Widget + Send>> {
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,
+3 -2
View File
@@ -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 {
+4 -4
View File
@@ -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<Local>,
pub time: &'a NaiveDateTime,
pub force: bool,
}
@@ -32,11 +32,11 @@ impl DrawReport {
pub struct WaitContext {
pub fds: Vec<PollFd>,
pub target_time: Option<DateTime<Local>>,
pub target_time: Option<NaiveDateTime>,
}
impl WaitContext {
pub fn set_time(&mut self, new_time: DateTime<Local>) {
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);
+6 -6
View File
@@ -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<Local>,
cur_date: NaiveDate,
dirty: bool,
offset: f64,
sections: u32,
@@ -22,8 +22,8 @@ impl Calendar {
&self,
buf: &mut Buffer,
background_color: &Color,
orig: &Date<Local>,
time: &Date<Local>,
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<Calendar> {
pub fn new(time: &NaiveDateTime, font_size: f32, sections: u32) -> Box<Calendar> {
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,
+4 -4
View File
@@ -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<Local>,
cur_time: NaiveDateTime,
clock_cache: Font,
size: f32,
}
impl Clock {
pub fn new(size: f32) -> Box<Clock> {
pub fn new(time: &NaiveDateTime, size: f32) -> Box<Clock> {
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,
})
+4 -4
View File
@@ -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<Local>,
cur_time: NaiveDateTime,
date_cache: Font,
size: f32,
}
impl Date {
pub fn new(size: f32) -> Box<Date> {
pub fn new(time: &NaiveDateTime, size: f32) -> Box<Date> {
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,
})