export calendar as .ics
This commit is contained in:
parent
66d8e64f4b
commit
6e235aa67e
1 changed files with 27 additions and 20 deletions
47
src/ics.rs
47
src/ics.rs
|
@ -3,30 +3,37 @@ use ics::{
|
|||
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");
|
||||
|
||||
// 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<chrono::Utc>) -> String {
|
||||
format!("{}", dt.format("%Y%m%dT%H%M%SZ"))
|
||||
}
|
||||
|
|
Reference in a new issue