Schedule.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary Schedule/Weekly.pm »
1 package DateTime::Schedule 0.03;
2 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # ABSTRACT: Determine scheduled days in range based on inclusions/exclusions
5  
6 0 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 class DateTime::Schedule {
9 use DateTime::Set;
10  
11 field $portion : param : reader = 1;
12  
13  
14 field $exclude : param : reader = [];
15  
16 ADJUST {
17 $portion = 0 if ($portion < 0);
18 $portion = 1 if ($portion > 1);
19  
20 $exclude = {map {$_->strftime('%F') => 1} ($exclude // [])->@*};
21 }
22  
23 method day_frac($datetime) {
24 my $this_day = $datetime->clone->truncate(to => 'day');
25 my $next_day = $this_day->clone->add(days => 1);
26 my $total = $next_day->epoch - $this_day->epoch; #total number of seconds in "this" day
27 my $diff = $datetime->epoch - $this_day->epoch; #number of seconds elapsed since beginning of day
28 return $diff / $total;
29 }
30  
31 method is_day_scheduled($d) {
32 my $str = $d->strftime('%F');
33 return !$self->exclude->{$str};
34 }
35  
36 method days_in_range($start, $end) {
37 $start = $start->clone;
38 $end = $end->clone;
39 $end = $end->add(days => 1) if ($self->day_frac($end) > $self->portion);
40 $start->truncate(to => 'day');
41 $end->truncate(to => 'day');
42  
43 my $dates = [];
44 my $d = $start->clone;
45 while ($d < $end) {
46 push($dates->@*, $d->clone) if ($self->is_day_scheduled($d));
47 $d->add(days => 1);
48 }
49 DateTime::Set->from_datetimes(dates => $dates);
50 }
51  
52 }
53  
54 =head1 NAME
55  
56 DateTime::Schedule - Determine scheduled days in range based on exclusions
57  
58 =head1 SYNOPSIS
59  
60 use DateTime::Schedule;
61  
62 my $dts = DateTime::Schedule->new(exclude => [
63 DateTime->new(year => 2024, month => 01, day => 01),
64 DateTime->new(year => 2024, month => 07, day => 04),
65 DateTime->new(year => 2024, month => 12, day => 25)
66 ]);
67  
68 my $start = DateTime->new(year => 2024, month => 1, day => 1);
69 my $end = DateTime->new(year => 2024, month => 12, day => 31);
70 print $dts->days_in_range($start, $end)->count; # 363
71  
72 =head1 DESCRIPTION
73  
74 This is a simple class that allows you to find out which days are "scheduled"
75 between a start date and an end date. For instance, given the start date of a
76 school year, and the current date, and with all school holidays entered as
77 L</exclude>d, this can tell you how many school days have elapsed in the year.
78  
79 =head1 CONSTRUCTORS
80  
81 =head2 new
82  
83 Default constructor. Returns a new L<DateTime::Schedule> instance.
84  
85 Parameters:
86  
87 =head4 portion
88  
89 I<Optional>. Default C<1>.
90  
91 A number between 0 and 1 indicating how much of a day must elapse to be
92 included/excluded at the boundaries of the range.
93  
94 =head4 exclude
95  
96 I<Optional>. Default C<[]>
97  
98 An arrayref of L<DateTime>s. These days are exclusions to the normal schedule
99 (e.g., holidays). Any time-portion of the DateTimes is ignored.
100  
101 =head1 METHODS
102  
103 =head2 is_day_scheduled($datetime)
104  
105 Given a L<DateTime>, returns true/false to indicate whether that day is
106 scheduled or not.
107  
108 Should be overridden by subclasses.
109  
110 =head2 days_in_range($start, $end)
111  
112 Given start/end L<DateTime>s, returns a L<DateTime::Set> of all the days which
113 are scheduled (i.e., not excluded)
114  
115 =head1 AUTHOR
116  
117 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
118  
119 =head1 LICENSE
120  
121 Copyright (c) 2024 Mark Tyrrell
122  
123 Permission is hereby granted, free of charge, to any person obtaining a copy
124 of this software and associated documentation files (the "Software"), to deal
125 in the Software without restriction, including without limitation the rights
126 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
127 copies of the Software, and to permit persons to whom the Software is
128 furnished to do so, subject to the following conditions:
129  
130 The above copyright notice and this permission notice shall be included in all
131 copies or substantial portions of the Software.
132  
133 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
134 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
135 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
136 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
137 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
138 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
139 SOFTWARE.
140  
141 =cut
142  
143 1;
144  
145 __END__
↑ Summary Schedule/Weekly.pm »