File Coverage

blib/lib/DateTime/Schedule/Weekly.pm
Criterion Covered Total %
statement 51 51 100.0
branch 2 2 100.0
condition 3 3 100.0
subroutine 17 17 100.0
pod 11 11 100.0
total 84 84 100.0


line stmt bran cond sub pod time code
1             package DateTime::Schedule::Weekly 0.03;
2 5     5   493540 use v5.26;
  5         20  
3              
4             # ABSTRACT: Augment DateTime::Schedule with a weekly recurrrence pattern
5              
6 5     5   1276 use Object::Pad;
  5         25092  
  5         40  
7              
8             class DateTime::Schedule::Weekly {
9 2     2   1908 inherit DateTime::Schedule;
  2         10  
  2         237  
10              
11 5     5   4945 use Readonly;
  5         39756  
  5         781  
12             Readonly::Array my @DAY_NUMS => (undef, qw(monday tuesday wednesday thursday friday saturday sunday));
13              
14 5     5   44 use constant true => !!1;
  5         10  
  5         461  
15 5     5   29 use constant false => !true;
  5         12  
  5         8873  
16              
17 145     145 1 438 field $sunday : param : reader = true;
18 145     125 1 1086 field $monday : param : reader = true;
  125         2682  
19 125     116 1 908 field $tuesday : param : reader = true;
  116         2643  
20 116     115 1 835 field $wednesday : param : reader = true;
  115         2469  
21 115     115 1 907 field $thursday : param : reader = true;
  115         2567  
22 115     115 1 891 field $friday : param : reader = true;
  115         2456  
23 115     130 1 830 field $saturday : param : reader = true;
  130         2546  
24              
25 130     795 1 993 field $include : param : reader = [];
  795         1951  
26              
27 795         4981 ADJUST {
28             die("At least one day must be scheduled")
29             unless ($self->sunday
30             || $self->monday
31             || $self->tuesday
32             || $self->wednesday
33             || $self->thursday
34             || $self->friday
35             || $self->saturday);
36              
37             $include = {map {$_->strftime('%F') => true} ($include // [])->@*};
38             }
39              
40 11     11 1 445751 sub weekdays($class, @params) {
  11         79  
  11         33  
  11         17  
41 11         248 __PACKAGE__->new(sunday => false, saturday => false, @params);
42             }
43              
44 6     6 1 71164 sub weekends($class, @params) {
  6         21  
  6         19  
  6         17  
45 6         187 __PACKAGE__->new(monday => false, tuesday => false, wednesday => false, thursday => false, friday => false, @params);
46             }
47              
48 801     801 1 2081 method is_day_scheduled($d) {
  801         3430  
  801         1447  
  801         1282  
49 801         2917 my $day_name = $DAY_NUMS[$d->day_of_week];
50 801         14710 my $str = $d->strftime('%F');
51 801 100       74360 return false if ($self->exclude->{$str});
52 795   100     2922 return $self->include->{$str} || $self->$day_name;
53             }
54             }
55              
56             =head1 NAME
57              
58             DateTime::Schedule::Weekly - Determine scheduled days in range based on weekly recurrence and inclusions/exclusions
59              
60             =head1 SYNOPSIS
61              
62             use DateTime::Schedule::Weekly;
63              
64             my $dts = DateTime::Schedule::Weekly->weekdays(exclude => [...list of school holidays...], portion => 0.5);
65              
66             my $school_days_elapsed = $dts->days_in_range($first_day_of_school, $now)->count;
67              
68             =head1 DESCRIPTION
69              
70             This subclass of L<DateTime::Schedule> augments its capabilities to support a
71             regular weekly schedule of on/off days.
72              
73             =head1 CONSTRUCTORS
74              
75             =head2 new
76              
77             Returns a new instance. Permits the following construction parameters, in
78             addition to those supported by L<DateTime::Schedule>:
79              
80             =head4 sunday, monday, tuesday, wednesday, thursday, friday, saturday
81              
82             I<Optional>. Default C<true>
83              
84             These seven constructor parameters indicate which days of the week are "scheduled"
85             By default, all are "on", making the behavior identical to L<DateTime::Schedule>'s.
86              
87             =head4 include
88              
89             I<Optional>. Default C<[]>
90              
91             An arrayref of L<DateTime>s. These days are included over and above the normal
92             schedule.
93              
94             E.g., a school schedule could be regularly M-F, but due to extensive weather-related
95             closures, the school added in one or more saturdays class days. These would be
96             C<include>d.
97              
98             B<N.B. Exclusions have priority over inclusions, so a date in both lists will be excluded!>
99              
100             =head2 weekdays
101              
102             Identical to calling L</new> except that by default C<saturday> and C<sunday> are
103             "off".
104              
105             =head2 weekends
106              
107             Identical to calling L</new> except that by default C<monday>, C<tuesday>,
108             C<wednesday>, C<thursday>, and C<friday> are "off".
109              
110             =head1 METHODS
111              
112             Implements all methods from L<DateTime::Schedule>
113              
114             =head2 is_day_scheduled($datetime)
115              
116             Given a L<DateTime>, returns false if date is in the exclusion list. Otherwise
117             returns true if date is in the inclusion list or if date is in a scheduled day
118             of week. Returns false otherwise.
119              
120             =head1 AUTHOR
121              
122             Mark Tyrrell C<< <mark@tyrrminal.dev> >>
123              
124             =head1 LICENSE
125              
126             Copyright (c) 2024 Mark Tyrrell
127              
128             Permission is hereby granted, free of charge, to any person obtaining a copy
129             of this software and associated documentation files (the "Software"), to deal
130             in the Software without restriction, including without limitation the rights
131             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
132             copies of the Software, and to permit persons to whom the Software is
133             furnished to do so, subject to the following conditions:
134              
135             The above copyright notice and this permission notice shall be included in all
136             copies or substantial portions of the Software.
137              
138             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
139             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
140             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
141             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
142             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
143             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
144             SOFTWARE.
145              
146             =cut
147              
148             1;
149              
150             __END__
151