blib/lib/Oxford/Calendar.pm

filter: Statementstmt 90.2% 231 / 256 Branchbran 73.1% 79 / 108 Conditioncond 78.2% 18 / 23 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 32 / 32 Podpod 85.7% 6 / 7 totaltotal 85.0% 374 / 440 CC 68 SCAR 26.7
CC
68
Cov
85%
Parse
48.6
ToOx
14.0
_to_ox_nearest
12.2
CRAP 14.4
↑ Summary
1 # Oxford University calendar conversion.
2 # Simon Cozens (c) 1999-2002
3 # Eugene van der Pijll (c) 2004
4 # University of Oxford (c) 2007-2015
5 # Dominic Hargreaves (c) 2016
6 # Artistic License
7 package Oxford::Calendar;
8 3 $Oxford::Calendar::VERSION = "2.13";
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Text::Abbrev;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶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);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Time::Seconds;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use constant CALENDAR => '/etc/oxford-calendar.yaml';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use constant SEVEN_WEEKS => 7 * ONE_WEEK;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use constant DEFAULT_MODE => 'nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use constant TERMS => qw(Michaelmas Hilary Trinity);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use constant DAYS => qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Constants defined by University regulations
23 3 ▶use constant MICHAELMAS_START => (10, 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use constant MICHAELMAS_END => (12, 17);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use constant HILARY_START => (1, 7);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use constant HILARY_END_IF_EARLIER => (3, 25);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use constant TRINITY_START_IF_LATER => (4, 20);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use constant TRINITY_END => (7, 6);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =head1 NAME
31  
32 Oxford::Calendar - University of Oxford calendar conversion routines
33  
34 =head1 SYNOPSIS
35  
36 use 5.10.0;
37 use Oxford::Calendar;
38 use Date::Calc;
39 say "Today is " . Oxford::Calendar::ToOx(reverse Date::Calc::Today);
40  
41 =head1 DESCRIPTION
42  
43 This module converts University of Oxford dates (Oxford academic dates)
44 to and from Real World dates, and provides information on Terms of the
45 University.
46  
47 The Terms of the University are defined by the
48 B<Regulations on the number and lengths of terms>, available online from
49  
50 L<https://www.admin.ox.ac.uk/examregs/2015-16/rotnalengofterm/>
51  
52 This document describes the start and end dates of Oxford Terms.
53  
54 In addition to this, the dates of Full Term, required to calculate the
55 week number of the term, are prescribed by Council, and published
56 periodically in the B<University Gazette>.
57  
58 Full term comprises weeks 1-8 inclusive, but sometimes, dates outside of
59 full term are presented in the Oxford academic date format.
60 This module will optionally provide such dates.
61  
62 Data for these prescribed dates may be supplied in the file
63 F</etc/oxford-calendar.yaml>; if this file does not exist, built-in data
64 will be used. The built-in data is periodically updated from the
65 semi-authoritative source at
66  
67 L<http://www.ox.ac.uk/about/facts-and-figures/dates-of-term>.
68  
69 or the authoritative source, the Gazette, available online from
70  
71 L<http://www.ox.ac.uk/gazette/>.
72  
73 L<http://www.ox.ac.uk/news-and-events/The-University-Year>
74 describes the academic year at Oxford.
75  
76 =head1 DATE FORMAT
77  
78 An Oxford academic date has the following format:
79  
80 =over
81  
82 <day of week>, <week number>[st,nd,rd,th] week, <term name> <year>
83  
84 =back
85  
86 where term name is one of
87  
88 =over
89  
90 =item *
91  
92 Michaelmas (autumn)
93  
94 =item *
95  
96 Hilary (spring)
97  
98 =item *
99  
100 Trinity (summer)
101  
102 =back
103  
104 Example:
105  
106 Friday, 8th Week, Michaelmas 2007
107  
108 =cut
109  
110 3 our %db;
111  
112 3 my $_initcal; # If this is true, we have our database of dates already.
113 my $_initrange;
114 3 my @_oxford_full_terms;
115  
116 14 ▶sub _get_week_suffix {
Subroutine _get_week_suffix: called CC=5 SCAR=16.2
117 14 my $week = shift;
118 14 ▶ die "_get_week_suffix: No week given" unless defined $week;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $week 0 14 14
119 14 my $wsuffix = "th";
120 14 ▶ abs($week) == 1 && ( $wsuffix = "st" );
Branch: 1 branch
BranchTrueFalseTotal
abs $week == 1 and $wsuffix = "st" 0 14 14
121 14 ▶ abs($week) == 2 && ( $wsuffix = "nd" );
Branch: 1 branch
BranchTrueFalseTotal
abs $week == 2 and $wsuffix = "nd" 2 12 14
122 14 ▶ abs($week) == 3 && ( $wsuffix = "rd" );
Branch: 1 branch
BranchTrueFalseTotal
abs $week == 3 and $wsuffix = "rd" 1 13 14
123
124 14 return $wsuffix;
125 }
126  
127 4 ▶sub _find_week {
Subroutine _find_week: called CC=2 SCAR=6.9
128 4 my $tm = shift;
129 4 my $sweek = shift;
130 4 my $sweek_tm = shift;
131  
132 4 my $eow = $sweek_tm + ONE_WEEK;
133  
134 4 ▶ while ( $tm >= $eow ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tm >= $eow) 12 4 16
135 12 $eow += ONE_WEEK;
136 12 $sweek++;
137 }
138 4 return $sweek;
139 }
140  
141 3 ▶sub _init_db {
Subroutine _init_db: called CC=2 SCAR=7.1
142 3 my $db;
143 3 ▶ if ( -r CALENDAR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r '/etc/oxford-calendar.yaml') { } 0 3 3
144 0 $db = YAML::LoadFile(CALENDAR);
145 }
146 else {
147 3 my $data = join '', <DATA>;
148 3 $db = YAML::Load($data);
149 }
150 3 %db = %{ $db->{Calendar} };
151 }
152  
153 2 ▶sub _init_range {
Subroutine _init_range: called CC=4 SCAR=14.2
154 2 foreach my $termspec ( keys %db ) {
155 184 ▶ next unless $db{$termspec};
Branch: 1 branch
BranchTrueFalseTotal
unless $Oxford::Calendar::db{$termspec} 0 184 184
156  
157 184 ▶ my $time = eval { Time::Piece->strptime($db{$termspec}->{start}, '%d/%m/%Y' ) }
Branch: 1 branch
BranchTrueFalseTotal
my $time = eval { do { "Time::Piece"->strptime($Oxford::Calendar::db{$termspec}{"start"}, "%d/%m/%Y") } } or die qq[Could not decode date ($Oxford::Calendar::db{$termspec}{"start"}) for term ${termspec}: $@] 184 0 184
158 or die
159 "Could not decode date ($db{$termspec}->{start}) for term $termspec: $@";
160  
161 push @_oxford_full_terms,
162 184 [$time, ($time + SEVEN_WEEKS), split(/ /, $termspec), $db{$termspec}->{provisional}];
163 }
164  
165 2 $_initrange++;
166 }
167  
168 14 ▶sub _fmt_oxdate_as_string {
Subroutine _fmt_oxdate_as_string: called CC=1 SCAR=0.0
169 14 my ( $dow, $week, $term, $year ) = @_;
170 14 my $wsuffix = _get_week_suffix($week);
171 14 return "$dow, $week$wsuffix week, $term $year";
172 }
173  
174 1 ▶sub _increment_term {
Subroutine _increment_term: called CC=4 SCAR=19.7
175 1 my ( $year, $term ) = @_;
176 1 ▶ if ( $term eq 'Michaelmas' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($term eq 'Michaelmas') { } 0 1 1
elsif ($term eq 'Hilary') { } 1 0 1
elsif ($term eq 'Trinity') { } 0 0 0
177 0 return $year + 1, 'Hilary';
178 } elsif ( $term eq 'Hilary' ) {
179 1 return $year, 'Trinity'
180 } elsif ( $term eq 'Trinity' ) {
181 0 return $year, 'Michaelmas';
182 } else {
183 0 die "_increment_term: Unknown term $term";
184 }
185 }
186  
187 17 ▶sub _sunday_of_first {
Subroutine _sunday_of_first: called CC=3 SCAR=11.0
188 17 my ( $year, $term ) = @_;
189 17 ▶ Init() unless defined $_initcal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_initcal 0 17 17
190 17 my $date = $db{"$term $year"};
191 17 ▶ return undef unless $date;
Branch: 1 branch
BranchTrueFalseTotal
unless $date 2 15 17
192 15 return ( $date->{provisional}, Decode_Date_EU($date->{start}) );
193 }
194  
195 7 ▶sub _to_ox_nearest {
Subroutine _to_ox_nearest: called CC=12 SCAR=25.0
196 7 my @date = @_;
197 7 my $confirmed = pop @date;
198 7 my $week;
199 my @term;
200 7 ▶ _init_range() unless defined $_initrange;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_initrange 2 5 7
201 7 my $dow = Day_of_Week_to_Text( Day_of_Week( @date ) );
202 7 my $tm = Time::Piece->strptime(join('/', @date[0..2]), '%Y/%m/%d');
203 7 my @terms = sort { $a->[0] <=> $b->[0] } @_oxford_full_terms;
204 7 my ( $prevterm, $nextterm );
205 7 my $curterm = shift @terms;
206  
207 7 ▶ while ($curterm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curterm) 319 0 319
208 319 ▶ if ( $tm < $curterm->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tm < $curterm->[0]) 7 312 319
209 7 ▶ if ( $prevterm && $tm >= ($prevterm->[1] + ONE_WEEK) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prevterm and $tm >= $prevterm->[1] + 604800) { } 7 0 7
Condition33%1/3
$prevterm and $tm >= $prevterm->[1] + 604800
!ll&&!rl&&r
007
MC/DC0%0/2
$prevterm and $tm >= $prevterm->[1] + 604800
$prevterm $tm >= $prevterm->[1] + 604800
Truth table33%1/3
A and B
A= $prevtermB= $tm >= $prevterm->[1] + 604800
ABresult
0X0
100
111
210 7 $nextterm = $curterm;
211 7 last;
212 } else {
213 0 die "Date out of range";
214 }
215 }
216 312 $prevterm = $curterm;
217 312 $curterm = shift @terms;
218 }
219 7 ▶ return undef unless $nextterm;
Branch: 1 branch
BranchTrueFalseTotal
unless $nextterm 0 7 7
220  
221 # We are in the gap between terms .. which one is closest?
222 7 my $prevgap = $tm - ($prevterm->[1] + ONE_WEEK);
223 7 my $nextgap = $tm - $nextterm->[0];
224  
225 7 ▶ if ( abs($prevgap) < abs($nextgap) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $prevgap < abs $nextgap) { } 4 3 7
226 # if equal go for -<n>th week
227 4 $week = _find_week( $tm, 8, $prevterm->[1] );
228 4 @term = @{$prevterm};
229 } else {
230 3 my $delta = $nextgap / (24 * 60 * 60);
231 3 $week = 1 + int( $delta / 7 );
232 3 ▶ $week -= 1 if $delta % 7;
Branch: 1 branch
BranchTrueFalseTotal
if $delta % 7 1 2 3
233 3 @term = @{$nextterm};
234 }
235 7 ▶ return undef if $term[4] && $confirmed;
Branch: 1 branch
BranchTrueFalseTotal
if $term[4] and $confirmed 1 6 7
Condition100%3/3
$term[4] and $confirmed
!ll&&!rl&&r
511
MC/DC100%2/2
$term[4] and $confirmed
$term[4] $confirmed
Truth table100%3/3
A and B
A= $term[4]B= $confirmed
ABresult
0X0
100
111
236 6 ▶ return ($dow, $week, $term[2], $term[3]) if ( wantarray );
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 5 6
237 5 return _fmt_oxdate_as_string( $dow, $week, $term[2], $term[3] );
238 }
239  
240  
241 3 ▶sub Init {
Subroutine Init: called CC=1 SCAR=0.0
Pod undocumented
242 3 _init_db;
243 3 Date::Calc::Language(Date::Calc::Decode_Language('English'));
244 3 $_initcal++;
245 }
246  
247 =head1 FUNCTIONS
248  
249 =over 3
250  
251 =item ToOx($day, $month, $year, [\%options])
252  
253 Given a day, month and year in standard human format (that is, month is
254 1-12, not 0-11, and year is four digits) will return a string of the
255 form
256  
257 Day, xth week, Term year
258  
259 or an array
260  
261 (Day, week of term, Term, year)
262
263 depending on how it is called. The exact behaviour is modified by the 'mode'
264 option described below.
265  
266 If the requested date is not in full term or extended term (see below),
267 undef will be returned.
268  
269 If the requested date is not covered by the database, ToOx will die with
270 an "out of range" error message. Therefore it is recommended to eval ToOx
271 with appropriate error handling.
272  
273 %options can contain additional named parameter options:
274  
275 =over 5
276  
277 =item mode
278  
279 Several modes are available:
280  
281 =over 6
282  
283 =item full_term
284  
285 Term dates will only be returned if the date requested is part of a full
286 term (as defined by the web page above).
287  
288 =item ext_term
289  
290 Term dates will only be returned if the date requested is part of an extended
291 term, or statutory term.
292  
293 =item nearest
294  
295 Will return term dates based on the nearest term, even if the date requested
296 is not part of an extended term (i.e. will include fictional week numbers).
297  
298 This is currently the default behaviour, for backwards compatibility with
299 previous releases; this may be changed in future.
300  
301 =back
302  
303 =back
304  
305 =over 4
306  
307 =item confirmed
308  
309 If true, ignores dates marked as provisional in the database.
310  
311 =back
312  
313 =cut
314  
315 24 ▶sub ToOx {
Subroutine ToOx: called CC=14 SCAR=26.4
Pod documented
316 24 my (@dmy, $options);
317 24 ($dmy[0], $dmy[1], $dmy[2], $options) = @_;
318 24 ▶ my $mode = $options->{mode} || DEFAULT_MODE;
Condition100%2/2
$options->{'mode'} || 'nearest'
l!l
231
MC/DC100%1/1
$options->{'mode'} || 'nearest'
$options->{'mode'}
Truth table100%2/2
A
A= $options->{'mode'}
Aresult
11
00
319 24 my ($week, @term);
320 24 my @date = reverse @dmy;
321 24 ▶ Init unless defined $_initcal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_initcal 3 21 24
322 24 my $dow = Day_of_Week_to_Text( Day_of_Week( @date ) );
323  
324 24 @term = ThisTerm( @date );
325 24 ▶ if ( $#term ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#term) { } 14 10 24
326 # We're in term
327 14 my @term_start = _sunday_of_first( @term );
328 14 my $provisional = shift @term_start;
329 14 ▶ die "Date out of range" unless ( $#term_start == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless $#term_start == 2 2 12 14
330 12 my $days_from_start = Delta_Days( @term_start, @date );
331 12 ▶ my $week_offset = $days_from_start < 0 ? 1 : 7;
Branch: 1 branch
BranchTrueFalseTotal
$days_from_start < 0 ? : 2 10 12
332 12 my $week = int( ( $days_from_start + $week_offset ) / 7);
333 12 ▶ return undef if $options->{confirmed} && $provisional;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"confirmed"} and $provisional 1 11 12
Condition66%2/3
$options->{"confirmed"} and $provisional
!ll&&!rl&&r
1101
MC/DC50%1/2
$options->{"confirmed"} and $provisional
$options->{"confirmed"} $provisional
Truth table66%2/3
A and B
A= $options->{"confirmed"}B= $provisional
ABresult
0X0
100
111
334 11 ▶ return undef if ( ( $week < 1 || $week > 8 ) && $mode eq 'full_term' );
Branch: 1 branch
BranchTrueFalseTotal
if $week < 1 || $week > 8 and $mode eq "full_term" 1 10 11
Condition100%6/6
$week < 1 || $week > 8 and $mode eq "full_term"
!ll&&!rl&&r
551
$week < 1 or $week > 8
l!l&&r!l&&!r
245
MC/DC66%2/3
$week < 1 || $week > 8 and $mode eq "full_term"
$week < 1 $week > 8 $mode eq "full_term"
Truth table80%4/5
A or B and C
A= $week < 1B= $week > 8C= $mode eq "full_term"
ABCresult
00X0
1X00
0100
1X11
0111
335 10 ▶ return ( $dow, $week, $term[1], $term[0] ) if ( wantarray );
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 9 10
336 9 return _fmt_oxdate_as_string( $dow, $week, $term[1], $term[0] );
337 } else {
338 10 ▶ return undef if $mode eq 'full_term';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "full_term" 1 9 10
339 9 ▶ return undef if $mode eq 'ext_term';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "ext_term" 2 7 9
340 7 return _to_ox_nearest( @date, $options->{confirmed} );
341 }
342 }
343  
344 =item ThisTerm($year, $month, $day)
345  
346 Given a year, month, term in standard human format (that is, month is
347 1-12, not 0-11, and year is four digits) will returns the current term
348 or undef if in vacation or unknown. The term is given as an array in the
349 form (year, term).
350  
351 =cut
352  
353 32 ▶sub ThisTerm {
Subroutine ThisTerm: called CC=4 SCAR=13.9
Pod documented
354 32 my ( $year, $month, $day ) = @_;
355 32 my $term_dates = StatutoryTermDates( $year );
356 32 foreach my $term ( keys %{$term_dates} ) {
357 77 my $start = Date_to_Days( @{$term_dates->{$term}->{start}} );
358 77 my $end = Date_to_Days( @{$term_dates->{$term}->{end}} );
359 77 my $date = Date_to_Days( $year, $month, $day );
360 77 ▶ if ( ( $date >= $start ) && ( $date <= $end )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date >= $start and $date <= $end) 16 61 77
Condition100%3/3
$date >= $start and $date <= $end
!ll&&!rl&&r
421916
MC/DC100%2/2
$date >= $start and $date <= $end
$date >= $start $date <= $end
Truth table100%3/3
A and B
A= $date >= $startB= $date <= $end
ABresult
0X0
100
111
361 16 return ( $year, $term );
362 }
363 }
364 16 return undef;
365 }
366  
367 =item NextTerm($year, $month, $day)
368  
369 Given a day, month and year in standard human format (that is, month is
370 1-12, not 0-11, and year is four digits) will return the next term (whether
371 or not the date given is in term time).
372 The term is given as an array in the form (year, term).
373  
374 =cut
375  
376 2 ▶sub NextTerm {
Subroutine NextTerm: called CC=3 SCAR=11.0
Pod documented
377 2 my @date = @_;
378 2 my @next_term;
379 2 my @this_term = ThisTerm( @date );
380 2 ▶ if ( @this_term == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@this_term == 2) { } 1 1 2
381 1 @next_term = _increment_term( @this_term );
382 } else {
383 1 my @test_date = @date;
384 1 ▶ while ( @next_term != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@next_term != 2) 6 1 7
385 6 @test_date = Add_Delta_Days( @test_date, 1 );
386 6 @next_term = ThisTerm( @test_date );
387 }
388 }
389 2 return @next_term;
390 }
391  
392 =item StatutoryTermDates($year)
393  
394 Returns a hash reference keyed on terms for a given year, the value of
395 each being a hash reference containing start and end dates for that term.
396 The dates are stored as array references containing numeric
397 year, month, day values.
398  
399 Note: these are the statutory term dates, not full term dates.
400  
401 =cut
402  
403 32 ▶sub StatutoryTermDates {
Subroutine StatutoryTermDates: called CC=4 SCAR=14.0
Pod documented
404 32 my $year = shift;
405 32 ▶ die "StatutoryTermDates: no year given" unless $year;
Branch: 1 branch
BranchTrueFalseTotal
unless $year 0 32 32
406
407 # Calculate end of Hilary
408 32 my @palm_sunday =
409 Date::Calc::Add_Delta_Days( Date::Calc::Easter_Sunday( $year ), -7 );
410 32 my @saturday_before_palm_sunday =
411 Date::Calc::Add_Delta_Days( @palm_sunday, -6 );
412  
413 32 my $hilary_delta = Date::Calc::Delta_Days(
414 $year, HILARY_END_IF_EARLIER,
415 @saturday_before_palm_sunday
416 );
417  
418 32 my @hilary_end;
419 32 ▶ if ( $hilary_delta == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hilary_delta == 1) { } 5 27 32
420 5 @hilary_end = ( $year, HILARY_END_IF_EARLIER );
421 } else {
422 27 @hilary_end = @saturday_before_palm_sunday;
423 }
424
425 # Calculate start of Trinity
426 32 my @wednesday_after_easter_sunday =
427 Date::Calc::Add_Delta_Days( Date::Calc::Easter_Sunday( $year ), 3 );
428  
429 32 my $trinity_delta = Date::Calc::Delta_Days(
430 @wednesday_after_easter_sunday,
431 $year, TRINITY_START_IF_LATER
432 );
433  
434 32 my @trinity_start;
435 32 ▶ if ( $trinity_delta == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trinity_delta == 1) { } 0 32 32
436 0 @trinity_start = ( $year, TRINITY_START_IF_LATER );
437 } else {
438 32 @trinity_start = @wednesday_after_easter_sunday;
439 }
440  
441 32 my $term_dates = {
442 Michaelmas => {
443 start => [$year, MICHAELMAS_START],
444 end => [$year, MICHAELMAS_END]
445 },
446 Hilary => {
447 start => [$year, HILARY_START],
448 end => [@hilary_end]
449 },
450 Trinity => {
451 start => [@trinity_start],
452 end => [$year, TRINITY_END]
453 }
454 };
455 32 return $term_dates;
456 }
457  
458 =item Parse($string)
459  
460 Takes a free-form description of an Oxford calendar date, and attempts
461 to divine the expected meaning. If the name of a term is not found, the
462 current term will be assumed. If the description is unparsable, undef
463 is returned. Otherwise, an array will be returned of the form
464 C<($year,$term,$week,$day)>.
465  
466 This function is experimental.
467  
468 =cut
469  
470 3 ▶sub Parse {
Subroutine Parse: called CC=19 SCAR=38.8
Pod documented
471 3 my $string = shift;
472 3 my $term = "";
473 3 my ( $day, $week, $year );
474 3 $day = $week = $year = "";
475  
476 3 $string = lc($string);
477 3 $string =~ s/week//g;
478 3 $string =~ s/(\d+)(?:rd|st|nd|th)/$1/;
479 3 my %ab = Text::Abbrev::abbrev( DAYS, TERMS );
480 3 my $expand;
481 3 ▶ while ( $string =~ s/((?:\d|-)\d*)/ / ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/((?:\d|-)\d*)/ /) 6 3 9
482 6 ▶ if ( $1 > 50 ) { $year = $1; $year += 1900 if $year < 1900; }
Branch: 2 branches
BranchTrueFalseTotal
if ($1 > 50) { } 3 3 6
if $year < 1900 0 3 3
483 3 else { $week = $1 }
484 }
485 3 foreach ( sort { length $b <=> length $a } keys %ab ) {
486 198 ▶ if ( $string =~ s/\b$_\w+//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/\b$_\w+//i) 6 192 198
487  
488 #pos($string)-=length($_);
489 #my $foo=lc($_); $string=~s/\G$foo[a-z]*/ /i;
490 6 $expand = $ab{$_};
491 6 ▶ $term = $expand if ( scalar( grep /$expand/, TERMS ) > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep(/$expand/, ("Michaelmas", "Hilary", "Trinity")) > 0 3 3 6
492 6 ▶ $day = $expand if ( scalar( grep /$expand/, DAYS ) > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep(/$expand/, ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")) > 0 3 3 6
493 }
494 }
495 3 ▶ unless ($day) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($day) 0 3 3
496 0 %ab = Text::Abbrev::abbrev(DAYS);
497 0 foreach ( sort { length $b <=> length $a } keys %ab ) {
498 0 ▶ if ( $string =~ /$_/ig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$_/gi) 0 0 0
499 0 pos($string) -= length($_);
500 0 my $foo = lc($_);
501 0 $string =~ s/\G$foo[a-z]*/ /;
502 0 $day = $ab{$_};
503 }
504 }
505 }
506 3 ▶ unless ($term) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term) 0 3 3
507 0 %ab = Text::Abbrev::abbrev(TERMS);
508 0 foreach ( sort { length $b <=> length $a } keys %ab ) {
509 0 ▶ if ( $string =~ /$_/ig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$_/gi) 0 0 0
510 0 pos($string) -= length($_);
511 0 my $foo = lc($_);
512 0 $string =~ s/\G$foo[a-z]*/ /;
513 0 $term = $ab{$_};
514 }
515 }
516 }
517  
518 # Assume this term?
519 3 ▶ unless ($term) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term) 0 3 3
520 0 $term = ToOx( reverse Date::Calc::Today() );
521 0 ▶ return "Can't work out what term" unless $term =~ /week/;
Branch: 1 branch
BranchTrueFalseTotal
unless $term =~ /week/ 0 0 0
522 0 $term =~ s/.*eek,\s+(\w+).*/$1/;
523 }
524 3 ▶ $year = ( Date::Calc::Today() )[0] unless $year;
Branch: 1 branch
BranchTrueFalseTotal
unless $year 0 3 3
525 3 ▶ return undef unless defined $week and defined $day;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $week and defined $day 0 3 3
Condition33%1/3
defined $week and defined $day
!ll&&!rl&&r
003
MC/DC0%0/2
defined $week and defined $day
defined $week defined $day
Truth table33%1/3
A and B
A= defined $weekB= defined $day
ABresult
0X0
100
111
526 3 return ( $year, $term, $week, $day );
527 }
528  
529 =item FromOx($year, $term, $week, $day)
530  
531 Converts an Oxford date into a Gregorian date, returning a string of the
532 form C<DD/MM/YYYY> or undef.
533  
534 The arguments are of the same format as returned by ToOx in array context;
535 that is, a four-digit year, the name of the term, the week number, and
536 the name of the day of week (e.g. 'Sunday').
537  
538 If the requested date is not covered by the database, FromOx will die with
539 an "out of range" error message. Therefore it is recommended to eval ToOx
540 with appropriate error handling.
541  
542 =cut
543  
544 4 ▶sub FromOx {
Subroutine FromOx: called CC=4 SCAR=13.9
Pod documented
545 4 my %lu;
546 4 ▶ Init unless defined $_initcal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_initcal 0 4 4
547 4 my ( $year, $term, $week, $day );
548 4 ( $year, $term, $week, $day ) = @_;
549 4 $year =~ s/\s//g;
550 4 $term =~ s/\s//g;
551 4 ▶ die "No data for $term $year" unless exists $db{"$term $year"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Oxford::Calendar::db{"$term $year"} 1 3 4
552 {
553 3 my $foo = 0;
554 3 %lu = ( map { $_, $foo++ } DAYS );
555 }
556 3 my $delta = 7 * ( $week - 1 ) + $lu{$day};
557 3 my @start = _sunday_of_first( $year, $term );
558 3 shift @start;
559 3 ▶ die "The internal database is bad for $term $year"
Branch: 1 branch
BranchTrueFalseTotal
unless $start[0] 0 3 3
560 unless $start[0];
561 3 return join "/", reverse( Date::Calc::Add_Delta_Days( @start, $delta ) );
562 }
563  
564 3 1;
565  
566 =back
567  
568 =head1 BUGS
569  
570 Bugs may be browsed and submitted at
571  
572 L<http://rt.cpan.org/Public/Dist/Display.html?Name=Oxford-Calendar>
573  
574 A copy of the maintainer's git repository may be found at
575  
576 L<https://github.com/jmdh/Oxford-Calendar>
577  
578 =head1 AUTHOR
579  
580 Simon Cozens is the original author of this module.
581  
582 Eugene van der Pijll, C<pijll@cpan.org> took over maintenance from
583 Simon for a time.
584  
585 Dominic Hargreaves maintains this module (between 2008 and 2015 for
586 IT Services, University of Oxford).
587  
588 =cut
589  
590 __DATA__
↑ Summary