| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$self->{"nanoseconds"} == &DateTime::INFINITY() or $self->{"nanoseconds"} == &DateTime::NEG_INFINITY() or $self->{"nanoseconds"} eq &DateTime::NAN()
$self->{"nanoseconds"} == &DateTime::INFINITY() or $self->{"nanoseconds"} == &DateTime::NEG_INFINITY()
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()
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 12 | $ret{years} = int( $months / 12 ); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 12 | $months -= $ret{years} * 12; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 64 | ▶ | if ( $units{months} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 8 | $ret{months} = $months; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 64 | ▶ | if ( $units{weeks} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 11 | $ret{weeks} = int( $days / 7 ); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 11 | $days -= $ret{weeks} * 7; | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 64 | ▶ | if ( $units{days} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 8 | $ret{days} = $days; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 64 | ▶ | if ( $units{hours} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 21 | $ret{hours} = int( $minutes / 60 ); | |||||||||||||||||||||||||||||||||||||||||
| 180 | 21 | $minutes -= $ret{hours} * 60; | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 64 | ▶ | if ( $units{minutes} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 11 | $ret{minutes} = $minutes; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 64 | ▶ | if ( $units{seconds} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 16 | $ret{seconds} = $seconds; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 16 | $seconds = 0; | |||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 64 | ▶ | if ( $units{nanoseconds} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 8 | $ret{nanoseconds} = $seconds * MAX_NANOSECONDS + $self->{nanoseconds}; | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 64 | ▶ | wantarray ? @ret{@units} : $ret{ $units[0] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | $new{end_of_month} = $p{end_of_month} | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 22872 | ▶ | if exists $p{end_of_month}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
@_ == 1 and builtin::blessed($_[0])
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__)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$dt ||= "DateTime"->now
$dt
"DateTime"->now
Truth table66%2/3
A ||= B
A= $dtB= "DateTime"->now
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 1 | ▶ | if ( DateTime::Helpers::isa( $d2, 'DateTime' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 6 | ▶ | Carp::croak( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||