Browse Source

fix date parsing

master
Astro 2 years ago
parent
commit
6da61433d9
  1. 9
      src/ics/mod.rs
  2. 8
      src/main.rs

9
src/ics/mod.rs

@ -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()
}
}

8
src/main.rs

@ -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);
}
}
});

Loading…
Cancel
Save