From 6e235aa67e5a9c98b0bd236a7efecfcfb88bcc32 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 16 Aug 2022 11:54:47 +0200 Subject: [PATCH] export calendar as .ics --- src/ics.rs | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/ics.rs b/src/ics.rs index 8287691..e948dc8 100644 --- a/src/ics.rs +++ b/src/ics.rs @@ -3,30 +3,37 @@ use ics::{ Event, ICalendar, }; -pub fn export(timetable: Vec, filename: &str) { +pub fn export(courses: Vec, filename: &str) { let mut calendar = ICalendar::new("2.0", "cal8tor"); - // Create event which contains the information regarding the course - let mut event = Event::new("uid", "dtstamp"); + // Create events which contains the information regarding the course + for course in courses { + let mut event = Event::new("uid", "dtstamp"); - // Add properties - // Public event - event.push(Class::public()); - // Consume actual time - event.push(Transp::opaque()); - // Professor's name - event.push(Description::new("Jean-Jacques Bourdin")); - // Start time of the course - event.push(DtStart::new("20220919T123000Z")); - // End time of the course - event.push(DtEnd::new("20220919T033000Z")); - // Room location - event.push(Location::new("A188")); - // Course's name - event.push(Summary::new("Algorithmique avancée")); + // Public event + event.push(Class::public()); + // Consume actual time + event.push(Transp::opaque()); + // Professor's name + event.push(Description::new(course.professor.unwrap_or_default())); + // Start time of the course + event.push(DtStart::new(dt_ical(course.dtstart.unwrap()))); + // End time of the course + event.push(DtEnd::new(dt_ical(course.dtend.unwrap()))); + // Room location + event.push(Location::new(course.room)); + // Course's name + event.push(Summary::new(course.name)); - // Add the course to the calendar - calendar.add_event(event); + // Add the course to the calendar + calendar.add_event(event); + } calendar.save_file(filename).unwrap(); } + +/// Transform the datetime from chrono to the ICS format +/// See https://github.com/hummingly/ics/issues/17#issue-985662287 +fn dt_ical(dt: chrono::DateTime) -> String { + format!("{}", dt.format("%Y%m%dT%H%M%SZ")) +}