DateTime/Set.pm

filter: Statementstmt 90.5% 308 / 340 Branchbran 75.2% 128 / 170 Conditioncond 53.3% 16 / 30 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 92.9% 53 / 57 Podpod 100% 29 / 29 totaltotal 83.7% 541 / 646 CC 100 SCAR 20.5
CC
100
Cov
84%
as_list
32.5
closest
20.0
from_recurrence
10.0
CRAP 7.8
« DateTime/Span.pm ↑ Summary
1 package DateTime::Set;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Params::Validate qw( validate SCALAR BOOLEAN OBJECT CODEREF ARRAYREF );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use DateTime 0.12; # this is for version checking only
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use DateTime::Span;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Set::Infinite 0.59;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Set::Infinite::_recurrence;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 23 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 23 ▶use constant INFINITY => 100 ** 100 ** 100 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use constant NEG_INFINITY => -1 * (100 ** 100 ** 100);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 23 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 $VERSION = '0.3900';
19 }
20  
21  
22 7952 ▶sub _fix_datetime {
Subroutine _fix_datetime: called CC=5 SCAR=16.3
23 # internal function -
24 # (not a class method)
25 #
26 # checks that the parameter is an object, and
27 # also protects the object against mutation
28
29 7952 ▶ return $_[0]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 12 7940 7952
30 unless defined $_[0]; # error
31 7940 ▶ return $_[0]->clone
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 7851 89 7940
32 if ref( $_[0] ); # "immutable" datetime
33 89 ▶ return DateTime::Infinite::Future->new
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == "Inf" 79 10 89
34 if $_[0] == INFINITY; # Inf
35 10 ▶ return DateTime::Infinite::Past->new
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == "-Inf" 10 0 10
36 if $_[0] == NEG_INFINITY; # -Inf
37 0 return $_[0]; # error
38 }
39  
40 74 ▶sub _fix_return_datetime {
Subroutine _fix_return_datetime: called CC=5 SCAR=16.2
41 74 my ( $dt, $dt_arg ) = @_;
42  
43 # internal function -
44 # (not a class method)
45 #
46 # checks that the returned datetime has the same
47 # time zone as the parameter
48  
49 # TODO: set locale
50  
51 74 ▶ return unless $dt;
Branch: 1 branch
BranchTrueFalseTotal
unless $dt 13 61 74
52 61 ▶ return unless $dt_arg;
Branch: 1 branch
BranchTrueFalseTotal
unless $dt_arg 0 61 61
53 61 ▶ if ( $dt_arg->can('time_zone_long_name') &&
Branch: 1 branch
BranchTrueFalseTotal
if ($dt_arg->can("time_zone_long_name") and not $dt_arg->time_zone_long_name eq "floating") 2 59 61
Condition66%2/3
$dt_arg->can("time_zone_long_name") and not $dt_arg->time_zone_long_name eq "floating"
!ll&&!rl&&r
0592
MC/DC50%1/2
$dt_arg->can("time_zone_long_name") and not $dt_arg->time_zone_long_name eq "floating"
$dt_arg->can("time_zone_long_name") not $dt_arg->time_zone_long_name eq "floating"
Truth table66%2/3
A and not(B)
A= $dt_arg->can("time_zone_long_name")B= not $dt_arg->time_zone_long_name eq "floating"
ABresult
0X0
100
111
54 !( $dt_arg->time_zone_long_name eq 'floating' ) )
55 {
56 2 $dt->set_time_zone( $dt_arg->time_zone );
57 }
58 61 return $dt;
59 }
60  
61 0 ▶sub iterate {
Subroutine iterate: not called CC=1 SCAR=6.9
Pod documented
62 # deprecated method - use map() or grep() instead
63 0 my ( $self, $callback ) = @_;
64 0 my $class = ref( $self );
65 0 my $return = $class->empty_set;
66 $return->{set} = $self->{set}->iterate(
67 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
68 0 my $min = $_[0]->min;
69 0 ▶ $callback->( $min->clone ) if ref($min);
Branch: 1 branch
BranchTrueFalseTotal
if ref $min 0 0 0
70 }
71 0 );
72 0 $return;
73 }
74  
75 1 ▶sub map {
Subroutine map: called CC=2 SCAR=7.0
Pod documented
76 1 my ( $self, $callback ) = @_;
77 1 my $class = ref( $self );
78 1 ▶ die "The callback parameter to map() must be a subroutine reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 1 1
79 unless ref( $callback ) eq 'CODE';
80 1 my $return = $class->empty_set;
81 $return->{set} = $self->{set}->iterate(
82 2 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
83 2 local $_ = $_[0]->min;
84 2 ▶ next unless ref( $_ );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 2 2
85 2 $_ = $_->clone;
86 2 my @list = $callback->();
87 2 my $set = Set::Infinite::_recurrence->new();
88 2 $set = $set->union( $_ ) for @list;
89 2 return $set;
90 }
91 1 );
92 1 $return;
93 }
94  
95 1 ▶sub grep {
Subroutine grep: called CC=2 SCAR=7.0
Pod documented
96 1 my ( $self, $callback ) = @_;
97 1 my $class = ref( $self );
98 1 ▶ die "The callback parameter to grep() must be a subroutine reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 1 1
99 unless ref( $callback ) eq 'CODE';
100 1 my $return = $class->empty_set;
101 $return->{set} = $self->{set}->iterate(
102 2 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
103 2 local $_ = $_[0]->min;
104 2 ▶ next unless ref( $_ );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 2 2
105 2 $_ = $_->clone;
106 2 my $result = $callback->();
107 2 ▶ return $_ if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 1 1 2
108 1 return;
109 }
110 1 );
111 1 $return;
112 }
113  
114 8 ▶sub add { return shift->add_duration( DateTime::Duration->new(@_) ) }
Subroutine add: called CC=1 SCAR=0.0
Pod documented
115  
116 1 ▶sub subtract { return shift->subtract_duration( DateTime::Duration->new(@_) ) }
Subroutine subtract: called CC=1 SCAR=0.0
Pod documented
117  
118 3 ▶sub subtract_duration { return $_[0]->add_duration( $_[1]->inverse ) }
Subroutine subtract_duration: called CC=1 SCAR=0.0
Pod documented
119  
120 18 ▶sub add_duration {
Subroutine add_duration: called CC=1 SCAR=0.0
Pod documented
121 18 my ( $self, $dur ) = @_;
122 18 $dur = $dur->clone; # $dur must be "immutable"
123  
124 $self->{set} = $self->{set}->iterate(
125 74 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
126 74 my $min = $_[0]->min;
127 74 ▶ $min->clone->add_duration( $dur ) if ref($min);
Branch: 1 branch
BranchTrueFalseTotal
if ref $min 74 0 74
128 },
129 18 ▶ backtrack_callback => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
130 18 my ( $min, $max ) = ( $_[0]->min, $_[0]->max );
131 18 ▶ if ( ref($min) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $min) 14 4 18
132 {
133 14 $min = $min->clone;
134 14 $min->subtract_duration( $dur );
135 }
136 18 ▶ if ( ref($max) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $max) 8 10 18
137 {
138 8 $max = $max->clone;
139 8 $max->subtract_duration( $dur );
140 }
141 18 return Set::Infinite::_recurrence->new( $min, $max );
142 },
143 18 );
144 18 $self;
145 }
146  
147 4 ▶sub set_time_zone {
Subroutine set_time_zone: called CC=1 SCAR=0.0
Pod documented
148 4 my ( $self, $tz ) = @_;
149  
150 $self->{set} = $self->{set}->iterate(
151 43 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
152 43 my $min = $_[0]->min;
153 43 ▶ $min->clone->set_time_zone( $tz ) if ref($min);
Branch: 1 branch
BranchTrueFalseTotal
if ref $min 43 0 43
154 },
155 8 ▶ backtrack_callback => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
156 8 my ( $min, $max ) = ( $_[0]->min, $_[0]->max );
157 8 ▶ if ( ref($min) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $min) 6 2 8
158 {
159 6 $min = $min->clone;
160 6 $min->set_time_zone( $tz );
161 }
162 8 ▶ if ( ref($max) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $max) 2 6 8
163 {
164 2 $max = $max->clone;
165 2 $max->set_time_zone( $tz );
166 }
167 8 return Set::Infinite::_recurrence->new( $min, $max );
168 },
169 4 );
170 4 $self;
171 }
172  
173 1 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
174 1 my $self = shift;
175 1 my %args = validate( @_,
176 { locale => { type => SCALAR | OBJECT,
177 default => undef },
178 }
179 );
180 $self->{set} = $self->{set}->iterate(
181 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
182 2 my $min = $_[0]->min;
183 2 ▶ $min->clone->set( %args ) if ref($min);
Branch: 1 branch
BranchTrueFalseTotal
if ref $min 2 0 2
184 },
185 1 );
186 1 $self;
187 }
188  
189 37 ▶sub from_recurrence {
Subroutine from_recurrence: called CC=10 SCAR=23.0
Pod documented
190 37 my $class = shift;
191  
192 37 my %args = @_;
193 37 my %param;
194
195 # Parameter renaming, such that we can use either
196 # recurrence => xxx or next => xxx, previous => xxx
197 37 ▶ $param{next} = delete $args{recurrence} || delete $args{next};
Condition100%3/3
delete $args{'recurrence'} || delete $args{'next'}
l!l&&r!l&&!r
24112
MC/DC100%2/2
delete $args{'recurrence'} || delete $args{'next'}
delete $args{'recurrence'} delete $args{'next'}
Truth table100%3/3
A || B
A= delete $args{'recurrence'}B= delete $args{'next'}
ABresult
1X1
011
000
198 37 $param{previous} = delete $args{previous};
199  
200 37 $param{span} = delete $args{span};
201 # they might be specifying a span using start / end
202 37 ▶ $param{span} = DateTime::Span->new( %args ) if keys %args;
Branch: 1 branch
BranchTrueFalseTotal
if keys %args 14 23 37
203  
204 37 my $self = {};
205
206 die "Not enough arguments in from_recurrence()"
207 37 ▶ unless $param{next} || $param{previous};
Branch: 1 branch
BranchTrueFalseTotal
unless $param{"next"} or $param{"previous"} 0 37 37
Condition66%2/3
$param{"next"} or $param{"previous"}
l!l&&r!l&&!r
3520
MC/DC0%0/2
$param{"next"} or $param{"previous"}
$param{"next"} $param{"previous"}
Truth table66%2/3
A or B
A= $param{"next"}B= $param{"previous"}
ABresult
1X1
011
000
208  
209 37 ▶ if ( ! $param{previous} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $param{'previous'}) { } 30 7 37
210 {
211 30 my $data = {};
212 $param{previous} =
213 1635 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 1635 _callback_previous ( _fix_datetime( $_[0] ), $param{next}, $data );
215 }
216 30 }
217 else
218 {
219 7 my $previous = $param{previous};
220 $param{previous} =
221 28 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 28 $previous->( _fix_datetime( $_[0] ) );
223 }
224 7 }
225  
226 37 ▶ if ( ! $param{next} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $param{'next'}) { } 2 35 37
227 {
228 2 my $data = {};
229 $param{next} =
230 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
231 4 _callback_next ( _fix_datetime( $_[0] ), $param{previous}, $data );
232 }
233 2 }
234 else
235 {
236 35 my $next = $param{next};
237 $param{next} =
238 6055 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
239 6055 $next->( _fix_datetime( $_[0] ) );
240 }
241 35 }
242  
243 37 my ( $min, $max );
244 37 $max = $param{previous}->( DateTime::Infinite::Future->new );
245 37 $min = $param{next}->( DateTime::Infinite::Past->new );
246 37 ▶ $max = INFINITY if $max->is_infinite;
Branch: 1 branch
BranchTrueFalseTotal
if $max->is_infinite 33 4 37
247 37 ▶ $min = NEG_INFINITY if $min->is_infinite;
Branch: 1 branch
BranchTrueFalseTotal
if $min->is_infinite 33 4 37
248
249 37 my $base_set = Set::Infinite::_recurrence->new( $min, $max );
250 $base_set = $base_set->intersection( $param{span}->{set} )
251 37 ▶ if $param{span};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"span"} 16 21 37
252
253 # warn "base set is $base_set\n";
254  
255 37 my $data = {};
256 $self->{set} =
257 $base_set->_recurrence(
258 $param{next},
259 $param{previous},
260 37 $data,
261 );
262 37 bless $self, $class;
263
264 37 return $self;
265 }
266  
267 83 ▶sub from_datetimes {
Subroutine from_datetimes: called CC=3 SCAR=11.0
Pod documented
268 83 my $class = shift;
269 83 my %args = validate( @_,
270 { dates =>
271 { type => ARRAYREF,
272 },
273 }
274 );
275 82 my $self = {};
276 82 $self->{set} = Set::Infinite::_recurrence->new;
277 # possible optimization: sort datetimes and use "push"
278 82 for( @{ $args{dates} } )
279 {
280 # DateTime::Infinite objects are not welcome here,
281 # but this is not enforced (it does't hurt)
282  
283 113 ▶ carp "The 'dates' argument to from_datetimes() must only contain ".
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($_, "utc_rd_values") 0 113 113
284 "datetime objects"
285 unless UNIVERSAL::can( $_, 'utc_rd_values' );
286  
287 113 $self->{set} = $self->{set}->union( $_->clone );
288 }
289  
290 82 bless $self, $class;
291 82 return $self;
292 }
293  
294 79 ▶sub empty_set {
Subroutine empty_set: called CC=1 SCAR=0.0
Pod documented
295 79 my $class = shift;
296  
297 79 return bless { set => Set::Infinite::_recurrence->new }, $class;
298 }
299  
300 2 ▶sub is_empty_set {
Subroutine is_empty_set: called CC=1 SCAR=0.0
Pod documented
301 2 my $set = $_[0];
302 2 $set->{set}->is_null;
303 }
304  
305 54 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
306 54 my $self = bless { %{ $_[0] } }, ref $_[0];
307 54 $self->{set} = $_[0]->{set}->copy;
308 54 return $self;
309 }
310  
311 # default callback that returns the
312 # "previous" value in a callback recurrence.
313 #
314 # This is used to simulate a 'previous' callback,
315 # when then 'previous' argument in 'from_recurrence' is missing.
316 #
317 1635 ▶sub _callback_previous {
Subroutine _callback_previous: called CC=5 SCAR=16.2
318 1635 my ($value, $callback_next, $callback_info) = @_;
319 1635 my $previous = $value->clone;
320  
321 1635 ▶ return $value if $value->is_infinite;
Branch: 1 branch
BranchTrueFalseTotal
if $value->is_infinite 202 1433 1635
322  
323 1433 my $freq = $callback_info->{freq};
324 1433 ▶ unless (defined $freq)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $freq) 28 1405 1433
325 {
326 # This is called just once, to setup the recurrence frequency
327 28 my $previous = $callback_next->( $value );
328 28 my $next = $callback_next->( $previous );
329 28 $freq = 2 * ( $previous - $next );
330 # save it for future use with this same recurrence
331 28 $callback_info->{freq} = $freq;
332 }
333  
334 1433 $previous->add_duration( $freq );
335 1433 $previous = $callback_next->( $previous );
336 1433 ▶ if ($previous >= $value)
Branch: 1 branch
BranchTrueFalseTotal
if ($previous >= $value) 0 1433 1433
337 {
338 # This error happens if the event frequency oscillates widely
339 # (more than 100% of difference from one interval to next)
340 0 my @freq = $freq->deltas;
341 0 print STDERR "_callback_previous: Delta components are: @freq\n";
342 0 warn "_callback_previous: iterator can't find a previous value, got ".
343 $previous->ymd." after ".$value->ymd;
344 }
345 1433 my $previous1;
346 1433 while (1)
347 {
348 1445 $previous1 = $previous->clone;
349 1445 $previous = $callback_next->( $previous );
350 1445 ▶ return $previous1 if $previous >= $value;
Branch: 1 branch
BranchTrueFalseTotal
if $previous >= $value 1433 12 1445
351 }
352 }
353  
354 # default callback that returns the
355 # "next" value in a callback recurrence.
356 #
357 # This is used to simulate a 'next' callback,
358 # when then 'next' argument in 'from_recurrence' is missing.
359 #
360 4 ▶sub _callback_next {
Subroutine _callback_next: called CC=5 SCAR=16.4
361 4 my ($value, $callback_previous, $callback_info) = @_;
362 4 my $next = $value->clone;
363  
364 4 ▶ return $value if $value->is_infinite;
Branch: 1 branch
BranchTrueFalseTotal
if $value->is_infinite 2 2 4
365  
366 2 my $freq = $callback_info->{freq};
367 2 ▶ unless (defined $freq)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $freq) 1 1 2
368 {
369 # This is called just once, to setup the recurrence frequency
370 1 my $next = $callback_previous->( $value );
371 1 my $previous = $callback_previous->( $next );
372 1 $freq = 2 * ( $next - $previous );
373 # save it for future use with this same recurrence
374 1 $callback_info->{freq} = $freq;
375 }
376  
377 2 $next->add_duration( $freq );
378 2 $next = $callback_previous->( $next );
379 2 ▶ if ($next <= $value)
Branch: 1 branch
BranchTrueFalseTotal
if ($next <= $value) 0 2 2
380 {
381 # This error happens if the event frequency oscillates widely
382 # (more than 100% of difference from one interval to next)
383 0 my @freq = $freq->deltas;
384 0 print STDERR "_callback_next: Delta components are: @freq\n";
385 0 warn "_callback_next: iterator can't find a previous value, got ".
386 $next->ymd." before ".$value->ymd;
387 }
388 2 my $next1;
389 2 while (1)
390 {
391 2 $next1 = $next->clone;
392 2 $next = $callback_previous->( $next );
393 2 ▶ return $next1 if $next >= $value;
Branch: 1 branch
BranchTrueFalseTotal
if $next >= $value 2 0 2
394 }
395 }
396  
397 40 ▶sub iterator {
Subroutine iterator: called CC=3 SCAR=11.0
Pod documented
398 40 my $self = shift;
399  
400 40 my %args = @_;
401 40 my $span;
402 40 $span = delete $args{span};
403 40 ▶ $span = DateTime::Span->new( %args ) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 10 30 40
404  
405 40 ▶ return $self->intersection( $span ) if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 15 25 40
406 25 return $self->clone;
407 }
408  
409  
410 # next() gets the next element from an iterator()
411 # next( $dt ) returns the next element after a datetime.
412 1367 ▶sub next {
Subroutine next: called CC=5 SCAR=16.1
Pod documented
413 1367 my $self = shift;
414 1367 ▶ return undef unless ref( $self->{set} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"set"} 12 1355 1367
415  
416 1355 ▶ if ( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 39 1316 1355
417 {
418 39 ▶ if ( $self->{set}->_is_recurrence )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'set'}->_is_recurrence) { } 20 19 39
419 {
420 return _fix_return_datetime(
421 20 $self->{set}->{param}[0]->( $_[0] ), $_[0] );
422 }
423 else
424 {
425 19 my $span = DateTime::Span->from_datetimes( after => $_[0] );
426 19 return _fix_return_datetime(
427 $self->intersection( $span )->next, $_[0] );
428 }
429 }
430  
431 1316 my ($head, $tail) = $self->{set}->first;
432 1316 $self->{set} = $tail;
433 1316 ▶ return $head->min if defined $head;
Branch: 1 branch
BranchTrueFalseTotal
if defined $head 1313 3 1316
434 3 return $head;
435 }
436  
437 # previous() gets the last element from an iterator()
438 # previous( $dt ) returns the previous element before a datetime.
439 126 ▶sub previous {
Subroutine previous: called CC=5 SCAR=16.1
Pod documented
440 126 my $self = shift;
441 126 ▶ return undef unless ref( $self->{set} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"set"} 15 111 126
442  
443 111 ▶ if ( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 35 76 111
444 {
445 35 ▶ if ( $self->{set}->_is_recurrence )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'set'}->_is_recurrence) { } 13 22 35
446 {
447 return _fix_return_datetime(
448 13 $self->{set}->{param}[1]->( $_[0] ), $_[0] );
449 }
450 else
451 {
452 22 my $span = DateTime::Span->from_datetimes( before => $_[0] );
453 22 return _fix_return_datetime(
454 $self->intersection( $span )->previous, $_[0] );
455 }
456 }
457  
458 76 my ($head, $tail) = $self->{set}->last;
459 76 $self->{set} = $tail;
460 76 ▶ return $head->max if defined $head;
Branch: 1 branch
BranchTrueFalseTotal
if defined $head 66 10 76
461 10 return $head;
462 }
463  
464 # "current" means less-or-equal to a datetime
465 29 ▶sub current {
Subroutine current: called CC=4 SCAR=14.0
Pod documented
466 29 my $self = shift;
467  
468 29 ▶ return undef unless ref( $self->{set} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"set"} 0 29 29
469  
470 29 ▶ if ( $self->{set}->_is_recurrence )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"set"}->_is_recurrence) 9 20 29
471 {
472 9 my $tmp = $self->next( $_[0] );
473 9 return $self->previous( $tmp );
474 }
475  
476 20 ▶ return $_[0] if $self->contains( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if $self->contains($_[0]) 0 20 20
477 20 $self->previous( $_[0] );
478 }
479  
480 0 ▶sub closest {
Subroutine closest: not called CC=4 SCAR=30.0
Pod documented
481 0 my $self = shift;
482 # return $_[0] if $self->contains( $_[0] );
483 0 my $dt1 = $self->current( $_[0] );
484 0 my $dt2 = $self->next( $_[0] );
485  
486 0 ▶ return $dt2 unless defined $dt1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dt1 0 0 0
487 0 ▶ return $dt1 unless defined $dt2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dt2 0 0 0
488  
489 0 my $delta = $_[0] - $dt1;
490 0 ▶ return $dt1 if ( $dt2 - $delta ) >= $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $dt2 - $delta >= $_[0] 0 0 0
491  
492 0 return $dt2;
493 }
494  
495 4 ▶sub as_list {
Subroutine as_list: called CC=17 SCAR=34.8
Pod documented
496 4 my $self = shift;
497 4 ▶ return undef unless ref( $self->{set} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"set"} 0 4 4
498  
499 4 my %args = @_;
500 4 my $span;
501 4 $span = delete $args{span};
502 4 ▶ $span = DateTime::Span->new( %args ) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 1 3 4
503  
504 4 my $set = $self->clone;
505 4 ▶ $set = $set->intersection( $span ) if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 1 3 4
506  
507 4 ▶ return if $set->{set}->is_null; # nothing = empty
Branch: 1 branch
BranchTrueFalseTotal
if $set->{"set"}->is_null 1 3 4
508  
509 # Note: removing this line means we may end up in an infinite loop!
510 ## return undef if $set->{set}->is_too_complex; # undef = no start/end
511
512 return undef
513 3 ▶ if $set->max->is_infinite ||
Branch: 1 branch
BranchTrueFalseTotal
if $set->max->is_infinite or $set->min->is_infinite 1 2 3
Condition66%2/3
$set->max->is_infinite or $set->min->is_infinite
l!l&&r!l&&!r
102
MC/DC50%1/2
$set->max->is_infinite or $set->min->is_infinite
$set->max->is_infinite $set->min->is_infinite
Truth table66%2/3
A or B
A= $set->max->is_infiniteB= $set->min->is_infinite
ABresult
1X1
011
000
514 $set->min->is_infinite;
515  
516 2 my @result;
517 2 my $next = $self->min;
518 2 ▶ if ( $span ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span) 0 2 2
519 0 my $next1 = $span->min;
520 0 ▶ $next = $next1 if $next1 && $next1 > $next;
Branch: 1 branch
BranchTrueFalseTotal
if $next1 and $next1 > $next 0 0 0
Condition0%0/3
$next1 and $next1 > $next
!ll&&!rl&&r
000
MC/DC0%0/2
$next1 and $next1 > $next
$next1 $next1 > $next
Truth table0%0/3
A and B
A= $next1B= $next1 > $next
ABresult
0X0
100
111
521 0 $next = $self->current( $next );
522 }
523 2 my $last = $self->max;
524 2 ▶ if ( $span ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span) 0 2 2
525 0 my $last1 = $span->max;
526 0 ▶ $last = $last1 if $last1 && $last1 < $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last1 and $last1 < $last 0 0 0
Condition0%0/3
$last1 and $last1 < $last
!ll&&!rl&&r
000
MC/DC0%0/2
$last1 and $last1 < $last
$last1 $last1 < $last
Truth table0%0/3
A and B
A= $last1B= $last1 < $last
ABresult
0X0
100
111
527 }
528 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$next and $next <= $last and do { do { push @result, $next if not $span or $span->contains($next); $next = $self->next($next) } } 1 2 3
Condition66%2/3
$next and $next <= $last
!ll&&!rl&&r
201
MC/DC50%1/2
$next and $next <= $last
$next $next <= $last
Truth table66%2/3
A and B
A= $nextB= $next <= $last
ABresult
0X0
100
111
529 3 ▶ push @result, $next if !$span || $span->contains($next);
Branch: 1 branch
BranchTrueFalseTotal
if not $span or $span->contains($next) 3 0 3
Condition33%1/3
not $span or $span->contains($next)
l!l&&r!l&&!r
300
MC/DC0%0/2
not $span or $span->contains($next)
not $span $span->contains($next)
Truth table33%1/3
not(A) or B
A= not $spanB= $span->contains($next)
ABresult
1X1
011
000
530 3 $next = $self->next( $next );
531 }
532 while $next && $next <= $last;
533 2 return @result;
534 }
535  
536 65 ▶sub intersection {
Subroutine intersection: called CC=3 SCAR=11.0
Pod documented
537 65 my ($set1, $set2) = ( shift, shift );
538 65 my $class = ref($set1);
539 65 my $tmp = $class->empty_set();
540 65 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 65 65
541 if $set2->can( 'as_set' );
542 65 ▶ $set2 = $class->from_datetimes( dates => [ $set2, @_ ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $set2->can("union") 1 64 65
543 unless $set2->can( 'union' );
544 65 $tmp->{set} = $set1->{set}->intersection( $set2->{set} );
545 65 return $tmp;
546 }
547  
548 10 ▶sub intersects {
Subroutine intersects: called CC=6 SCAR=17.9
Pod documented
549 10 my ($set1, $set2) = ( shift, shift );
550 10 my $class = ref($set1);
551 10 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 10 10
552 if $set2->can( 'as_set' );
553 10 ▶ unless ( $set2->can( 'union' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($set2->can("union")) 9 1 10
554 {
555 9 ▶ if ( $set1->{set}->_is_recurrence )
Branch: 1 branch
BranchTrueFalseTotal
if ($set1->{"set"}->_is_recurrence) 3 6 9
556 {
557 3 for ( $set2, @_ )
558 {
559 4 ▶ return 1 if $set1->current( $_ ) == $_;
Branch: 1 branch
BranchTrueFalseTotal
if $set1->current($_) == $_ 2 2 4
560 }
561 1 return 0;
562 }
563 6 $set2 = $class->from_datetimes( dates => [ $set2, @_ ] )
564 }
565 7 return $set1->{set}->intersects( $set2->{set} );
566 }
567  
568 30 ▶sub contains {
Subroutine contains: called CC=6 SCAR=17.9
Pod documented
569 30 my ($set1, $set2) = ( shift, shift );
570 30 my $class = ref($set1);
571 30 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 30 30
572 if $set2->can( 'as_set' );
573 30 ▶ unless ( $set2->can( 'union' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($set2->can("union")) 29 1 30
574 {
575 29 ▶ if ( $set1->{set}->_is_recurrence )
Branch: 1 branch
BranchTrueFalseTotal
if ($set1->{"set"}->_is_recurrence) 3 26 29
576 {
577 3 for ( $set2, @_ )
578 {
579 3 ▶ return 0 unless $set1->current( $_ ) == $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $set1->current($_) == $_ 2 1 3
580 }
581 1 return 1;
582 }
583 26 $set2 = $class->from_datetimes( dates => [ $set2, @_ ] )
584 }
585 27 return $set1->{set}->contains( $set2->{set} );
586 }
587  
588 4 ▶sub union {
Subroutine union: called CC=5 SCAR=17.3
Pod documented
589 4 my ($set1, $set2) = ( shift, shift );
590 4 my $class = ref($set1);
591 4 my $tmp = $class->empty_set();
592 4 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 4 4
593 if $set2->can( 'as_set' );
594 4 ▶ $set2 = $class->from_datetimes( dates => [ $set2, @_ ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $set2->can("union") 0 4 4
595 unless $set2->can( 'union' );
596 4 $tmp->{set} = $set1->{set}->union( $set2->{set} );
597 4 ▶ bless $tmp, 'DateTime::SpanSet'
Branch: 1 branch
BranchTrueFalseTotal
if $set2->isa("DateTime::Span") or $set2->isa("DateTime::SpanSet") 0 4 4
Condition33%1/3
$set2->isa("DateTime::Span") or $set2->isa("DateTime::SpanSet")
l!l&&r!l&&!r
004
MC/DC0%0/2
$set2->isa("DateTime::Span") or $set2->isa("DateTime::SpanSet")
$set2->isa("DateTime::Span") $set2->isa("DateTime::SpanSet")
Truth table33%1/3
A or B
A= $set2->isa("DateTime::Span")B= $set2->isa("DateTime::SpanSet")
ABresult
1X1
011
000
598 if $set2->isa('DateTime::Span') or $set2->isa('DateTime::SpanSet');
599 4 return $tmp;
600 }
601  
602 2 ▶sub complement {
Subroutine complement: called CC=4 SCAR=15.0
Pod documented
603 2 my ($set1, $set2) = ( shift, shift );
604 2 my $class = ref($set1);
605 2 my $tmp = $class->empty_set();
606 2 ▶ if (defined $set2)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $set2) { } 2 0 2
607 {
608 2 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 2 2
609 if $set2->can( 'as_set' );
610 2 ▶ $set2 = $class->from_datetimes( dates => [ $set2, @_ ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $set2->can("union") 2 0 2
611 unless $set2->can( 'union' );
612 # TODO: "compose complement";
613 2 $tmp->{set} = $set1->{set}->complement( $set2->{set} );
614 }
615 else
616 {
617 0 $tmp->{set} = $set1->{set}->complement;
618 0 bless $tmp, 'DateTime::SpanSet';
619 }
620 2 return $tmp;
621 }
622  
623 54 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
624 54 return _fix_datetime( $_[0]->{set}->min );
625 }
626  
627 23 *min = \&start;
628  
629 34 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
630 34 return _fix_datetime( $_[0]->{set}->max );
631 }
632  
633 23 *max = \&end;
634  
635 # returns a DateTime::Span
636 0 ▶sub span {
Subroutine span: not called CC=1 SCAR=6.9
Pod documented
637 0 my $set = $_[0]->{set}->span;
638 0 my $self = bless { set => $set }, 'DateTime::Span';
639 0 return $self;
640 }
641  
642 9 ▶sub count {
Subroutine count: called CC=8 SCAR=20.8
Pod documented
643 9 my ($self) = shift;
644 9 ▶ return undef unless ref( $self->{set} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"set"} 0 9 9
645  
646 9 my %args = @_;
647 9 my $span;
648 9 $span = delete $args{span};
649 9 ▶ $span = DateTime::Span->new( %args ) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 1 8 9
650  
651 9 my $set = $self->clone;
652 9 ▶ $set = $set->intersection( $span ) if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 1 8 9
653  
654 return $set->{set}->count
655 9 ▶ unless $set->{set}->is_too_complex;
Branch: 1 branch
BranchTrueFalseTotal
unless $set->{"set"}->is_too_complex 5 4 9
656  
657 return undef
658 4 ▶ if $set->max->is_infinite ||
Branch: 1 branch
BranchTrueFalseTotal
if $set->max->is_infinite or $set->min->is_infinite 3 1 4
Condition100%3/3
$set->max->is_infinite or $set->min->is_infinite
l!l&&r!l&&!r
121
MC/DC100%2/2
$set->max->is_infinite or $set->min->is_infinite
$set->max->is_infinite $set->min->is_infinite
Truth table100%3/3
A or B
A= $set->max->is_infiniteB= $set->min->is_infinite
ABresult
1X1
011
000
659 $set->min->is_infinite;
660  
661 1 my $count = 0;
662 1 my $iter = $set->iterator;
663 1 ▶ $count++ while $iter->next;
Branch: 1 branch
BranchTrueFalseTotal
$iter->next and do { ++$count } 1200 1 1201
664 1 return $count;
665 }
666  
667 23 1;
668  
669 __END__
« DateTime/Span.pm ↑ Summary