From c61a8075a98c4674694f3ab25f15cca54f9958f8 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 12 Jan 2019 15:38:57 +0100 Subject: use Local for instance timestamps instead of Utc --- src/icalwrap/icalvcalendar.rs | 10 +++++----- src/icalwrap/icalvevent.rs | 8 +++++--- src/khline.rs | 17 +++++++++++------ src/utils/dateutil.rs | 5 +++-- 4 files changed, 24 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/icalwrap/icalvcalendar.rs b/src/icalwrap/icalvcalendar.rs index 19c182c..dd6a5af 100644 --- a/src/icalwrap/icalvcalendar.rs +++ b/src/icalwrap/icalvcalendar.rs @@ -1,4 +1,4 @@ -use chrono::{DateTime, Utc}; +use chrono::{DateTime, Local}; use std::ffi::{CStr, CString}; use std::path::{PathBuf,Path}; use std::rc::Rc; @@ -10,7 +10,7 @@ use ical; pub struct IcalVCalendar { comp: Rc, path: Option, - instance_timestamp: Option>, + instance_timestamp: Option>, } pub struct IcalEventIter<'a> { @@ -57,7 +57,7 @@ impl IcalVCalendar { } } - pub fn with_internal_timestamp(mut self, datetime: DateTime) -> IcalVCalendar { + pub fn with_internal_timestamp(mut self, datetime: DateTime) -> IcalVCalendar { self.instance_timestamp = Some(datetime); self } @@ -321,7 +321,7 @@ impl Drop for IcalComponentOwner { mod tests { use super::*; use testdata; - use chrono::{Local, TimeZone}; + use chrono::{Utc, Local, TimeZone}; #[test] fn test_from_str_empty() { @@ -415,7 +415,7 @@ mod tests { fn test_with_internal_timestamp() { let cal = IcalVCalendar::from_str(testdata::TEST_EVENT_MULTIDAY, None).unwrap(); - let timestamp = Utc.ymd(2018, 1, 1).and_hms(11, 30, 20); + let timestamp = Local.ymd(2018, 1, 1).and_hms(11, 30, 20); let new_cal = cal.with_internal_timestamp(timestamp); let event = new_cal.get_principal_event(); diff --git a/src/icalwrap/icalvevent.rs b/src/icalwrap/icalvevent.rs index d51003f..278d737 100644 --- a/src/icalwrap/icalvevent.rs +++ b/src/icalwrap/icalvevent.rs @@ -8,7 +8,7 @@ use ical; pub struct IcalVEvent { ptr: *mut ical::icalcomponent, parent: Option, - instance_timestamp: Option>, + instance_timestamp: Option>, } impl Drop for IcalVEvent { @@ -123,7 +123,7 @@ impl IcalVEvent { result } - pub fn with_internal_timestamp(&self, datetime: DateTime) -> IcalVEvent { + pub fn with_internal_timestamp(&self, datetime: DateTime) -> IcalVEvent { IcalVEvent { ptr: self.ptr, parent: self.parent.as_ref().map(|parent| parent.shallow_copy()), @@ -132,7 +132,9 @@ impl IcalVEvent { } pub fn get_recur_instances(&self) -> impl Iterator + '_ { - self.get_recur_datetimes().into_iter().map(move |rec| self.with_internal_timestamp(rec)) + self.get_recur_datetimes().into_iter() + .map(|recur_utc| recur_utc.with_timezone(&Local)) + .map(move |recur_local| self.with_internal_timestamp(recur_local)) } pub fn get_parent(&self) -> Option<&IcalVCalendar> { diff --git a/src/khline.rs b/src/khline.rs index 82fcf2d..a3e0a03 100644 --- a/src/khline.rs +++ b/src/khline.rs @@ -6,13 +6,13 @@ use icalwrap::IcalVEvent; pub struct KhLine { path: PathBuf, - time: DateTime, + time: Option>, } impl KhLine { pub fn from(event: &IcalVEvent) -> Option { - let time = event.get_dtstart()?; let path = event.get_parent()?.get_path()?.to_path_buf(); + let time = event.get_dtstart(); Some(KhLine{ path, time }) } @@ -23,11 +23,16 @@ impl KhLine { } impl fmt::Display for KhLine { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let time_string = format!("{:010}", self.time.timestamp()); - let path_string = self.path.to_string_lossy(); - write!(f, "{} {}", time_string, path_string) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let path_string = self.path.to_string_lossy(); + match self.time { + Some(time) => { + let time_string = format!("{:010}", time.timestamp()); + write!(f, "{} {}", time_string, path_string) + } + None => write!(f, "{}", path_string) } + } } #[cfg(test)] diff --git a/src/utils/dateutil.rs b/src/utils/dateutil.rs index 84818ce..1272834 100644 --- a/src/utils/dateutil.rs +++ b/src/utils/dateutil.rs @@ -36,10 +36,11 @@ pub fn week_from_str_end(date_str: &str) -> Result,String> { Err("Could not parse '{}' as week".to_string()) } -pub fn datetime_from_timestamp(timestamp: &str) -> Option> { +pub fn datetime_from_timestamp(timestamp: &str) -> Option> { let timestamp_i64 = timestamp.parse::().ok()?; let naive_datetime = NaiveDateTime::from_timestamp_opt(timestamp_i64, 0)?; - Some(DateTime::from_utc(naive_datetime, Utc)) + let utc_time: DateTime = DateTime::from_utc(naive_datetime, Utc); + Some(utc_time.with_timezone(&Local)) } #[cfg(test)] -- cgit v1.2.3