-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_term_dates_callendar.pl
executable file
·74 lines (51 loc) · 1.98 KB
/
make_term_dates_callendar.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl
# Prints to STDOUT an ICAL calendar for University of Cambridge Term Dates
use strict;
use warnings;
use Ucam::Term;
use DateTime::Duration;
use DateTime::Span;
use Data::ICal;
use Data::ICal::Entry::Event;
use constant ICAL => '%Y%m%d';
use constant ONE_DAY => DateTime::Duration->new( days => 1 );
my $calendar = Data::ICal->new();
$calendar->add_property('X-WR-CALNAME' => 'UofC Term Dates');
foreach my $year (Ucam::Term->available_years) {
foreach my $term_name ('l', 'e', 'm') {
my $term = Ucam::Term->new($term_name,$year);
next unless $term->dates;
# Make the division into a 1 day span and add it
my $div = DateTime::Span->from_datetime_and_duration
(start => $term->division, duration => ONE_DAY );
add_span($calendar, $div, "Division of term");
# Remove the division 'day' from full term and add the 2 resulting spans
my $full = $term->fullterm_dates->complement($div);
foreach my $span ($full->as_list) {
add_span($calendar, $span, "Full " . $term->name. " term");
}
# Remove full term from term and add the 2 results
my $allterm = $term->dates->complement($term->fullterm_dates);
foreach my $span ($allterm->as_list) {
add_span($calendar, $span, $term->name. " term");
}
# For easter, add general Admission and the Long Vac
if ($term->name eq 'Easter') {
add_span($calendar, $term->general_admission, "General Admission");
add_span($calendar, $term->long_vac, "Long Vacation courses");
}
}
}
# Print the result
print $calendar->as_string;
# -- #
sub add_span {
my ($calendar, $span, $summary) = @_;
my $event = Data::ICal::Entry::Event->new();
$event->add_properties(
summary => $summary,
dtstart => [$span->start->strftime(ICAL), { VALUE => 'DATE' } ],
dtend => [$span->end ->strftime(ICAL), { VALUE => 'DATE' } ],
);
$calendar->add_entry($event);
}