From e961d5f8b04e6f8f203490625fe2b17f9f3cfdef Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sat, 6 Jan 2018 03:49:26 +0200 Subject: [PATCH] Use lazystatic to cache the current chrono date. This will backfire on every new year's eve. --- hammond-gtk/src/widgets/episode.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 51f7677..4651f1d 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -9,7 +9,6 @@ use humansize::{file_size_opts as size_opts, FileSize}; use hammond_data::dbqueries; use hammond_data::{EpisodeWidgetQuery, Podcast}; -// use hammond_data::utils::*; use hammond_data::errors::*; use hammond_downloader::downloader; @@ -72,6 +71,10 @@ impl Default for EpisodeWidget { } } +lazy_static! { + static ref NOW: DateTime = Utc::now(); +} + impl EpisodeWidget { pub fn new(episode: &mut EpisodeWidgetQuery, sender: Sender) -> EpisodeWidget { let widget = EpisodeWidget::default(); @@ -148,9 +151,8 @@ impl EpisodeWidget { /// Set the date label depending on the current time. fn set_date(&self, epoch: i32) { - let now = Utc::now(); let date = Utc.timestamp(i64::from(epoch), 0); - if now.year() == date.year() { + if NOW.year() == date.year() { self.date.set_text(&date.format("%e %b").to_string().trim()); } else { self.date @@ -173,6 +175,10 @@ impl EpisodeWidget { /// Set the Episode label dependings on its size fn set_size(&self, bytes: Option) { + if (bytes == Some(0)) || bytes.is_none() { + return; + }; + // Declare a custom humansize option struct // See: https://docs.rs/humansize/1.0.2/humansize/file_size_opts/struct.FileSizeOpts.html let custom_options = size_opts::FileSizeOpts { @@ -188,13 +194,11 @@ impl EpisodeWidget { }; if let Some(size) = bytes { - if size != 0 { - let s = size.file_size(custom_options); - if let Ok(s) = s { - self.size.set_text(&s); - self.size.show(); - self.separator2.show(); - } + let s = size.file_size(custom_options); + if let Ok(s) = s { + self.size.set_text(&s); + self.size.show(); + self.separator2.show(); } }; }