export calendar as .ics

This commit is contained in:
Mylloon 2022-08-16 11:54:47 +02:00
parent 66d8e64f4b
commit 6e235aa67e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -3,30 +3,37 @@ use ics::{
Event, ICalendar, Event, ICalendar,
}; };
pub fn export(timetable: Vec<String>, filename: &str) { pub fn export(courses: Vec<crate::timetable::models::Course>, filename: &str) {
let mut calendar = ICalendar::new("2.0", "cal8tor"); let mut calendar = ICalendar::new("2.0", "cal8tor");
// Create event which contains the information regarding the course // Create events which contains the information regarding the course
for course in courses {
let mut event = Event::new("uid", "dtstamp"); let mut event = Event::new("uid", "dtstamp");
// Add properties
// Public event // Public event
event.push(Class::public()); event.push(Class::public());
// Consume actual time // Consume actual time
event.push(Transp::opaque()); event.push(Transp::opaque());
// Professor's name // Professor's name
event.push(Description::new("Jean-Jacques Bourdin")); event.push(Description::new(course.professor.unwrap_or_default()));
// Start time of the course // Start time of the course
event.push(DtStart::new("20220919T123000Z")); event.push(DtStart::new(dt_ical(course.dtstart.unwrap())));
// End time of the course // End time of the course
event.push(DtEnd::new("20220919T033000Z")); event.push(DtEnd::new(dt_ical(course.dtend.unwrap())));
// Room location // Room location
event.push(Location::new("A188")); event.push(Location::new(course.room));
// Course's name // Course's name
event.push(Summary::new("Algorithmique avancée")); event.push(Summary::new(course.name));
// Add the course to the calendar // Add the course to the calendar
calendar.add_event(event); calendar.add_event(event);
}
calendar.save_file(filename).unwrap(); 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<chrono::Utc>) -> String {
format!("{}", dt.format("%Y%m%dT%H%M%SZ"))
}