File Coverage

blib/lib/DateTime/Calendar/WarwickUniversity.pm
Criterion Covered Total %
statement 30 35 85.7
branch 7 14 50.0
condition 3 6 50.0
subroutine 9 9 100.0
pod 3 3 100.0
total 52 67 77.6


line stmt bran cond sub pod time code
1             package DateTime::Calendar::WarwickUniversity;
2              
3             =head1 NAME
4              
5             DateTime::Calendar::WarwickUniversity - Warwick University academic calendar
6              
7             =head1 SYNOPSIS
8              
9             use DateTime::Calendar::WarwickUniversity;
10              
11             my $dt = DateTime::Calendar::WarwickUniversity->new(
12             year => 2007,
13             month => 01,
14             day => 8,
15             );
16              
17             # 15
18             print $dt->academic_week;
19              
20             # 11
21             print $dt->term_week;
22              
23             # 2, 1
24             print join(', ', $dt->term_and_week);
25              
26             =head1 DESCRIPTION
27              
28             DateTime::Calendar::WarwickUniversity is used for working with the
29             academic calendar in use at the University of Warwick.
30              
31             =cut
32              
33 5     5   1287756 use 5.008004;
  5         20  
  5         215  
34 5     5   30 use strict;
  5         11  
  5         186  
35 5     5   35 use warnings;
  5         13  
  5         175  
36              
37 5     5   29 use Carp;
  5         10  
  5         717  
38 5     5   12237 use DateTime::Event::WarwickUniversity;
  5         4974  
  5         166  
39 5     5   73 use base 'DateTime';
  5         10  
  5         12493  
40              
41             our $VERSION = '0.02';
42              
43             =head2 academic_week
44              
45             Takes no argument.
46              
47             Returns the academic week for the current object, in the range 1..53.
48              
49             =cut
50              
51             sub academic_week {
52 12     12 1 10070 my $self = shift;
53              
54 12         81 my $start = DateTime::Event::WarwickUniversity
55             ->new_year_for_academic_year($self);
56              
57             # TODO: Check whether the 53 and 1 are always valid.
58 12 100       19520 return $self->week_number - $start->week_number
59             + ($self->week_year > $start->week_year ? 53 : 1);
60             }
61              
62             =head2 term_and_week
63              
64             Takes no argument.
65              
66             Returns a list ($term, $week) for the current object.
67             $term is either in the range 1..3, or one of 'C', 'E' or 'S', representing
68             the Christmas, Easter and Summer holidays.
69             $week is in the range 1..10.
70              
71             =cut
72              
73             sub term_and_week {
74 8     8 1 5019 my $self = shift;
75              
76 8         25 my $academic_week = $self->academic_week;
77              
78             # TODO: Check these assumptions.
79 8 100       585 if ($academic_week <= 10) {
    50          
    50          
    0          
    0          
80 4         16 return (1, $academic_week);
81             } elsif ($academic_week <= 14) {
82 0         0 return ('C', $academic_week - 10);
83             } elsif ($academic_week <= 24) {
84 4         18 return (2, $academic_week - 14);
85             } elsif ($academic_week <= 29) {
86 0         0 return ('E', $academic_week - 24);
87             } elsif ($academic_week <= 39) {
88 0         0 return (3, $academic_week - 29);
89             } else {
90 0         0 return ('S', $academic_week - 39);
91             }
92             }
93              
94             =head2 term_week
95              
96             Takes no argument.
97              
98             Returns the term week for the current object, in the range 1..30, or undef if
99             the date does not fall within a term week.
100              
101             =cut
102              
103             sub term_week {
104 4     4 1 6370 my $self = shift;
105              
106 4         15 my ($term, $week) = $self->term_and_week;
107              
108 4 50 66     29 if ($term == 1 or $term == 2 or $term == 3) {
      33        
109 4         90 return ($term - 1) * 10 + $week;
110             } else {
111 0           return undef;
112             }
113             }
114              
115             1;
116             __END__