DateTime/Span.pm

filter: Statementstmt 80.4% 119 / 148 Branchbran 53.9% 41 / 76 Conditioncond 58.3% 7 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 77.7% 21 / 27 Podpod 84.2% 16 / 19 totaltotal 70.6% 205 / 290 CC 45 SCAR 21.1
CC
45
Cov
71%
complement
30.0
from_datetimes
15.0
from_datetime_and_duration
9.6
CRAP 8.2
« DateTime/SpanSet.pm ↑ Summary DateTime/Set.pm »
1 # Copyright (c) 2003 Flavio Soibelmann Glock. All rights reserved.
2 # This program is free software; you can redistribute it and/or
3 # modify it under the same terms as Perl itself.
4  
5 package DateTime::Span;
6  
7 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 23 ▶use DateTime::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use DateTime::SpanSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 23 ▶use Params::Validate qw( validate SCALAR BOOLEAN OBJECT CODEREF ARRAYREF );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 23 ▶use constant INFINITY => DateTime::INFINITY;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use constant NEG_INFINITY => DateTime::NEG_INFINITY;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 $VERSION = $DateTime::Set::VERSION;
18  
19 2 ▶sub set_time_zone {
Subroutine set_time_zone: called CC=1 SCAR=0.0
Pod documented
20 2 my ( $self, $tz ) = @_;
21  
22 $self->{set} = $self->{set}->iterate(
23 2 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
24 2 my %tmp = %{ $_[0]->{list}[0] };
25 2 ▶ $tmp{a} = $tmp{a}->clone->set_time_zone( $tz ) if ref $tmp{a};
Branch: 1 branch
BranchTrueFalseTotal
if ref $tmp{"a"} 2 0 2
26 2 ▶ $tmp{b} = $tmp{b}->clone->set_time_zone( $tz ) if ref $tmp{b};
Branch: 1 branch
BranchTrueFalseTotal
if ref $tmp{"b"} 2 0 2
27 2 \%tmp;
28 }
29 2 );
30 2 return $self;
31 }
32  
33 # note: the constructor must clone its DateTime parameters, such that
34 # the set elements become immutable
35 115 ▶sub from_datetimes {
Subroutine from_datetimes: called CC=14 SCAR=27.1
Pod documented
36 115 my $class = shift;
37 115 my %args = validate( @_,
38 { start =>
39 { type => OBJECT,
40 optional => 1,
41 },
42 end =>
43 { type => OBJECT,
44 optional => 1,
45 },
46 after =>
47 { type => OBJECT,
48 optional => 1,
49 },
50 before =>
51 { type => OBJECT,
52 optional => 1,
53 },
54 }
55 );
56 115 my $self = {};
57 115 my $set;
58  
59 115 ▶ die "No arguments given to DateTime::Span->from_datetimes\n"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %args 0 115 115
60 unless keys %args;
61  
62 115 ▶ if ( exists $args{start} && exists $args{after} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"start"} and exists $args{"after"}) 0 115 115
Condition66%2/3
exists $args{"start"} and exists $args{"after"}
!ll&&!rl&&r
63520
MC/DC0%0/2
exists $args{"start"} and exists $args{"after"}
exists $args{"start"} exists $args{"after"}
Truth table66%2/3
A and B
A= exists $args{"start"}B= exists $args{"after"}
ABresult
0X0
100
111
63 0 die "Cannot give both start and after arguments to DateTime::Span->from_datetimes\n";
64 }
65 115 ▶ if ( exists $args{end} && exists $args{before} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"end"} and exists $args{"before"}) 0 115 115
Condition66%2/3
exists $args{"end"} and exists $args{"before"}
!ll&&!rl&&r
78370
MC/DC0%0/2
exists $args{"end"} and exists $args{"before"}
exists $args{"end"} exists $args{"before"}
Truth table66%2/3
A and B
A= exists $args{"end"}B= exists $args{"before"}
ABresult
0X0
100
111
66 0 die "Cannot give both end and before arguments to DateTime::Span->from_datetimes\n";
67 }
68  
69 115 my ( $start, $open_start, $end, $open_end );
70 115 ( $start, $open_start ) = ( NEG_INFINITY, 0 );
71 115 ▶ ( $start, $open_start ) = ( $args{start}, 0 ) if exists $args{start};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"start"} 52 63 115
72 115 ▶ ( $start, $open_start ) = ( $args{after}, 1 ) if exists $args{after};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"after"} 26 89 115
73 115 ( $end, $open_end ) = ( INFINITY, 0 );
74 115 ▶ ( $end, $open_end ) = ( $args{end}, 0 ) if exists $args{end};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"end"} 37 78 115
75 115 ▶ ( $end, $open_end ) = ( $args{before}, 1 ) if exists $args{before};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"before"} 28 87 115
76  
77 115 ▶ if ( $start > $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $end) 0 115 115
78 0 die "Span cannot start after the end in DateTime::Span->from_datetimes\n";
79 }
80 115 $set = Set::Infinite::_recurrence->new( $start, $end );
81 115 ▶ if ( $start != $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start != $end) 105 10 115
82 # remove start, such that we have ">" instead of ">="
83 105 ▶ $set = $set->complement( $start ) if $open_start;
Branch: 1 branch
BranchTrueFalseTotal
if $open_start 26 79 105
84 # remove end, such that we have "<" instead of "<="
85 105 ▶ $set = $set->complement( $end ) if $open_end;
Branch: 1 branch
BranchTrueFalseTotal
if $open_end 18 87 105
86 }
87  
88 115 $self->{set} = $set;
89 115 bless $self, $class;
90 115 return $self;
91 }
92  
93 3 ▶sub from_datetime_and_duration {
Subroutine from_datetime_and_duration: called CC=9 SCAR=22.7
Pod documented
94 3 my $class = shift;
95 3 my %args = @_;
96  
97 3 my $key;
98 my $dt;
99 # extract datetime parameters
100 3 for ( qw( start end before after ) ) {
101 12 ▶ if ( exists $args{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$_}) 3 9 12
102 3 $key = $_;
103 3 $dt = delete $args{$_};
104 }
105 }
106  
107 # extract duration parameters
108 3 my $dt_duration;
109 3 ▶ if ( exists $args{duration} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'duration'}) { } 0 3 3
110 0 $dt_duration = $args{duration};
111 }
112 else {
113 3 $dt_duration = DateTime::Duration->new( %args );
114 }
115 # warn "Creating span from $key => ".$dt->datetime." and $dt_duration";
116 3 my $other_date;
117 my $other_key;
118 3 ▶ if ( $dt_duration->is_positive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt_duration->is_positive) { } 2 1 3
119 2 ▶ if ( $key eq 'end' || $key eq 'before' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'end' or $key eq 'before') { } 1 1 2
Condition66%2/3
$key eq "end" or $key eq "before"
l!l&&r!l&&!r
101
MC/DC50%1/2
$key eq "end" or $key eq "before"
$key eq "end" $key eq "before"
Truth table66%2/3
A or B
A= $key eq "end"B= $key eq "before"
ABresult
1X1
011
000
120 1 $other_key = 'start';
121 1 $other_date = $dt->clone->subtract_duration( $dt_duration );
122 }
123 else {
124 1 $other_key = 'before';
125 1 $other_date = $dt->clone->add_duration( $dt_duration );
126 }
127 }
128 else {
129 1 ▶ if ( $key eq 'end' || $key eq 'before' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'end' or $key eq 'before') { } 1 0 1
Condition33%1/3
$key eq "end" or $key eq "before"
l!l&&r!l&&!r
100
MC/DC0%0/2
$key eq "end" or $key eq "before"
$key eq "end" $key eq "before"
Truth table33%1/3
A or B
A= $key eq "end"B= $key eq "before"
ABresult
1X1
011
000
130 1 $other_key = 'start';
131 1 $other_date = $dt->clone->add_duration( $dt_duration );
132 }
133 else {
134 0 $other_key = 'before';
135 0 $other_date = $dt->clone->subtract_duration( $dt_duration );
136 }
137 }
138 # warn "Creating span from $key => ".$dt->datetime." and ".$other_date->datetime;
139 3 return $class->new( $key => $dt, $other_key => $other_date );
140 }
141  
142 # This method is intentionally not documented. It's really only for
143 # use by ::Set and ::SpanSet's as_list() and iterator() methods.
144 32 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
145 32 my $class = shift;
146 32 my %args = @_;
147  
148 # If we find anything _not_ appropriate for from_datetimes, we
149 # assume it must be for durations, and call this constructor.
150 # This way, we don't need to hardcode the DateTime::Duration
151 # parameters.
152 32 foreach ( keys %args )
153 {
154 43 ▶ return $class->from_datetime_and_duration(%args)
Branch: 1 branch
BranchTrueFalseTotal
unless /^(?:before|after|start|end)$/ 0 43 43
155 unless /^(?:before|after|start|end)$/;
156 }
157  
158 32 return $class->from_datetimes(%args);
159 }
160  
161 0 ▶sub is_empty_set {
Subroutine is_empty_set: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my $set = $_[0];
163 0 $set->{set}->is_null;
164 }
165  
166 1 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
167 bless {
168 set => $_[0]->{set}->copy,
169 1 }, ref $_[0];
170 }
171  
172 # Set::Infinite methods
173  
174 3 ▶sub intersection {
Subroutine intersection: called CC=4 SCAR=14.2
Pod documented
175 3 my ($set1, $set2) = @_;
176 3 my $class = ref($set1);
177 3 my $tmp = {}; # $class->new();
178 3 ▶ $set2 = $set2->as_spanset
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_spanset") 0 3 3
179 if $set2->can( 'as_spanset' );
180 3 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 3 3
181 if $set2->can( 'as_set' );
182 3 ▶ $set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $set2->can("union") 0 3 3
183 unless $set2->can( 'union' );
184 3 $tmp->{set} = $set1->{set}->intersection( $set2->{set} );
185  
186 # intersection() can generate something more complex than a span.
187 3 bless $tmp, 'DateTime::SpanSet';
188  
189 3 return $tmp;
190 }
191  
192 9 ▶sub intersects {
Subroutine intersects: called CC=4 SCAR=14.5
Pod documented
193 9 my ($set1, $set2) = @_;
194 9 my $class = ref($set1);
195 9 ▶ $set2 = $set2->as_spanset
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_spanset") 0 9 9
196 if $set2->can( 'as_spanset' );
197 9 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 9 9
198 if $set2->can( 'as_set' );
199 9 ▶ $set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $set2->can("union") 9 0 9
200 unless $set2->can( 'union' );
201 9 return $set1->{set}->intersects( $set2->{set} );
202 }
203  
204 1 ▶sub contains {
Subroutine contains: called CC=4 SCAR=14.5
Pod documented
205 1 my ($set1, $set2) = @_;
206 1 my $class = ref($set1);
207 1 ▶ $set2 = $set2->as_spanset
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_spanset") 0 1 1
208 if $set2->can( 'as_spanset' );
209 1 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 1 1
210 if $set2->can( 'as_set' );
211 1 ▶ $set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $set2->can("union") 1 0 1
212 unless $set2->can( 'union' );
213 1 return $set1->{set}->contains( $set2->{set} );
214 }
215  
216 5 ▶sub union {
Subroutine union: called CC=4 SCAR=14.2
Pod documented
217 5 my ($set1, $set2) = @_;
218 5 my $class = ref($set1);
219 5 my $tmp = {}; # $class->new();
220 5 ▶ $set2 = $set2->as_spanset
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_spanset") 0 5 5
221 if $set2->can( 'as_spanset' );
222 5 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 5 5
223 if $set2->can( 'as_set' );
224 5 ▶ $set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $set2->can("union") 0 5 5
225 unless $set2->can( 'union' );
226 5 $tmp->{set} = $set1->{set}->union( $set2->{set} );
227
228 # union() can generate something more complex than a span.
229 5 bless $tmp, 'DateTime::SpanSet';
230  
231 # # We have to check it's internal structure to find out.
232 # if ( $#{ $tmp->{set}->{list} } != 0 ) {
233 # bless $tmp, 'Date::SpanSet';
234 # }
235  
236 5 return $tmp;
237 }
238  
239 0 ▶sub complement {
Subroutine complement: not called CC=5 SCAR=34.0
Pod documented
240 0 my ($set1, $set2) = @_;
241 0 my $class = ref($set1);
242 0 my $tmp = {}; # $class->new;
243 0 ▶ if (defined $set2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $set2) { } 0 0 0
244 0 ▶ $set2 = $set2->as_spanset
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_spanset") 0 0 0
245 if $set2->can( 'as_spanset' );
246 0 ▶ $set2 = $set2->as_set
Branch: 1 branch
BranchTrueFalseTotal
if $set2->can("as_set") 0 0 0
247 if $set2->can( 'as_set' );
248 0 ▶ $set2 = DateTime::Set->from_datetimes( dates => [ $set2 ] )
Branch: 1 branch
BranchTrueFalseTotal
unless $set2->can("union") 0 0 0
249 unless $set2->can( 'union' );
250 0 $tmp->{set} = $set1->{set}->complement( $set2->{set} );
251 }
252 else {
253 0 $tmp->{set} = $set1->{set}->complement;
254 }
255  
256 # complement() can generate something more complex than a span.
257 0 bless $tmp, 'DateTime::SpanSet';
258  
259 # # We have to check it's internal structure to find out.
260 # if ( $#{ $tmp->{set}->{list} } != 0 ) {
261 # bless $tmp, 'Date::SpanSet';
262 # }
263  
264 0 return $tmp;
265 }
266  
267 53 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
268 53 return DateTime::Set::_fix_datetime( $_[0]->{set}->min );
269 }
270  
271 23 *min = \&start;
272  
273 53 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
274 53 return DateTime::Set::_fix_datetime( $_[0]->{set}->max );
275 }
276  
277 23 *max = \&end;
278  
279 3 ▶sub start_is_open {
Subroutine start_is_open: called CC=1 SCAR=0.0
Pod documented
280 # min_a returns info about the set boundary
281 3 my ($min, $open) = $_[0]->{set}->min_a;
282 3 return $open;
283 }
284  
285 0 ▶sub start_is_closed { $_[0]->start_is_open ? 0 : 1 }
Subroutine start_is_closed: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->start_is_open ? : 0 0 0
286  
287 3 ▶sub end_is_open {
Subroutine end_is_open: called CC=1 SCAR=0.0
Pod documented
288 # max_a returns info about the set boundary
289 3 my ($max, $open) = $_[0]->{set}->max_a;
290 3 return $open;
291 }
292  
293 0 ▶sub end_is_closed { $_[0]->end_is_open ? 0 : 1 }
Subroutine end_is_closed: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->end_is_open ? : 0 0 0
294  
295  
296 # span == $self
297 0 ▶sub span { @_ }
Subroutine span: not called CC=1 SCAR=6.9
Pod undocumented
298  
299 0 ▶sub duration {
Subroutine duration: not called CC=2 SCAR=17.9
Pod documented
300 0 my $dur;
301  
302 0 local $@;
303 0 eval {
304 0 local $SIG{__DIE__}; # don't want to trap this (rt ticket 5434)
305 0 $dur = $_[0]->end->subtract_datetime_absolute( $_[0]->start )
306 };
307
308 0 ▶ return $dur if defined $dur;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dur 0 0 0
309  
310 0 return DateTime::Infinite::Future->new -
311 DateTime::Infinite::Past->new;
312 }
313 23 *size = \&duration;
314  
315 23 1;
316  
317 __END__
« DateTime/SpanSet.pm ↑ Summary DateTime/Set.pm »