diff --git a/Cargo.toml b/Cargo.toml index 1d41910..ca43f63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,9 @@ keywords = ["orgmode","emacs","parser"] travis-ci = { repository = "PoiScript/orgize" } [dependencies] -bytecount = "0.5.1" -jetscii = "0.4.3" +bytecount = "0.5" +chrono = { version = "0.4", optional = true } +jetscii = "0.4" memchr = "2" [dev-dependencies] diff --git a/src/objects/timestamp.rs b/src/objects/timestamp.rs index 22e6813..6493f23 100644 --- a/src/objects/timestamp.rs +++ b/src/objects/timestamp.rs @@ -8,6 +8,39 @@ pub struct Datetime<'a> { pub dayname: &'a str, } +#[cfg(feature = "chrono")] +mod chrono { + use super::Datetime; + use chrono::*; + + impl<'a> Datetime<'a> { + pub fn naive_date(&self) -> NaiveDate { + let (y, m, d) = self.date; + NaiveDate::from_ymd(y.into(), m.into(), d.into()) + } + + pub fn naive_time(&self) -> NaiveTime { + if let Some((h, m)) = self.time { + NaiveTime::from_hms(h.into(), m.into(), 0) + } else { + NaiveTime::from_hms(0, 0, 0) + } + } + + pub fn naive_date_time(&self) -> NaiveDateTime { + NaiveDateTime::new(self.naive_date(), self.naive_time()) + } + + pub fn date_time(&self, offset: Tz::Offset) -> DateTime { + DateTime::from_utc(self.naive_date_time(), offset) + } + + pub fn date(&self, offset: Tz::Offset) -> Date { + Date::from_utc(self.naive_date(), offset) + } + } +} + #[cfg_attr(test, derive(PartialEq))] #[derive(Debug, Clone, Copy)] pub enum RepeaterType {