fix date parsing

This commit is contained in:
Astro 2019-10-11 21:24:43 +02:00
parent e7b024e843
commit 6da61433d9
2 changed files with 12 additions and 5 deletions

View File

@ -1,5 +1,5 @@
use std::collections::HashMap;
use chrono::NaiveDateTime;
use chrono::{NaiveDate, NaiveDateTime};
mod tokenizer;
mod parser;
@ -33,12 +33,17 @@ impl<'a> GetValue<'a, String> for Object {
}
// TODO: TZID, offset
// TODO: enum for all day events?
impl<'a> GetValue<'a, NaiveDateTime> for Object {
fn get(&self, key: &'_ str) -> Option<NaiveDateTime> {
let s = self.get(key)?;
NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%SZ")
.or_else(|_| NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%S"))
.or_else(|_| NaiveDateTime::parse_from_str(s, "%Y%m%d"))
.or_else(|_|
NaiveDate::parse_from_str(s, "%Y%m%d")
.map(|date| date.and_hms(0, 0, 0))
)
.map_err(|e| println!("Cannot parse date: {:?}", e))
.ok()
}
}

View File

@ -29,7 +29,7 @@ fn extract_vevent_objs(results: &mut Vec<Object>, mut obj: Object) {
}
}
fn obj_to_event(calendar: String, obj: Object) -> Option<Event> {
fn obj_to_event(calendar: String, obj: &Object) -> Option<Event> {
if obj.name != "VEVENT" {
return None;
}
@ -155,11 +155,13 @@ impl Resources {
let mut objs = vec![];
extract_vevent_objs(&mut objs, obj);
for obj in objs {
let dbg = format!("{:?}", obj);
if let Some(event) = obj_to_event(cal_opts.url.clone(), &obj) {
events.insert(event.id.clone(), event);
} else {
println!("ignore {}", dbg);
let dtstart: Option<&str> = obj.get("DTSTART");
let summary: Option<&str> = obj.get("SUMMARY");
println!("cannot convert {} {:?} {:?}",
obj.name, dtstart, summary);
}
}
});