Commit e961d5f8 authored by Jordan Petridis's avatar Jordan Petridis 🌱

Use lazystatic to cache the current chrono date. This will backfire on every new year's eve.

parent 5fed283f
Pipeline #1855 passed with stages
in 26 minutes and 20 seconds
......@@ -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> = Utc::now();
}
impl EpisodeWidget {
pub fn new(episode: &mut EpisodeWidgetQuery, sender: Sender<Action>) -> 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<i32>) {
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();
}
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment