/usr/share/perl5/Oxford/Calendar.pm is in liboxford-calendar-perl 2.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 | # Oxford University calendar conversion.
# Simon Cozens (c) 1999-2002
# Eugene van der Pijll (c) 2004
# University of Oxford (c) 2007-2015
# Dominic Hargreaves (c) 2016
# Artistic License
package Oxford::Calendar;
$Oxford::Calendar::VERSION = "2.11";
use strict;
use Text::Abbrev;
use Date::Calc qw(Add_Delta_Days Decode_Date_EU Delta_Days Mktime Easter_Sunday Date_to_Days Day_of_Week_to_Text Day_of_Week);
use YAML;
use Time::Seconds;
use Time::Piece;
use constant CALENDAR => '/etc/oxford-calendar.yaml';
use constant SEVEN_WEEKS => 7 * ONE_WEEK;
use constant DEFAULT_MODE => 'nearest';
use constant TERMS => qw(Michaelmas Hilary Trinity);
use constant DAYS => qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
# Constants defined by University regulations
use constant MICHAELMAS_START => (10, 1);
use constant MICHAELMAS_END => (12, 17);
use constant HILARY_START => (1, 7);
use constant HILARY_END_IF_EARLIER => (3, 25);
use constant TRINITY_START_IF_LATER => (4, 20);
use constant TRINITY_END => (7, 6);
=head1 NAME
Oxford::Calendar - University of Oxford calendar conversion routines
=head1 SYNOPSIS
use 5.10.0;
use Oxford::Calendar;
use Date::Calc;
say "Today is " . Oxford::Calendar::ToOx(reverse Date::Calc::Today);
=head1 DESCRIPTION
This module converts University of Oxford dates (Oxford academic dates)
to and from Real World dates, and provides information on Terms of the
University.
The Terms of the University are defined by the
B<Regulations on the number and lengths of terms>, available online from
L<https://www.admin.ox.ac.uk/examregs/2015-16/rotnalengofterm/>
This document describes the start and end dates of Oxford Terms.
In addition to this, the dates of Full Term, required to calculate the
week number of the term, are prescribed by Council, and published
periodically in the B<University Gazette>.
Full term comprises weeks 1-8 inclusive, but sometimes, dates outside of
full term are presented in the Oxford academic date format.
This module will optionally provide such dates.
Data for these prescribed dates may be supplied in the file
F</etc/oxford-calendar.yaml>; if this file does not exist, built-in data
will be used. The built-in data is periodically updated from the
semi-authoritative source at
L<http://www.ox.ac.uk/about/facts-and-figures/dates-of-term>.
or the authoritative source, the Gazette, available online from
L<http://www.ox.ac.uk/gazette/>.
L<http://www.ox.ac.uk/news-and-events/The-University-Year>
describes the academic year at Oxford.
=head1 DATE FORMAT
An Oxford academic date has the following format:
=over
<day of week>, <week number>[st,nd,rd,th] week, <term name> <year>
=back
where term name is one of
=over
=item *
Michaelmas (autumn)
=item *
Hilary (spring)
=item *
Trinity (summer)
=back
Example:
Friday, 8th Week, Michaelmas 2007
=cut
our %db;
my $_initcal; # If this is true, we have our database of dates already.
my $_initrange;
my @_oxford_full_terms;
sub _get_week_suffix {
my $week = shift;
die "_get_week_suffix: No week given" unless defined $week;
my $wsuffix = "th";
abs($week) == 1 && ( $wsuffix = "st" );
abs($week) == 2 && ( $wsuffix = "nd" );
abs($week) == 3 && ( $wsuffix = "rd" );
return $wsuffix;
}
sub _find_week {
my $tm = shift;
my $sweek = shift;
my $sweek_tm = shift;
my $eow = $sweek_tm + ONE_WEEK;
while ( $tm >= $eow ) {
$eow += ONE_WEEK;
$sweek++;
}
return $sweek;
}
sub _init_db {
my $db;
if ( -r CALENDAR ) {
$db = YAML::LoadFile(CALENDAR);
}
else {
my $data = join '', <DATA>;
$db = YAML::Load($data);
}
%db = %{ $db->{Calendar} };
}
sub _init_range {
foreach my $termspec ( keys %db ) {
next unless $db{$termspec};
my $time = eval { Time::Piece->strptime($db{$termspec}->{start}, '%d/%m/%Y' ) }
or die
"Could not decode date ($db{$termspec}->{start}) for term $termspec: $@";
push @_oxford_full_terms,
[$time, ($time + SEVEN_WEEKS), split(/ /, $termspec), $db{$termspec}->{provisional}];
}
$_initrange++;
}
sub _fmt_oxdate_as_string {
my ( $dow, $week, $term, $year ) = @_;
my $wsuffix = _get_week_suffix($week);
return "$dow, $week$wsuffix week, $term $year";
}
sub _increment_term {
my ( $year, $term ) = @_;
if ( $term eq 'Michaelmas' ) {
return $year + 1, 'Hilary';
} elsif ( $term eq 'Hilary' ) {
return $year, 'Trinity'
} elsif ( $term eq 'Trinity' ) {
return $year, 'Michaelmas';
} else {
die "_increment_term: Unknown term $term";
}
}
sub _sunday_of_first {
my ( $year, $term ) = @_;
Init() unless defined $_initcal;
my $date = $db{"$term $year"};
return undef unless $date;
return ( $date->{provisional}, Decode_Date_EU($date->{start}) );
}
sub _to_ox_nearest {
my @date = @_;
my $confirmed = pop @date;
my $week;
my @term;
_init_range() unless defined $_initrange;
my $dow = Day_of_Week_to_Text( Day_of_Week( @date ) );
my $tm = Time::Piece->strptime(join('/', @date[0..2]), '%Y/%m/%d');
my @terms = sort { $a->[0] <=> $b->[0] } @_oxford_full_terms;
my ( $prevterm, $nextterm );
my $curterm = shift @terms;
while ($curterm) {
if ( $tm < $curterm->[0] ) {
if ( $prevterm && $tm >= ($prevterm->[1] + ONE_WEEK) ) {
$nextterm = $curterm;
last;
} else {
die "Date out of range";
}
}
$prevterm = $curterm;
$curterm = shift @terms;
}
return undef unless $nextterm;
# We are in the gap between terms .. which one is closest?
my $prevgap = $tm - ($prevterm->[1] + ONE_WEEK);
my $nextgap = $tm - $nextterm->[0];
if ( abs($prevgap) < abs($nextgap) ) {
# if equal go for -<n>th week
$week = _find_week( $tm, 8, $prevterm->[1] );
@term = @{$prevterm};
} else {
my $delta = $nextgap / (24 * 60 * 60);
$week = 1 + int( $delta / 7 );
$week -= 1 if $delta % 7;
@term = @{$nextterm};
}
return undef if $term[4] && $confirmed;
return ($dow, $week, $term[2], $term[3]) if ( wantarray );
return _fmt_oxdate_as_string( $dow, $week, $term[2], $term[3] );
}
sub Init {
_init_db;
Date::Calc::Language(Date::Calc::Decode_Language('English'));
$_initcal++;
}
=head1 FUNCTIONS
=over 3
=item ToOx($day, $month, $year, [\%options])
Given a day, month and year in standard human format (that is, month is
1-12, not 0-11, and year is four digits) will return a string of the
form
Day, xth week, Term year
or an array
(Day, week of term, Term, year)
depending on how it is called. The exact behaviour is modified by the 'mode'
option described below.
If the requested date is not in full term or extended term (see below),
undef will be returned.
If the requested date is not covered by the database, ToOx will die with
an "out of range" error message. Therefore it is recommended to eval ToOx
with appropriate error handling.
%options can contain additional named parameter options:
=over 5
=item mode
Several modes are available:
=over 6
=item full_term
Term dates will only be returned if the date requested is part of a full
term (as defined by the web page above).
=item ext_term
Term dates will only be returned if the date requested is part of an extended
term, or statutory term.
=item nearest
Will return term dates based on the nearest term, even if the date requested
is not part of an extended term (i.e. will include fictional week numbers).
This is currently the default behaviour, for backwards compatibility with
previous releases; this may be changed in future.
=back
=back
=over 4
=item confirmed
If true, ignores dates marked as provisional in the database.
=back
=cut
sub ToOx {
my (@dmy, $options);
($dmy[0], $dmy[1], $dmy[2], $options) = @_;
my $mode = $options->{mode} || DEFAULT_MODE;
my ($week, @term);
my @date = reverse @dmy;
Init unless defined $_initcal;
my $dow = Day_of_Week_to_Text( Day_of_Week( @date ) );
@term = ThisTerm( @date );
if ( $#term ) {
# We're in term
my @term_start = _sunday_of_first( @term );
my $provisional = shift @term_start;
die "Date out of range" unless ( $#term_start == 2 );
my $days_from_start = Delta_Days( @term_start, @date );
my $week_offset = $days_from_start < 0 ? 1 : 7;
my $week = int( ( $days_from_start + $week_offset ) / 7);
return undef if $options->{confirmed} && $provisional;
return undef if ( ( $week < 1 || $week > 8 ) && $mode eq 'full_term' );
return ( $dow, $week, $term[1], $term[0] ) if ( wantarray );
return _fmt_oxdate_as_string( $dow, $week, $term[1], $term[0] );
} else {
return undef if $mode eq 'full_term';
return undef if $mode eq 'ext_term';
return _to_ox_nearest( @date, $options->{confirmed} );
}
}
=item ThisTerm($year, $month, $day)
Given a year, month, term in standard human format (that is, month is
1-12, not 0-11, and year is four digits) will returns the current term
or undef if in vacation or unknown. The term is given as an array in the
form (year, term).
=cut
sub ThisTerm {
my ( $year, $month, $day ) = @_;
my $term_dates = StatutoryTermDates( $year );
foreach my $term ( keys %{$term_dates} ) {
my $start = Date_to_Days( @{$term_dates->{$term}->{start}} );
my $end = Date_to_Days( @{$term_dates->{$term}->{end}} );
my $date = Date_to_Days( $year, $month, $day );
if ( ( $date >= $start ) && ( $date <= $end )) {
return ( $year, $term );
}
}
return undef;
}
=item NextTerm($year, $month, $day)
Given a day, month and year in standard human format (that is, month is
1-12, not 0-11, and year is four digits) will return the next term (whether
or not the date given is in term time).
The term is given as an array in the form (year, term).
=cut
sub NextTerm {
my @date = @_;
my @next_term;
my @this_term = ThisTerm( @date );
if ( @this_term == 2 ) {
@next_term = _increment_term( @this_term );
} else {
my @test_date = @date;
while ( @next_term != 2 ) {
@test_date = Add_Delta_Days( @test_date, 1 );
@next_term = ThisTerm( @test_date );
}
}
return @next_term;
}
=item StatutoryTermDates($year)
Returns a hash reference keyed on terms for a given year, the value of
each being a hash reference containing start and end dates for that term.
The dates are stored as array references containing numeric
year, month, day values.
Note: these are the statutory term dates, not full term dates.
=cut
sub StatutoryTermDates {
my $year = shift;
die "StatutoryTermDates: no year given" unless $year;
# Calculate end of Hilary
my @palm_sunday =
Date::Calc::Add_Delta_Days( Date::Calc::Easter_Sunday( $year ), -7 );
my @saturday_before_palm_sunday =
Date::Calc::Add_Delta_Days( @palm_sunday, -6 );
my $hilary_delta = Date::Calc::Delta_Days(
$year, HILARY_END_IF_EARLIER,
@saturday_before_palm_sunday
);
my @hilary_end;
if ( $hilary_delta == 1 ) {
@hilary_end = ( $year, HILARY_END_IF_EARLIER );
} else {
@hilary_end = @saturday_before_palm_sunday;
}
# Calculate start of Trinity
my @wednesday_after_easter_sunday =
Date::Calc::Add_Delta_Days( Date::Calc::Easter_Sunday( $year ), 3 );
my $trinity_delta = Date::Calc::Delta_Days(
@wednesday_after_easter_sunday,
$year, TRINITY_START_IF_LATER
);
my @trinity_start;
if ( $trinity_delta == 1 ) {
@trinity_start = ( $year, TRINITY_START_IF_LATER );
} else {
@trinity_start = @wednesday_after_easter_sunday;
}
my $term_dates = {
Michaelmas => {
start => [$year, MICHAELMAS_START],
end => [$year, MICHAELMAS_END]
},
Hilary => {
start => [$year, HILARY_START],
end => [@hilary_end]
},
Trinity => {
start => [@trinity_start],
end => [$year, TRINITY_END]
}
};
return $term_dates;
}
=item Parse($string)
Takes a free-form description of an Oxford calendar date, and attempts
to divine the expected meaning. If the name of a term is not found, the
current term will be assumed. If the description is unparsable, undef
is returned. Otherwise, an array will be returned of the form
C<($year,$term,$week,$day)>.
This function is experimental.
=cut
sub Parse {
my $string = shift;
my $term = "";
my ( $day, $week, $year );
$day = $week = $year = "";
$string = lc($string);
$string =~ s/week//g;
$string =~ s/(\d+)(?:rd|st|nd|th)/$1/;
my %ab = Text::Abbrev::abbrev( DAYS, TERMS );
my $expand;
while ( $string =~ s/((?:\d|-)\d*)/ / ) {
if ( $1 > 50 ) { $year = $1; $year += 1900 if $year < 1900; }
else { $week = $1 }
}
foreach ( sort { length $b <=> length $a } keys %ab ) {
if ( $string =~ s/\b$_\w+//i ) {
#pos($string)-=length($_);
#my $foo=lc($_); $string=~s/\G$foo[a-z]*/ /i;
$expand = $ab{$_};
$term = $expand if ( scalar( grep /$expand/, TERMS ) > 0 );
$day = $expand if ( scalar( grep /$expand/, DAYS ) > 0 );
}
}
unless ($day) {
%ab = Text::Abbrev::abbrev(DAYS);
foreach ( sort { length $b <=> length $a } keys %ab ) {
if ( $string =~ /$_/ig ) {
pos($string) -= length($_);
my $foo = lc($_);
$string =~ s/\G$foo[a-z]*/ /;
$day = $ab{$_};
}
}
}
unless ($term) {
%ab = Text::Abbrev::abbrev(TERMS);
foreach ( sort { length $b <=> length $a } keys %ab ) {
if ( $string =~ /$_/ig ) {
pos($string) -= length($_);
my $foo = lc($_);
$string =~ s/\G$foo[a-z]*/ /;
$term = $ab{$_};
}
}
}
# Assume this term?
unless ($term) {
$term = ToOx( reverse Date::Calc::Today() );
return "Can't work out what term" unless $term =~ /week/;
$term =~ s/.*eek,\s+(\w+).*/$1/;
}
$year = ( Date::Calc::Today() )[0] unless $year;
return undef unless defined $week and defined $day;
return ( $year, $term, $week, $day );
}
=item FromOx($year, $term, $week, $day)
Converts an Oxford date into a Gregorian date, returning a string of the
form C<DD/MM/YYYY> or undef.
The arguments are of the same format as returned by ToOx in array context;
that is, a four-digit year, the name of the term, the week number, and
the name of the day of week (e.g. 'Sunday').
If the requested date is not covered by the database, FromOx will die with
an "out of range" error message. Therefore it is recommended to eval ToOx
with appropriate error handling.
=cut
sub FromOx {
my %lu;
Init unless defined $_initcal;
my ( $year, $term, $week, $day );
( $year, $term, $week, $day ) = @_;
$year =~ s/\s//g;
$term =~ s/\s//g;
die "No data for $term $year" unless exists $db{"$term $year"};
{
my $foo = 0;
%lu = ( map { $_, $foo++ } DAYS );
}
my $delta = 7 * ( $week - 1 ) + $lu{$day};
my @start = _sunday_of_first( $year, $term );
shift @start;
die "The internal database is bad for $term $year"
unless $start[0];
return join "/", reverse( Date::Calc::Add_Delta_Days( @start, $delta ) );
}
1;
=back
=head1 BUGS
Bugs may be browsed and submitted at
L<http://rt.cpan.org/Public/Dist/Display.html?Name=Oxford-Calendar>
A copy of the maintainer's git repository may be found at
L<https://github.com/jmdh/Oxford-Calendar>
=head1 AUTHOR
Simon Cozens is the original author of this module.
Eugene van der Pijll, C<pijll@cpan.org> took over maintenance from
Simon for a time.
Dominic Hargreaves maintains this module (between 2008 and 2015 for
IT Services, University of Oxford).
=cut
__DATA__
--- #YAML:1.0
Calendar:
Hilary 2001:
start: 14/01/2001
Hilary 2002:
start: 13/01/2002
Hilary 2003:
start: 19/01/2003
Hilary 2004:
start: 18/01/2004
Hilary 2005:
start: 16/01/2005
Hilary 2006:
start: 15/01/2006
Hilary 2007:
start: 14/01/2007
Hilary 2008:
start: 13/01/2008
Hilary 2009:
start: 18/01/2009
Hilary 2010:
start: 17/01/2010
Hilary 2011:
start: 16/01/2011
Hilary 2012:
start: 15/01/2012
Hilary 2013:
start: 13/01/2013
Hilary 2014:
start: 19/01/2014
Hilary 2015:
start: 18/01/2015
Hilary 2016:
start: 17/01/2016
Hilary 2017:
start: 15/01/2017
Hilary 2018:
start: 14/01/2018
Hilary 2019:
start: 13/01/2019
provisional: 1
Hilary 2020:
start: 19/01/2020
provisional: 1
Hilary 2021:
start: 17/01/2021
provisional: 1
Hilary 2022:
start: 16/01/2022
provisional: 1
Michaelmas 2001:
start: 07/10/2001
Michaelmas 2002:
start: 13/10/2002
Michaelmas 2003:
start: 12/10/2003
Michaelmas 2004:
start: 10/10/2004
Michaelmas 2005:
start: 09/10/2005
Michaelmas 2006:
start: 08/10/2006
Michaelmas 2007:
start: 07/10/2007
Michaelmas 2008:
start: 12/10/2008
Michaelmas 2009:
start: 11/10/2009
Michaelmas 2010:
start: 10/10/2010
Michaelmas 2011:
start: 09/10/2011
Michaelmas 2012:
start: 07/10/2012
Michaelmas 2013:
start: 13/10/2013
Michaelmas 2014:
start: 12/10/2014
Michaelmas 2015:
start: 11/10/2015
Michaelmas 2016:
start: 09/10/2016
Michaelmas 2017:
start: 08/10/2017
Michaelmas 2018:
start: 07/10/2018
provisional: 1
Michaelmas 2019:
start: 13/10/2019
provisional: 1
Michaelmas 2020:
start: 11/10/2020
provisional: 1
Michaelmas 2021:
start: 10/10/2021
provisional: 1
Trinity 2001:
start: 22/04/2001
Trinity 2002:
start: 21/04/2002
Trinity 2003:
start: 27/04/2003
Trinity 2004:
start: 25/04/2004
Trinity 2005:
start: 24/04/2005
Trinity 2006:
start: 23/04/2006
Trinity 2007:
start: 22/04/2007
Trinity 2008:
start: 20/04/2008
Trinity 2009:
start: 26/04/2009
Trinity 2010:
start: 25/04/2010
Trinity 2011:
start: 01/05/2011
Trinity 2012:
start: 22/04/2012
Trinity 2013:
start: 21/04/2013
Trinity 2014:
start: 27/04/2014
Trinity 2015:
start: 26/04/2015
Trinity 2016:
start: 24/04/2016
Trinity 2017:
start: 23/04/2017
Trinity 2018:
start: 22/04/2018
Trinity 2019:
start: 28/04/2019
provisional: 1
Trinity 2020:
start: 26/04/2020
provisional: 1
Trinity 2021:
start: 25/04/2021
provisional: 1
Trinity 2022:
start: 24/04/2022
provisional: 1
|