diff options
-rw-r--r-- | src/main.rs | 30 |
1 files changed, 25 insertions, 5 deletions
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 <https://www.gnu.org/licenses/>. 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<DateTime<Utc>>, + end: Option<DateTime<Utc>>, summary: String, } +fn parse_datetime(datetime: Option<String>) -> Option<DateTime<Utc>> { + 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); } } } |