From a38d4b19e3b9d8791773b6fd7171cfb07773f9ea Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 17 Dec 2018 21:42:47 +0100 Subject: Parse date/time into proper objects. --- src/main.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 4ce0158..a4728a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,16 +15,33 @@ // along with this program. If not, see . use ical; +use chrono::{DateTime, TimeZone, Utc}; use std::fs::File; use std::io::BufReader; #[derive(Default)] struct Event { - start: String, - end: String, + start: Option>, + end: Option>, summary: String, } +fn parse_datetime(datetime: Option) -> Option> { + if let Some(mut dt) = datetime { + if dt.is_empty() { + return None; + } + + if !dt.contains("T") { + dt += "T000000"; + } + Utc.datetime_from_str(&dt, "%Y%m%dT%H%M%S").ok() + } + else { + None + } +} + fn main() { let buf = BufReader::new(File::open("test.ics").unwrap()); let calendars: Vec<_> = ical::IcalParser::new(buf) @@ -38,14 +55,17 @@ fn main() { for p in e.properties { match p.name.as_ref() { - "DTSTART" => ev.start = p.value.unwrap_or("".to_string()), - "DTEND" => ev.end = p.value.unwrap_or("".to_string()), + "DTSTART" => ev.start = parse_datetime(p.value), + "DTEND" => ev.end = parse_datetime(p.value), "SUMMARY" => ev.summary = p.value.unwrap_or("".to_string()), _ => (), } } - println!("---> {} - {}: {}", ev.start, ev.end, ev.summary); + println!("---> {} - {}: {}", + ev.start.unwrap_or(Utc::now()), + ev.end.unwrap_or(Utc::now()), + ev.summary); } } } -- cgit v1.2.3