DateTimeX/Lite/Duration.pm

filter: Statementstmt 97.9% 143 / 146 Branchbran 91.6% 55 / 60 Conditioncond 66.6% 10 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 97.8% 45 / 46 Podpod 100% 33 / 33 totaltotal 93.5% 289 / 309 CC 40 SCAR 12.5
CC
40
Cov
94%
in_units
10.0
new
6.0
_add_overload
5.6
CRAP 3.5
« DateTimeX/Lite/Locale.pm ↑ Summary DateTimeX/Lite/Strftime.pm »
1  
2 package DateTimeX::Lite::Duration;
3 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 56 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 56 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 56 ▶use overload ( fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
10 '+' => '_add_overload',
11 '-' => '_subtract_overload',
12 '*' => '_multiply_overload',
13 '<=>' => '_compare_overload',
14 'cmp' => '_compare_overload',
15 56 );
16  
17 56 ▶use constant MAX_NANOSECONDS => 1_000_000_000; # 1E9 = almost 32 bits
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 56 my @all_units = qw( months days minutes seconds nanoseconds );
20  
21 # XXX - need to reject non-integers but accept infinity, NaN, &
22 # 1.56e+18
23 sub new
24 4729 ▶{
Subroutine new: called CC=6 SCAR=17.9
Pod documented
25 4729 my ($class, %p) = @_;
26 4729 foreach my $field (qw(years months weeks days hours minutes seconds nanoseconds)) {
27 37832 ▶ $p{$field} = 0 unless defined $p{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p{$field} 27151 10681 37832
28 }
29
30 =head1
31 my %p = validate( @_,
32 { years => { type => SCALAR, default => 0 },
33 months => { type => SCALAR, default => 0 },
34 weeks => { type => SCALAR, default => 0 },
35 days => { type => SCALAR, default => 0 },
36 hours => { type => SCALAR, default => 0 },
37 minutes => { type => SCALAR, default => 0 },
38 seconds => { type => SCALAR, default => 0 },
39 nanoseconds => { type => SCALAR, default => 0 },
40 end_of_month => { type => SCALAR, default => undef,
41 regex => qr/^(?:wrap|limit|preserve)$/ },
42 } );
43 =cut
44  
45 4729 my $self = bless {}, $class;
46  
47 4729 $self->{months} = ( $p{years} * 12 ) + $p{months};
48  
49 4729 $self->{days} = ( $p{weeks} * 7 ) + $p{days};
50  
51 4729 $self->{minutes} = ( $p{hours} * 60 ) + $p{minutes};
52  
53 4729 $self->{seconds} = $p{seconds};
54  
55 4729 ▶ if ( $p{nanoseconds} )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'nanoseconds'}) { } 38 4691 4729
56 {
57 38 $self->{nanoseconds} = $p{nanoseconds};
58 38 $self->_normalize_nanoseconds;
59 }
60 else
61 {
62 # shortcut - if they don't need nanoseconds
63 4691 $self->{nanoseconds} = 0;
64 }
65  
66 $self->{end_of_month} =
67 ( defined $p{end_of_month}
68 ? $p{end_of_month}
69 4729 ▶ : $self->{months} < 0
Branch: 2 branches
BranchTrueFalseTotal
defined $p{'end_of_month'} ? : 13 4716 4729
$self->{'months'} < 0 ? : 116 4600 4716
70 ? 'preserve'
71 : 'wrap'
72 );
73  
74 4729 return $self;
75 }
76  
77 # make the signs of seconds, nanos the same; 0 < abs(nanos) < MAX_NANOS
78 # NB this requires nanoseconds != 0 (callers check this already)
79 sub _normalize_nanoseconds
80 47 ▶{
Subroutine _normalize_nanoseconds: called CC=5 SCAR=16.1
81 47 my $self = shift;
82  
83 return if
84 ( $self->{nanoseconds} == DateTimeX::Lite::INFINITY()
85 || $self->{nanoseconds} == DateTimeX::Lite::NEG_INFINITY()
86 47 ▶ || $self->{nanoseconds} eq DateTimeX::Lite::NAN()
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nanoseconds"} == "Inf" or $self->{"nanoseconds"} == "-Inf" or $self->{"nanoseconds"} eq "NaN" 3 44 47
Condition100%6/6
$self->{"nanoseconds"} == "Inf" or $self->{"nanoseconds"} == "-Inf" or $self->{"nanoseconds"} eq "NaN"
l!l&&r!l&&!r
2144
$self->{"nanoseconds"} == "Inf" or $self->{"nanoseconds"} == "-Inf"
l!l&&r!l&&!r
1145
MC/DC100%3/3
$self->{"nanoseconds"} == "Inf" or $self->{"nanoseconds"} == "-Inf" or $self->{"nanoseconds"} eq "NaN"
$self->{"nanoseconds"} == "Inf" $self->{"nanoseconds"} == "-Inf" $self->{"nanoseconds"} eq "NaN"
Truth table100%4/4
A or B or C
A= $self->{"nanoseconds"} == "Inf"B= $self->{"nanoseconds"} == "-Inf"C= $self->{"nanoseconds"} eq "NaN"
ABCresult
1XX1
01X1
0011
0000
87 );
88  
89 44 my $seconds = $self->{seconds} + $self->{nanoseconds} / MAX_NANOSECONDS;
90 44 $self->{seconds} = int( $seconds );
91 44 $self->{nanoseconds} = $self->{nanoseconds} % MAX_NANOSECONDS;
92 44 ▶ $self->{nanoseconds} -= MAX_NANOSECONDS if $seconds < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $seconds < 0 13 31 44
93 }
94  
95 11 ▶sub clone { bless { %{ $_[0] } }, ref $_[0] }
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
96  
97 4 ▶sub years { abs( $_[0]->in_units( 'years' ) ) }
Subroutine years: called CC=1 SCAR=0.0
Pod documented
98 4 ▶sub months { abs( $_[0]->in_units( 'months', 'years' ) ) }
Subroutine months: called CC=1 SCAR=0.0
Pod documented
99 4 ▶sub weeks { abs( $_[0]->in_units( 'weeks' ) ) }
Subroutine weeks: called CC=1 SCAR=0.0
Pod documented
100 4 ▶sub days { abs( $_[0]->in_units( 'days', 'weeks' ) ) }
Subroutine days: called CC=1 SCAR=0.0
Pod documented
101 10 ▶sub hours { abs( $_[0]->in_units( 'hours' ) ) }
Subroutine hours: called CC=1 SCAR=0.0
Pod documented
102 7 ▶sub minutes { abs( $_[0]->in_units( 'minutes', 'hours' ) ) }
Subroutine minutes: called CC=1 SCAR=0.0
Pod documented
103 7 ▶sub seconds { abs( $_[0]->in_units( 'seconds' ) ) }
Subroutine seconds: called CC=1 SCAR=0.0
Pod documented
104 4 ▶sub nanoseconds { abs( $_[0]->in_units( 'nanoseconds', 'seconds' ) ) }
Subroutine nanoseconds: called CC=1 SCAR=0.0
Pod documented
105  
106 8 ▶sub is_positive { $_[0]->_has_positive && ! $_[0]->_has_negative }
Subroutine is_positive: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_has_positive and not $_[0]->_has_negative 5 3 8
107 10 ▶sub is_negative { ! $_[0]->_has_positive && $_[0]->_has_negative }
Subroutine is_negative: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
not $_[0]->_has_positive and $_[0]->_has_negative 8 2 10
108  
109 18 ▶sub _has_positive { ( grep { $_ > 0 } @{ $_[0] }{@all_units} ) ? 1 : 0}
Subroutine _has_positive: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ > 0;} @{$_[0];}{@all_units}) ? : 7 11 18
110 13 ▶sub _has_negative { ( grep { $_ < 0 } @{ $_[0] }{@all_units} ) ? 1 : 0 }
Subroutine _has_negative: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ < 0;} @{$_[0];}{@all_units}) ? : 7 6 13
111  
112 6966 ▶sub is_zero { return 0 if grep { $_ != 0 } @{ $_[0] }{@all_units};
Subroutine is_zero: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ != 0;} @{$_[0];}{@all_units} 6963 3 6966
113 3 return 1 }
114  
115 30 ▶sub delta_months { $_[0]->{months} }
Subroutine delta_months: called CC=1 SCAR=0.0
Pod documented
116 36 ▶sub delta_days { $_[0]->{days} }
Subroutine delta_days: called CC=1 SCAR=0.0
Pod documented
117 26 ▶sub delta_minutes { $_[0]->{minutes} }
Subroutine delta_minutes: called CC=1 SCAR=0.0
Pod documented
118 45 ▶sub delta_seconds { $_[0]->{seconds} }
Subroutine delta_seconds: called CC=1 SCAR=0.0
Pod documented
119 33 ▶sub delta_nanoseconds { $_[0]->{nanoseconds} }
Subroutine delta_nanoseconds: called CC=1 SCAR=0.0
Pod documented
120  
121 sub deltas
122 6983 ▶{
Subroutine deltas: called CC=1 SCAR=0.0
Pod documented
123 6983 map { $_ => $_[0]->{$_} } @all_units;
124 }
125  
126 sub in_units
127 64 ▶{
Subroutine in_units: called CC=10 SCAR=23.0
Pod documented
128 64 my $self = shift;
129 64 my @units = @_;
130  
131 64 my %units = map { $_ => 1 } @units;
132  
133 64 my %ret;
134  
135 my ( $months, $days, $minutes, $seconds ) =
136 64 @{ $self }{qw( months days minutes seconds )};
137  
138 64 ▶ if ( $units{years} )
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"years"}) 12 52 64
139 {
140 12 $ret{years} = int( $months / 12 );
141 12 $months -= $ret{years} * 12;
142 }
143  
144 64 ▶ if ( $units{months} )
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"months"}) 8 56 64
145 {
146 8 $ret{months} = $months;
147 }
148  
149 64 ▶ if ( $units{weeks} )
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"weeks"}) 11 53 64
150 {
151 11 $ret{weeks} = int( $days / 7 );
152 11 $days -= $ret{weeks} * 7;
153 }
154  
155 64 ▶ if ( $units{days} )
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"days"}) 8 56 64
156 {
157 8 $ret{days} = $days;
158 }
159  
160 64 ▶ if ( $units{hours} )
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"hours"}) 21 43 64
161 {
162 21 $ret{hours} = int( $minutes / 60 );
163 21 $minutes -= $ret{hours} * 60;
164 }
165  
166 64 ▶ if ( $units{minutes} )
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"minutes"}) 11 53 64
167 {
168 11 $ret{minutes} = $minutes
169 }
170  
171 64 ▶ if ( $units{seconds} )
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"seconds"}) 16 48 64
172 {
173 16 $ret{seconds} = $seconds;
174 16 $seconds = 0;
175 }
176  
177 64 ▶ if ( $units{nanoseconds} )
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"nanoseconds"}) 8 56 64
178 {
179 8 $ret{nanoseconds} = $seconds * MAX_NANOSECONDS + $self->{nanoseconds};
180 }
181  
182 64 ▶ wantarray ? @ret{@units} : $ret{ $units[0] };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 62 64
183 }
184  
185 576 ▶sub is_wrap_mode { $_[0]->{end_of_month} eq 'wrap' ? 1 : 0 }
Subroutine is_wrap_mode: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'end_of_month'} eq 'wrap' ? : 444 132 576
186 3 ▶sub is_limit_mode { $_[0]->{end_of_month} eq 'limit' ? 1 : 0 }
Subroutine is_limit_mode: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'end_of_month'} eq 'limit' ? : 3 0 3
187 1151 ▶sub is_preserve_mode { $_[0]->{end_of_month} eq 'preserve' ? 1 : 0 }
Subroutine is_preserve_mode: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'end_of_month'} eq 'preserve' ? : 263 888 1151
188  
189 0 ▶sub end_of_month_mode { $_[0]->{end_of_month} }
Subroutine end_of_month_mode: not called CC=1 SCAR=6.9
Pod documented
190  
191 sub calendar_duration
192 3 ▶{
Subroutine calendar_duration: called CC=1 SCAR=0.0
Pod documented
193 3 my $self = shift;
194  
195 return
196 3 (ref $self)->new( map { $_ => $self->{$_} } qw( months days end_of_month ) )
197 }
198  
199 sub clock_duration
200 3 ▶{
Subroutine clock_duration: called CC=1 SCAR=0.0
Pod documented
201 3 my $self = shift;
202  
203 return
204 3 (ref $self)->new( map { $_ => $self->{$_} } qw( minutes seconds nanoseconds end_of_month ) )
205 }
206  
207 sub inverse
208 1418 ▶{
Subroutine inverse: called CC=3 SCAR=11.0
Pod documented
209 1418 my $self = shift;
210  
211 1418 my %new;
212 1418 foreach my $u (@all_units)
213 {
214 7090 $new{$u} = $self->{$u};
215 # avoid -0 bug
216 7090 ▶ $new{$u} *= -1 if $new{$u};
Branch: 1 branch
BranchTrueFalseTotal
if $new{$u} 1433 5657 7090
217 }
218  
219 1418 return (ref $self)->new(%new);
220 }
221  
222 sub add_duration
223 15 ▶{
Subroutine add_duration: called CC=3 SCAR=11.0
Pod documented
224 15 my ( $self, $dur ) = @_;
225  
226 15 foreach my $u (@all_units)
227 {
228 75 $self->{$u} += $dur->{$u};
229 }
230  
231 15 ▶ $self->_normalize_nanoseconds if $self->{nanoseconds};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nanoseconds"} 8 7 15
232  
233 15 return $self;
234 }
235  
236 sub add
237 4 ▶{
Subroutine add: called CC=1 SCAR=0.0
Pod documented
238 4 my $self = shift;
239  
240 4 return $self->add_duration( (ref $self)->new(@_) );
241 }
242  
243 10 ▶sub subtract_duration { return $_[0]->add_duration( $_[1]->inverse ) }
Subroutine subtract_duration: called CC=1 SCAR=0.0
Pod documented
244  
245 sub subtract
246 4 ▶{
Subroutine subtract: called CC=1 SCAR=0.0
Pod documented
247 4 my $self = shift;
248  
249 4 return $self->subtract_duration( (ref $self)->new(@_) )
250 }
251  
252 sub multiply
253 3 ▶{
Subroutine multiply: called CC=3 SCAR=11.0
Pod documented
254 3 my $self = shift;
255 3 my $multiplier = shift;
256  
257 3 foreach my $u (@all_units)
258 {
259 15 $self->{$u} *= $multiplier;
260 }
261  
262 3 ▶ $self->_normalize_nanoseconds if $self->{nanoseconds};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nanoseconds"} 1 2 3
263  
264 3 return $self;
265 }
266  
267 sub compare
268 5 ▶{
Subroutine compare: called CC=2 SCAR=7.0
Pod documented
269 5 my ( $class, $dur1, $dur2, $dt ) = @_;
270  
271 5 ▶ $dt ||= DateTimeX::Lite->now;
Condition66%2/3
$dt ||= "DateTimeX::Lite"->now
l!l&&r!l&&!r
410
MC/DC0%0/2
$dt ||= "DateTimeX::Lite"->now
$dt "DateTimeX::Lite"->now
Truth table66%2/3
A ||= B
A= $dtB= "DateTimeX::Lite"->now
ABresult
1X1
011
000
272  
273 return
274 5 DateTimeX::Lite->compare( $dt->clone->add_duration($dur1), $dt->clone->add_duration($dur2) );
275 }
276  
277 sub _add_overload
278 1 ▶{
Subroutine _add_overload: called CC=4 SCAR=17.2
279 1 my ( $d1, $d2, $rev ) = @_;
280  
281 1 ▶ ($d1, $d2) = ($d2, $d1) if $rev;
Branch: 1 branch
BranchTrueFalseTotal
if $rev 0 1 1
282  
283 1 ▶ if ( blessed $d2 && $d2->isa( 'DateTimeX::Lite' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($d2) and $d2->isa("DateTimeX::Lite")) 0 1 1
Condition33%1/3
builtin::blessed($d2) and $d2->isa("DateTimeX::Lite")
!ll&&!rl&&r
010
MC/DC0%0/2
builtin::blessed($d2) and $d2->isa("DateTimeX::Lite")
builtin::blessed($d2) $d2->isa("DateTimeX::Lite")
Truth table33%1/3
A and B
A= builtin::blessed($d2)B= $d2->isa("DateTimeX::Lite")
ABresult
0X0
100
111
284 {
285 0 $d2->add_duration($d1);
286 0 return;
287 }
288  
289 # will also work if $d1 is a DateTimeX::Lite.pm object
290 1 return $d1->clone->add_duration($d2);
291 }
292  
293 sub _subtract_overload
294 6 ▶{
Subroutine _subtract_overload: called CC=4 SCAR=15.6
295 6 my ( $d1, $d2, $rev ) = @_;
296  
297 6 ▶ ($d1, $d2) = ($d2, $d1) if $rev;
Branch: 1 branch
BranchTrueFalseTotal
if $rev 0 6 6
298  
299 6 ▶ Carp::croak( "Cannot subtract a DateTimeX::Lite object from a DateTimeX::Lite::Duration object" )
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($d2) and $d2->isa("DateTimeX::Lite") 0 6 6
Condition33%1/3
builtin::blessed($d2) and $d2->isa("DateTimeX::Lite")
!ll&&!rl&&r
060
MC/DC0%0/2
builtin::blessed($d2) and $d2->isa("DateTimeX::Lite")
builtin::blessed($d2) $d2->isa("DateTimeX::Lite")
Truth table33%1/3
A and B
A= builtin::blessed($d2)B= $d2->isa("DateTimeX::Lite")
ABresult
0X0
100
111
300 if blessed $d2 && $d2->isa( 'DateTimeX::Lite' );
301  
302 6 return $d1->clone->subtract_duration($d2);
303 }
304  
305 sub _multiply_overload
306 2 ▶{
Subroutine _multiply_overload: called CC=1 SCAR=0.0
307 2 my $self = shift;
308  
309 2 my $new = $self->clone;
310  
311 2 return $new->multiply(@_);
312 }
313  
314 sub _compare_overload
315 1 ▶{
Subroutine _compare_overload: called CC=1 SCAR=0.0
316 1 Carp::croak( 'DateTimeX::Lite::Duration does not overload comparison.'
317 . ' See the documentation on the compare() method for details.' );
318 }
319  
320  
321 56 1;
322  
323 __END__
« DateTimeX/Lite/Locale.pm ↑ Summary DateTimeX/Lite/Strftime.pm »