summaryrefslogtreecommitdiffstats
path: root/src/icalwrap
diff options
context:
space:
mode:
authorVincent Breitmoser <look@my.amazin.horse>2019-01-19 16:24:36 +0100
committerVincent Breitmoser <look@my.amazin.horse>2019-01-19 16:24:36 +0100
commitb7cbe25c029439cd19a5cd9c9ed65cb34a9e6d85 (patch)
tree13597a0164754412cc1408530800aa1940559219 /src/icalwrap
parent2733f73a485300fc284f2d428b2b31b1f09199fb (diff)
introduce IcalTime type
Diffstat (limited to 'src/icalwrap')
-rw-r--r--src/icalwrap/icaltime.rs63
-rw-r--r--src/icalwrap/icalvcalendar.rs7
-rw-r--r--src/icalwrap/mod.rs2
3 files changed, 68 insertions, 4 deletions
diff --git a/src/icalwrap/icaltime.rs b/src/icalwrap/icaltime.rs
new file mode 100644
index 0000000..243fe46
--- /dev/null
+++ b/src/icalwrap/icaltime.rs
@@ -0,0 +1,63 @@
+use std::ops::Deref;
+use chrono::prelude::*;
+use ical;
+use utils::dateutil;
+
+pub struct IcalTime {
+ time: ical::icaltimetype,
+}
+
+impl IcalTime {
+ pub fn now() -> Self {
+ dateutil::now().into()
+ }
+}
+
+unsafe fn tz_utc() -> *mut ical::_icaltimezone {
+ ical::icaltimezone_get_utc_timezone()
+}
+
+impl Deref for IcalTime {
+ type Target = ical::icaltimetype;
+
+ fn deref(&self) -> &ical::icaltimetype {
+ &self.time
+ }
+}
+
+impl From<DateTime<Local>> for IcalTime {
+ fn from(time: DateTime<Local>) -> IcalTime {
+ let timestamp = time.timestamp();
+ let is_date = 0;
+ let time = unsafe {
+ ical::icaltime_from_timet_with_zone(timestamp, is_date, tz_utc())
+ };
+
+ IcalTime{ time }
+ }
+}
+
+impl From<DateTime<Utc>> for IcalTime {
+ fn from(time: DateTime<Utc>) -> IcalTime {
+ let timestamp = time.timestamp();
+ let is_date = 0;
+ let time = unsafe {
+ ical::icaltime_from_timet_with_zone(timestamp, is_date, tz_utc())
+ };
+
+ IcalTime{ time }
+ }
+}
+
+impl<T: TimeZone> From<Date<T>> for IcalTime {
+ fn from(date: Date<T>) -> IcalTime {
+ let timestamp = date.with_timezone(&Utc).and_hms(0, 0, 0).timestamp();
+ let is_date = 1;
+ let time = unsafe {
+ ical::icaltime_from_timet_with_zone(timestamp, is_date, tz_utc())
+ };
+
+ IcalTime{ time }
+ }
+}
+
diff --git a/src/icalwrap/icalvcalendar.rs b/src/icalwrap/icalvcalendar.rs
index b76e853..fdb1c1d 100644
--- a/src/icalwrap/icalvcalendar.rs
+++ b/src/icalwrap/icalvcalendar.rs
@@ -6,6 +6,7 @@ use std::io;
use super::IcalVEvent;
use super::IcalComponent;
+use super::IcalTime;
use ical;
use utils::dateutil;
@@ -130,11 +131,9 @@ impl IcalVCalendar {
}
pub fn with_dtstamp_now(self) -> Self {
+ let dtstamp = IcalTime::now();
unsafe {
- let now = dateutil::now().timestamp();
- let is_date = 0;
- let now_icaltime = ical::icaltime_from_timet_with_zone(now, is_date, ical::icaltimezone_get_utc_timezone());
- ical::icalcomponent_set_dtstamp(self.get_ptr(), now_icaltime);
+ ical::icalcomponent_set_dtstamp(self.get_ptr(), *dtstamp);
}
self
}
diff --git a/src/icalwrap/mod.rs b/src/icalwrap/mod.rs
index 12d1a6f..09e8279 100644
--- a/src/icalwrap/mod.rs
+++ b/src/icalwrap/mod.rs
@@ -2,9 +2,11 @@ mod icalvcalendar;
mod icalvevent;
mod icalproperty;
mod icalcomponent;
+mod icaltime;
pub use self::icalvcalendar::IcalVCalendar;
pub use self::icalvcalendar::IcalEventIter;
pub use self::icalvevent::IcalVEvent;
pub use self::icalproperty::IcalProperty;
pub use self::icalcomponent::IcalComponent;
+use self::icaltime::IcalTime;