blib/lib/DateTime/Duration.pm

filter: Statementstmt 98.8% 167 / 169 Branchbran 91.9% 57 / 62 Conditioncond 80.0% 12 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 51 / 51 Podpod 100% 33 / 33 totaltotal 95.8% 324 / 338 CC 40 SCAR 11.4
CC
40
Cov
96%
in_units
10.0
_normalize_nanoseconds
5.0
_duration_object_from_args
4.1
CRAP 3.1
« blib/lib/DateTime.pm ↑ Summary blib/lib/DateTime/Helpers.pm »
1 package DateTime::Duration;
2  
3 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 49 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 49 our $VERSION = '1.68';
8  
9 49 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 49 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 49 ▶use DateTime::Helpers;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 49 ▶use DateTime::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 49 ▶use Params::ValidationCompiler 0.26 qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 49 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 49 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
17 49 fallback => 1,
18 '+' => '_add_overload',
19 '-' => '_subtract_overload',
20 '*' => '_multiply_overload',
21 '<=>' => '_compare_overload',
22 'cmp' => '_compare_overload',
23 49 );
24  
25 sub MAX_NANOSECONDS () {1_000_000_000} # 1E9 = almost 32 bits
26  
27 49 my @all_units = qw( months days minutes seconds nanoseconds );
28  
29 {
30 my %units = map {
31 49 $_ => {
32  
33 # XXX - what we really want is to accept an integer, Inf, -Inf,
34 # and NaN, but I can't figure out how to accept NaN since it never
35 # compares to anything.
36 type => t('Defined'),
37 default => 0,
38 }
39 } qw(
40 years
41 months
42 weeks
43 days
44 hours
45 minutes
46 seconds
47 nanoseconds
48 );
49  
50 49 my $check = validation_for(
51 name => '_check_new_params',
52 name_is_optional => 1,
53 params => {
54 %units,
55 end_of_month => {
56 type => t('EndOfMonthMode'),
57 optional => 1,
58 },
59 },
60 );
61  
62 32006 ▶ sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
63 32006 my $class = shift;
64 32006 my %p = $check->(@_);
65  
66 32006 my $self = bless {}, $class;
67  
68 32006 $self->{months} = ( $p{years} * 12 ) + $p{months};
69  
70 32006 $self->{days} = ( $p{weeks} * 7 ) + $p{days};
71  
72 32006 $self->{minutes} = ( $p{hours} * 60 ) + $p{minutes};
73  
74 32006 $self->{seconds} = $p{seconds};
75  
76 32006 ▶ if ( $p{nanoseconds} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'nanoseconds'}) { } 39 31967 32006
77 39 $self->{nanoseconds} = $p{nanoseconds};
78 39 $self->_normalize_nanoseconds;
79 }
80 else {
81  
82 # shortcut - if they don't need nanoseconds
83 31967 $self->{nanoseconds} = 0;
84 }
85  
86 $self->{end_of_month} = (
87 defined $p{end_of_month} ? $p{end_of_month}
88 32006 ▶ : $self->{months} < 0 ? 'preserve'
Branch: 2 branches
BranchTrueFalseTotal
defined $p{'end_of_month'} ? : 16 31990 32006
$self->{'months'} < 0 ? : 117 31873 31990
89 : 'wrap'
90 );
91  
92 32006 return $self;
93 }
94 }
95  
96 # make the signs of seconds, nanos the same; 0 < abs(nanos) < MAX_NANOS
97 # NB this requires nanoseconds != 0 (callers check this already)
98 49 ▶sub _normalize_nanoseconds {
Subroutine _normalize_nanoseconds: called CC=5 SCAR=16.1
99 48 my $self = shift;
100  
101 return
102 if ( $self->{nanoseconds} == DateTime::INFINITY()
103 || $self->{nanoseconds} == DateTime::NEG_INFINITY()
104 48 ▶ || $self->{nanoseconds} eq DateTime::NAN() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nanoseconds"} == &DateTime::INFINITY() or $self->{"nanoseconds"} == &DateTime::NEG_INFINITY() or $self->{"nanoseconds"} eq &DateTime::NAN() 3 45 48
Condition100%6/6
$self->{"nanoseconds"} == &DateTime::INFINITY() or $self->{"nanoseconds"} == &DateTime::NEG_INFINITY() or $self->{"nanoseconds"} eq &DateTime::NAN()
l!l&&r!l&&!r
2145
$self->{"nanoseconds"} == &DateTime::INFINITY() or $self->{"nanoseconds"} == &DateTime::NEG_INFINITY()
l!l&&r!l&&!r
1146
MC/DC100%3/3
$self->{"nanoseconds"} == &DateTime::INFINITY() or $self->{"nanoseconds"} == &DateTime::NEG_INFINITY() or $self->{"nanoseconds"} eq &DateTime::NAN()
$self->{"nanoseconds"} == &DateTime::INFINITY() $self->{"nanoseconds"} == &DateTime::NEG_INFINITY() $self->{"nanoseconds"} eq &DateTime::NAN()
Truth table100%4/4
A or B or C
A= $self->{"nanoseconds"} == &DateTime::INFINITY()B= $self->{"nanoseconds"} == &DateTime::NEG_INFINITY()C= $self->{"nanoseconds"} eq &DateTime::NAN()
ABCresult
1XX1
01X1
0011
0000
105  
106 45 my $seconds = $self->{seconds} + $self->{nanoseconds} / MAX_NANOSECONDS;
107 45 $self->{seconds} = int($seconds);
108 45 $self->{nanoseconds} = $self->{nanoseconds} % MAX_NANOSECONDS;
109 45 ▶ $self->{nanoseconds} -= MAX_NANOSECONDS if $seconds < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $seconds < 0 13 32 45
110 }
111  
112 12 ▶sub clone { bless { %{ $_[0] } }, ref $_[0] }
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
113  
114 4 ▶sub years { abs( $_[0]->in_units('years') ) }
Subroutine years: called CC=1 SCAR=0.0
Pod documented
115 4 ▶sub months { abs( $_[0]->in_units( 'months', 'years' ) ) }
Subroutine months: called CC=1 SCAR=0.0
Pod documented
116 4 ▶sub weeks { abs( $_[0]->in_units('weeks') ) }
Subroutine weeks: called CC=1 SCAR=0.0
Pod documented
117 4 ▶sub days { abs( $_[0]->in_units( 'days', 'weeks' ) ) }
Subroutine days: called CC=1 SCAR=0.0
Pod documented
118 10 ▶sub hours { abs( $_[0]->in_units('hours') ) }
Subroutine hours: called CC=1 SCAR=0.0
Pod documented
119 7 ▶sub minutes { abs( $_[0]->in_units( 'minutes', 'hours' ) ) }
Subroutine minutes: called CC=1 SCAR=0.0
Pod documented
120 7 ▶sub seconds { abs( $_[0]->in_units('seconds') ) }
Subroutine seconds: called CC=1 SCAR=0.0
Pod documented
121 4 ▶sub nanoseconds { abs( $_[0]->in_units( 'nanoseconds', 'seconds' ) ) }
Subroutine nanoseconds: called CC=1 SCAR=0.0
Pod documented
122  
123 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
124 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
125  
126 18 ▶sub _has_positive {
Subroutine _has_positive: called CC=2 SCAR=6.9
127 18 ▶ ( grep { $_ > 0 } @{ $_[0] }{@all_units} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ > 0;} @{$_[0];}{@all_units}) ? : 7 11 18
128 }
129  
130 13 ▶sub _has_negative {
Subroutine _has_negative: called CC=2 SCAR=6.9
131 13 ▶ ( grep { $_ < 0 } @{ $_[0] }{@all_units} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ < 0;} @{$_[0];}{@all_units}) ? : 7 6 13
132 }
133  
134 32678 ▶sub is_zero {
Subroutine is_zero: called CC=2 SCAR=6.9
Pod documented
135 32678 ▶ return 0 if grep { $_ != 0 } @{ $_[0] }{@all_units};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ != 0;} @{$_[0];}{@all_units} 31931 747 32678
136 747 return 1;
137 }
138  
139 30 ▶sub delta_months { $_[0]->{months} }
Subroutine delta_months: called CC=1 SCAR=0.0
Pod documented
140 36 ▶sub delta_days { $_[0]->{days} }
Subroutine delta_days: called CC=1 SCAR=0.0
Pod documented
141 29 ▶sub delta_minutes { $_[0]->{minutes} }
Subroutine delta_minutes: called CC=1 SCAR=0.0
Pod documented
142 47 ▶sub delta_seconds { $_[0]->{seconds} }
Subroutine delta_seconds: called CC=1 SCAR=0.0
Pod documented
143 33 ▶sub delta_nanoseconds { $_[0]->{nanoseconds} }
Subroutine delta_nanoseconds: called CC=1 SCAR=0.0
Pod documented
144  
145 63873 ▶sub deltas {
Subroutine deltas: called CC=1 SCAR=0.0
Pod documented
146 63873 map { $_ => $_[0]->{$_} } @all_units;
147 }
148  
149 64 ▶sub in_units {
Subroutine in_units: called CC=10 SCAR=23.0
Pod documented
150 64 my $self = shift;
151 64 my @units = @_;
152  
153 64 my %units = map { $_ => 1 } @units;
154  
155 64 my %ret;
156  
157 my ( $months, $days, $minutes, $seconds )
158 64 = @{$self}{qw( months days minutes seconds )};
159  
160 64 ▶ if ( $units{years} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"years"}) 12 52 64
161 12 $ret{years} = int( $months / 12 );
162 12 $months -= $ret{years} * 12;
163 }
164  
165 64 ▶ if ( $units{months} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"months"}) 8 56 64
166 8 $ret{months} = $months;
167 }
168  
169 64 ▶ if ( $units{weeks} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"weeks"}) 11 53 64
170 11 $ret{weeks} = int( $days / 7 );
171 11 $days -= $ret{weeks} * 7;
172 }
173  
174 64 ▶ if ( $units{days} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"days"}) 8 56 64
175 8 $ret{days} = $days;
176 }
177  
178 64 ▶ if ( $units{hours} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"hours"}) 21 43 64
179 21 $ret{hours} = int( $minutes / 60 );
180 21 $minutes -= $ret{hours} * 60;
181 }
182  
183 64 ▶ if ( $units{minutes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"minutes"}) 11 53 64
184 11 $ret{minutes} = $minutes;
185 }
186  
187 64 ▶ if ( $units{seconds} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"seconds"}) 16 48 64
188 16 $ret{seconds} = $seconds;
189 16 $seconds = 0;
190 }
191  
192 64 ▶ if ( $units{nanoseconds} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($units{"nanoseconds"}) 8 56 64
193 8 $ret{nanoseconds} = $seconds * MAX_NANOSECONDS + $self->{nanoseconds};
194 }
195  
196 64 ▶ wantarray ? @ret{@units} : $ret{ $units[0] };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 62 64
197 }
198  
199 541 ▶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' ? : 426 115 541
200 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
201 1081 ▶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' ? : 227 854 1081
202  
203 2 ▶sub end_of_month_mode { $_[0]->{end_of_month} }
Subroutine end_of_month_mode: called CC=1 SCAR=0.0
Pod documented
204  
205 3 ▶sub calendar_duration {
Subroutine calendar_duration: called CC=1 SCAR=0.0
Pod documented
206 3 my $self = shift;
207  
208 return ( ref $self )
209 3 ->new( map { $_ => $self->{$_} } qw( months days end_of_month ) );
210 }
211  
212 3 ▶sub clock_duration {
Subroutine clock_duration: called CC=1 SCAR=0.0
Pod documented
213 3 my $self = shift;
214  
215 return ( ref $self )
216 3 ->new( map { $_ => $self->{$_} }
217 qw( minutes seconds nanoseconds end_of_month ) );
218 }
219  
220 22872 ▶sub inverse {
Subroutine inverse: called CC=4 SCAR=13.9
Pod documented
221 22872 my $self = shift;
222 22872 my %p = @_;
223  
224 22872 my %new;
225 22872 foreach my $u (@all_units) {
226 114360 $new{$u} = $self->{$u};
227  
228 # avoid -0 bug
229 114360 ▶ $new{$u} *= -1 if $new{$u};
Branch: 1 branch
BranchTrueFalseTotal
if $new{$u} 22888 91472 114360
230 }
231  
232 $new{end_of_month} = $p{end_of_month}
233 22872 ▶ if exists $p{end_of_month};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"end_of_month"} 2 22870 22872
234  
235 22872 return ( ref $self )->new(%new);
236 }
237  
238 17 ▶sub add_duration {
Subroutine add_duration: called CC=3 SCAR=11.0
Pod documented
239 17 my ( $self, $dur ) = @_;
240  
241 17 foreach my $u (@all_units) {
242 85 $self->{$u} += $dur->{$u};
243 }
244  
245 17 ▶ $self->_normalize_nanoseconds if $self->{nanoseconds};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nanoseconds"} 8 9 17
246  
247 17 return $self;
248 }
249  
250 5 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
251 5 my $self = shift;
252  
253 5 return $self->add_duration( $self->_duration_object_from_args(@_) );
254 }
255  
256 5 ▶sub subtract {
Subroutine subtract: called CC=1 SCAR=0.0
Pod documented
257 5 my $self = shift;
258  
259 5 return $self->subtract_duration( $self->_duration_object_from_args(@_) );
260 }
261  
262 # Syntactic sugar for add and subtract: use a duration object if it's
263 # supplied, otherwise build a new one from the arguments.
264 10 ▶sub _duration_object_from_args {
Subroutine _duration_object_from_args: called CC=4 SCAR=14.1
265 10 my $self = shift;
266  
267 10 ▶ return $_[0]
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) 2 8 10
Condition66%4/6
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
802
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
802
MC/DC33%1/3
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
@_ == 1 builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table50%2/4
A and B and C
A= @_ == 1B= builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)
ABCresult
0XX0
10X0
1100
1111
268 if @_ == 1 && blessed( $_[0] ) && $_[0]->isa(__PACKAGE__);
269  
270 8 return __PACKAGE__->new(@_);
271 }
272  
273 11 ▶sub subtract_duration { return $_[0]->add_duration( $_[1]->inverse ) }
Subroutine subtract_duration: called CC=1 SCAR=0.0
Pod documented
274  
275 {
276 49 my $check = validation_for(
277 name => '_check_multiply_params',
278 name_is_optional => 1,
279 params => [
280 { type => t('Int') },
281 ],
282 );
283  
284 4 ▶ sub multiply {
Subroutine multiply: called CC=3 SCAR=11.0
Pod documented
285 4 my $self = shift;
286 4 my ($multiplier) = $check->(@_);
287  
288 3 foreach my $u (@all_units) {
289 15 $self->{$u} *= $multiplier;
290 }
291  
292 3 ▶ $self->_normalize_nanoseconds if $self->{nanoseconds};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nanoseconds"} 1 2 3
293  
294 3 return $self;
295 }
296 }
297  
298 49 ▶sub compare {
Subroutine compare: called CC=2 SCAR=7.0
Pod documented
299 5 my ( undef, $dur1, $dur2, $dt ) = @_;
300  
301 5 ▶ $dt ||= DateTime->now;
Condition66%2/3
$dt ||= "DateTime"->now
l!l&&r!l&&!r
410
MC/DC0%0/2
$dt ||= "DateTime"->now
$dt "DateTime"->now
Truth table66%2/3
A ||= B
A= $dtB= "DateTime"->now
ABresult
1X1
011
000
302  
303 5 return DateTime->compare(
304 $dt->clone->add_duration($dur1),
305 $dt->clone->add_duration($dur2)
306 );
307 }
308  
309 1 ▶sub _add_overload {
Subroutine _add_overload: called CC=3 SCAR=12.7
310 1 my ( $d1, $d2, $rev ) = @_;
311  
312 1 ▶ ( $d1, $d2 ) = ( $d2, $d1 ) if $rev;
Branch: 1 branch
BranchTrueFalseTotal
if $rev 0 1 1
313  
314 1 ▶ if ( DateTime::Helpers::isa( $d2, 'DateTime' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (DateTime::Helpers::isa($d2, "DateTime")) 0 1 1
315 0 $d2->add_duration($d1);
316 0 return;
317 }
318  
319 # will also work if $d1 is a DateTime.pm object
320 1 return $d1->clone->add_duration($d2);
321 }
322  
323 6 ▶sub _subtract_overload {
Subroutine _subtract_overload: called CC=3 SCAR=11.4
324 6 my ( $d1, $d2, $rev ) = @_;
325  
326 6 ▶ ( $d1, $d2 ) = ( $d2, $d1 ) if $rev;
Branch: 1 branch
BranchTrueFalseTotal
if $rev 0 6 6
327  
328 6 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
if DateTime::Helpers::isa($d2, "DateTime") 0 6 6
329 'Cannot subtract a DateTime object from a DateTime::Duration object')
330 if DateTime::Helpers::isa( $d2, 'DateTime' );
331  
332 6 return $d1->clone->subtract_duration($d2);
333 }
334  
335 3 ▶sub _multiply_overload {
Subroutine _multiply_overload: called CC=1 SCAR=0.0
336 3 my $self = shift;
337  
338 3 my $new = $self->clone;
339  
340 3 return $new->multiply(shift);
341 }
342  
343 1 ▶sub _compare_overload {
Subroutine _compare_overload: called CC=1 SCAR=0.0
344 1 Carp::croak( 'DateTime::Duration does not overload comparison.'
345 . ' See the documentation on the compare() method for details.'
346 );
347 }
348  
349 49 1;
350  
351 # ABSTRACT: Duration objects for date math
352  
353 __END__
« blib/lib/DateTime.pm ↑ Summary blib/lib/DateTime/Helpers.pm »