diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-12-17 21:42:47 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-12-17 21:42:47 +0100 |
commit | a38d4b19e3b9d8791773b6fd7171cfb07773f9ea (patch) | |
tree | fff9fb35b4ca93b5d14fa8754f47f8a92823c0a3 | |
parent | bde7175e5bbcc7f31b52d28bc53e5746528db9b4 (diff) | |
download | icaltool-a38d4b19e3b9d8791773b6fd7171cfb07773f9ea.tar.gz icaltool-a38d4b19e3b9d8791773b6fd7171cfb07773f9ea.tar.bz2 icaltool-a38d4b19e3b9d8791773b6fd7171cfb07773f9ea.zip |
Parse date/time into proper objects.
-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); } } } |