blib/lib/Time/WorkHours.pm

filter: Statementstmt 100% 81 / 81 Branchbran 81.2% 13 / 16 Conditioncond 50.0% 6 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 8 / 8 totaltotal 88.4% 123 / 139 CC 13 SCAR 8.5
CC
13
Cov
92%
within
4.2
new
3.1
shift_to_work_time
3.0
CRAP 2.3
↑ Summary
1 package Time::WorkHours;
2  
3 =head1 NAME
4  
5 Time::WorkHours - proportional shift DateTime to a work hours and few work hours routines
6  
7 =head1 SYNOPSIS
8  
9 # our work hours is from 02:00 until 07:00
10 my $wh = Time::WorkHours->new(
11 work_interval_start => '02h',
12 work_interval => '5h', # or 5*60
13 );
14
15 # shift given date to our work hours
16 my $new_datetime = $wh->shift_to_work_time(DateTime->new(
17 'hour' => 14,
18 'minute' => 32,
19 # ... what ever month, year, day
20 ));
21  
22 =head1 DESCRIPTION
23  
24 Purpouse of this module is to equaly distribute tasks that are comming
25 through the whole day into certain day interval.
26  
27 Or just move comming request to the start of work hours.
28  
29 For example imagine you want to process smoke tests only in the idle
30 server hours - in the night. But the CPAN modules are comming through
31 all day. You can note down the modules as they are comming and set
32 the processing time to C<< $wh->shift_to_work_time() >> so that
33 it will not take the processing time when the server has to do it's
34 "real" job.
35  
36 =cut
37  
38 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 3 our $VERSION = '0.01';
42  
43 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use Carp::Clan 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 3 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 =head1 PROPERTIES
49  
50 work_interval_start
51 work_interval
52  
53 =cut
54  
55 3 __PACKAGE__->mk_accessors(qw{
56 work_interval_start
57 work_interval
58 });
59  
60  
61 3 my $DAY_MINUTES = 24*60;
62  
63  
64 =head1 METHODS
65  
66 =head2 new()
67  
68 Object constructor. Pass two mandatory arguments. C<work_interval_start>
69 and C<work_interval>.
70  
71 C<work_interval_start> is the minute (or hour) when the work hours
72 start.
73  
74 C<work_interval> is how many minutes (or hours) does the work interval
75 last.
76  
77 Both can be passed as a number in that case must represent minutes or as a string
78 with numbers and 'h' at the end representing the value in hours.
79  
80 Example:
81  
82 $wh = Time::WorkHours->new(
83 work_interval_start => '2h', # or 120
84 work_interval => 180, # or '3h'
85 );
86  
87 Work interval starts at 02:00 and lasts for 3 hours.
88  
89 =cut
90  
91 207 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
92 207 my $class = shift;
93 207 my $self = $class->SUPER::new({ @_ });
94
95 207 ▶ croak 'pass work_interval_start'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->work_interval_start 0 207 207
96 if not defined $self->work_interval_start;
97 207 ▶ croak 'pass work_interval'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->work_interval 0 207 207
98 if not defined $self->work_interval;
99
100 207 return $self;
101 }
102  
103  
104 =head2 work_start($datetime)
105  
106 Return nearest DateTime when the work time starts. If inside
107 the work interval then returns start datetime of this
108 interval.
109  
110 If argument not passed the default is C<< DateTime->now >>.
111  
112 =cut
113  
114 2460 ▶sub work_start {
Subroutine work_start: called CC=2 SCAR=6.9
Pod documented
115 2460 my $self = shift;
116 2460 my $date = shift;
117
118 # make a copy of passed DateTime
119 2460 ▶ if ($date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date) { } 2459 1 2460
120 2459 $date = $date->clone();
121 }
122 else {
123 1 $date = DateTime->now();
124 }
125 2460 _strip_seconds($date);
126 2460 my $work_start = $date->clone;
127  
128 2460 my $work_interval_start = $self->work_interval_start_minute;
129 2460 my $work_interval = $self->work_interval_minutes;
130 2460 my $work_interval_end = $work_interval_start + $work_interval;
131 2460 my $work_interval_shift += $DAY_MINUTES - $work_interval_end;
132  
133 2460 $date->add('minutes' => $work_interval_shift);
134 2460 my $date_minutes = $date->hour*60 + $date->minute;
135
136 2460 $work_start->add('minutes' => $work_interval_start + $work_interval_shift - $date_minutes);
137
138 2460 return $work_start;
139 }
140  
141  
142 =head2 next_work_start($datetime)
143  
144 Same as work_start but will always return DateTime in the
145 future.
146  
147 =cut
148  
149 2 ▶sub next_work_start {
Subroutine next_work_start: called CC=2 SCAR=6.9
Pod documented
150 2 my $self = shift;
151 2 my $date = shift;
152
153 2 my $work_start = $self->work_start($date);
154
155 # shift by 24h if date is within work hours so the work_start is in the past
156 2 ▶ $work_start->add('hours' => 24)
Branch: 1 branch
BranchTrueFalseTotal
if $work_start < $date 1 1 2
157 if $work_start < $date;
158
159 2 return $work_start;
160 }
161  
162  
163 =head2 work_end($datetime)
164  
165 Returns nearest end of the work time.
166  
167 If argument not passed the default is C<< DateTime->now >>.
168  
169 =cut
170  
171 1127 ▶sub work_end {
Subroutine work_end: called CC=2 SCAR=7.0
Pod documented
172 1127 my $self = shift;
173 1127 ▶ my $date = shift || DateTime->now;
Condition66%2/3
shift() || 'DateTime'->now
l!l&&r!l&&!r
112610
MC/DC0%0/2
shift() || 'DateTime'->now
shift() 'DateTime'->now
Truth table66%2/3
A || B
A= shift()B= 'DateTime'->now
ABresult
1X1
011
000
174
175 1127 my $work_start = $self->work_start($date);
176 1127 my $work_interval = $self->work_interval_minutes;
177  
178 1127 return $work_start->add('minutes' => $work_interval);;
179 }
180  
181  
182 =head2 within($datetime)
183  
184 Return true/false if the $datetime lies within work hours.
185  
186 If argument not passed the default is C<< DateTime->now >>.
187  
188 =cut
189  
190 1025 ▶sub within {
Subroutine within: called CC=4 SCAR=14.2
Pod documented
191 1025 my $self = shift;
192 1025 ▶ my $date = shift || DateTime->now;
Condition33%1/3
shift() || 'DateTime'->now
l!l&&r!l&&!r
102500
MC/DC0%0/2
shift() || 'DateTime'->now
shift() 'DateTime'->now
Truth table33%1/3
A || B
A= shift()B= 'DateTime'->now
ABresult
1X1
011
000
193
194 1025 my $work_start_datetime = $self->work_start($date);
195 1025 my $work_end_datetime = $self->work_end($date);
196
197 1025 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $date >= $work_start_datetime and $date < $work_end_datetime 359 666 1025
Condition66%2/3
$date >= $work_start_datetime and $date < $work_end_datetime
!ll&&!rl&&r
6660359
MC/DC50%1/2
$date >= $work_start_datetime and $date < $work_end_datetime
$date >= $work_start_datetime $date < $work_end_datetime
Truth table66%2/3
A and B
A= $date >= $work_start_datetimeB= $date < $work_end_datetime
ABresult
0X0
100
111
198 if (($date >= $work_start_datetime) and ($date < $work_end_datetime));
199 666 return 0;
200 }
201  
202  
203 =head2 shift_to_work_time($date)
204  
205 Takes the $date and moves it to the neares work time interval.
206 The shift is calculated proportionaly so that the time shifts are
207 distributed in the work hour interval in the same order as
208 they occure in 24h interval.
209  
210 Example:
211  
212 my $wh = Time::WorkHours->new(
213 work_interval_start => '22h',
214 work_interval => '4h',
215 );
216 my $new_datetime = $wh->shift_to_work_time(DateTime->new(
217 'day' => 5,
218 'hour' => 14,
219 'minute' => 00,
220 # ... what ever month, year
221 ));
222  
223 Will shift to next day to 00:00 as 14:00 is just in the middle of 02:00 - (22:00) - 02:00
224 interval so it's shifted to the middle of 22:00 - 02:00 work hours.
225  
226 If the DateTime will be at 01:59 (last minute of the work interval) there will be no shift.
227  
228 If the DateTime will be at 02:00 (first non work minute) the shift will be to 22:00.
229  
230 If argument not passed the default is C<< DateTime->now >>.
231  
232 =cut
233  
234 103 ▶sub shift_to_work_time {
Subroutine shift_to_work_time: called CC=3 SCAR=11.1
Pod documented
235 103 my $self = shift;
236 103 ▶ my $date = shift || DateTime->now;
Condition33%1/3
shift() || 'DateTime'->now
l!l&&r!l&&!r
10300
MC/DC0%0/2
shift() || 'DateTime'->now
shift() 'DateTime'->now
Truth table33%1/3
A || B
A= shift()B= 'DateTime'->now
ABresult
1X1
011
000
237
238 103 ▶ croak 'pass DataTime object as argument'
Branch: 1 branch
BranchTrueFalseTotal
if ref $date ne "DateTime" 0 103 103
239 if ref $date ne 'DateTime';
240
241 103 $date = $date->clone;
242
243 103 my $work_interval_start = $self->work_interval_start_minute;
244 103 my $work_interval = $self->work_interval_minutes;
245 103 my $work_start = $self->work_start($date);
246 103 my $work_interval_end = $work_interval_start + $work_interval;
247 103 my $work_interval_shift = $DAY_MINUTES - $work_interval_end;
248  
249 103 $date->add('minutes' => $work_interval_shift);
250 103 my $date_minutes = $date->hour*60 + $date->minute;
251
252 103 my $event_date = $work_start->add('minutes' => ($date_minutes / $DAY_MINUTES) * $work_interval);
253
254 103 return $event_date;
255 }
256  
257  
258 =head2 work_interval_start_minute()
259  
260 Return number of minute in the day when the work interval starts.
261  
262 =cut
263  
264 2564 ▶sub work_interval_start_minute {
Subroutine work_interval_start_minute: called CC=2 SCAR=6.9
Pod documented
265 2564 my $self = shift;
266 2564 my $work_interval_start = $self->work_interval_start;
267
268 2564 ▶ if ($work_interval_start =~ m/\b([0-9]+)h$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_interval_start =~ /\b([0-9]+)h$/) { } 2264 300 2564
269 2264 return $1*60;
270 }
271 else {
272 300 return $work_interval_start;
273 }
274 }
275  
276  
277 =head2 work_interval_minutes()
278  
279 Return for how many minutes does the work interval lasts.
280  
281 =cut
282  
283 3691 ▶sub work_interval_minutes {
Subroutine work_interval_minutes: called CC=2 SCAR=6.9
Pod documented
284 3691 my $self = shift;
285 3691 my $work_interval = $self->work_interval;
286
287 3691 ▶ if ($work_interval =~ m/\b([0-9]+)h$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work_interval =~ /\b([0-9]+)h$/) { } 2791 900 3691
288 2791 return $1*60;
289 }
290 else {
291 900 return $work_interval;
292 }
293 }
294  
295 2460 ▶sub _strip_seconds {
Subroutine _strip_seconds: called CC=1 SCAR=0.0
296 2460 my $date = shift;
297 2460 $date->add('seconds' => -$date->second);
298 2460 return $date;
299 }
300  
301  
302 3 'ROMERQUELLE(R)';
303  
304  
305 __END__
↑ Summary