From ee0f45dfae708fdced05570421e796df16d024a3 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Sun, 20 Mar 2022 00:37:36 -0600 Subject: [PATCH] =?UTF-8?q?fix:=20Use=20local=20datetime=20for=20calendar?= =?UTF-8?q?=20styles=20=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.rs | 24 +++++++++++++++--------- src/calendar.rs | 19 +++++++++++-------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index e1a1d90..f310b9e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -33,7 +33,7 @@ use unicode_segmentation::Graphemes; use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; -use chrono::{Datelike, Local, NaiveDate, NaiveDateTime, TimeZone, Timelike}; +use chrono::{Datelike, FixedOffset, Local, NaiveDate, NaiveDateTime, TimeZone, Timelike}; use anyhow::Context as AnyhowContext; use anyhow::{anyhow, Result}; @@ -777,17 +777,21 @@ impl TaskwarriorTui { } } - pub fn get_dates_with_styles(&self) -> Vec<(NaiveDate, Style)> { - let mut tasks_with_styles = vec![]; - + pub fn get_dates_with_styles(&self) -> Vec<(chrono::Date, Style)> { if !self.tasks.is_empty() { let tasks = &self.tasks; - let tasks_with_due_dates = tasks.iter().filter(|t| t.due().is_some()); - - tasks_with_styles - .extend(tasks_with_due_dates.map(|t| (t.due().unwrap().clone().date(), self.style_for_task(t)))); + tasks + .iter() + .filter_map(|t| t.due().map(|d| (d.clone(), self.style_for_task(t)))) + .map(|(d, t)| { + let now = Local::now(); + let reference = TimeZone::from_utc_datetime(now.offset(), &d); + (reference.date(), t) + }) + .collect() + } else { + vec![] } - tasks_with_styles } pub fn get_position(lb: &LineBuffer) -> usize { @@ -4815,7 +4819,9 @@ mod tests { let backend = TestBackend::new(80, 20); let mut terminal = Terminal::new(backend).unwrap(); + app.render(&mut terminal).unwrap(); + dbg!(app.get_dates_with_styles()); println!("{}", buffer_view(terminal.backend().buffer())); } diff --git a/src/calendar.rs b/src/calendar.rs index f94a05f..6e54076 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -5,7 +5,7 @@ use std::fmt; const COL_WIDTH: usize = 21; -use chrono::{Datelike, Duration, Local, Month, NaiveDate, NaiveDateTime, TimeZone}; +use chrono::{format::Fixed, Date, Datelike, Duration, FixedOffset, Local, Month, NaiveDate, NaiveDateTime, TimeZone}; use tui::{ buffer::Buffer, @@ -24,7 +24,7 @@ pub struct Calendar<'a> { pub month: u32, pub style: Style, pub months_per_row: usize, - pub date_style: Vec<(NaiveDate, Style)>, + pub date_style: Vec<(Date, Style)>, pub today_style: Style, pub title_background_color: Color, } @@ -70,7 +70,7 @@ impl<'a> Calendar<'a> { self } - pub fn date_style(mut self, date_style: Vec<(NaiveDate, Style)>) -> Self { + pub fn date_style(mut self, date_style: Vec<(Date, Style)>) -> Self { self.date_style = date_style; self } @@ -112,10 +112,10 @@ impl<'a> Widget for Calendar<'a> { let months: Vec<_> = (0..12).collect(); - let mut days: Vec<_> = months + let mut days: Vec<(Date, Date)> = months .iter() .map(|i| { - let first = NaiveDate::from_ymd(year, i + 1, 1); + let first = Date::from_utc(NaiveDate::from_ymd(year, i + 1, 1), *Local::now().offset()); ( first, first - Duration::days(i64::from(first.weekday().num_days_from_sunday())), @@ -193,13 +193,13 @@ impl<'a> Widget for Calendar<'a> { if let Some(i) = index { style = self.date_style[i].1; } - if d.1 == Local::today().naive_local() { + if d.1 == Local::today() { buf.set_string(x, y, s, self.today_style); } else { buf.set_string(x, y, s, style); } x += 3; - d.1 += Duration::days(1); + d.1 = Date::from_utc(d.1.naive_local() + Duration::days(1), *Local::now().offset()); } moredays |= d.0.month() == d.1.month() || d.1 < d.0; } @@ -219,7 +219,10 @@ impl<'a> Widget for Calendar<'a> { &mut months .iter() .map(|i| { - let first = NaiveDate::from_ymd(self.year + new_year as i32, i + 1, 1); + let first = Date::from_utc( + NaiveDate::from_ymd(self.year + new_year as i32, i + 1, 1), + *Local::now().offset(), + ); ( first, first - Duration::days(i64::from(first.weekday().num_days_from_sunday())),