| Filename | /home/s1/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/x86_64-linux/DateTime/Duration.pm |
| Statements | Executed 19 statements in 3.90ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1.71ms | 2.78ms | DateTime::Duration::BEGIN@13 |
| 1 | 1 | 1 | 974µs | 12.8ms | DateTime::Duration::BEGIN@11 |
| 1 | 1 | 1 | 499µs | 3.08ms | DateTime::Duration::BEGIN@10 |
| 1 | 1 | 1 | 32µs | 36µs | DateTime::Duration::BEGIN@3 |
| 1 | 1 | 1 | 11µs | 66µs | DateTime::Duration::BEGIN@22 |
| 1 | 1 | 1 | 11µs | 11µs | DateTime::Duration::BEGIN@9 |
| 1 | 1 | 1 | 11µs | 18µs | DateTime::Duration::BEGIN@4 |
| 1 | 1 | 1 | 6µs | 6µs | DateTime::Duration::BEGIN@8 |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::_add_overload |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::_compare_overload |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::_has_negative |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::_has_positive |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::_multiply_overload |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::_normalize_nanoseconds |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::_subtract_overload |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::add |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::add_duration |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::calendar_duration |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::clock_duration |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::clone |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::compare |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::days |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::delta_days |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::delta_minutes |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::delta_months |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::delta_nanoseconds |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::delta_seconds |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::deltas |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::end_of_month_mode |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::hours |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::in_units |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::inverse |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::is_limit_mode |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::is_negative |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::is_positive |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::is_preserve_mode |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::is_wrap_mode |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::is_zero |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::minutes |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::months |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::multiply |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::nanoseconds |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::new |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::seconds |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::subtract |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::subtract_duration |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::weeks |
| 0 | 0 | 0 | 0s | 0s | DateTime::Duration::years |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package DateTime::Duration; | ||||
| 2 | |||||
| 3 | 2 | 41µs | 2 | 41µs | # spent 36µs (32+4) within DateTime::Duration::BEGIN@3 which was called:
# once (32µs+4µs) by DateTime::BEGIN@12 at line 3 # spent 36µs making 1 call to DateTime::Duration::BEGIN@3
# spent 4µs making 1 call to strict::import |
| 4 | 2 | 51µs | 2 | 26µs | # spent 18µs (11+8) within DateTime::Duration::BEGIN@4 which was called:
# once (11µs+8µs) by DateTime::BEGIN@12 at line 4 # spent 18µs making 1 call to DateTime::Duration::BEGIN@4
# spent 8µs making 1 call to warnings::import |
| 5 | |||||
| 6 | 1 | 600ns | our $VERSION = '1.27'; | ||
| 7 | |||||
| 8 | 2 | 30µs | 1 | 6µs | # spent 6µs within DateTime::Duration::BEGIN@8 which was called:
# once (6µs+0s) by DateTime::BEGIN@12 at line 8 # spent 6µs making 1 call to DateTime::Duration::BEGIN@8 |
| 9 | 2 | 36µs | 1 | 11µs | # spent 11µs within DateTime::Duration::BEGIN@9 which was called:
# once (11µs+0s) by DateTime::BEGIN@12 at line 9 # spent 11µs making 1 call to DateTime::Duration::BEGIN@9 |
| 10 | 2 | 338µs | 1 | 3.08ms | # spent 3.08ms (499µs+2.58) within DateTime::Duration::BEGIN@10 which was called:
# once (499µs+2.58ms) by DateTime::BEGIN@12 at line 10 # spent 3.08ms making 1 call to DateTime::Duration::BEGIN@10 |
| 11 | 2 | 573µs | 2 | 12.9ms | # spent 12.8ms (974µs+11.8) within DateTime::Duration::BEGIN@11 which was called:
# once (974µs+11.8ms) by DateTime::BEGIN@12 at line 11 # spent 12.8ms making 1 call to DateTime::Duration::BEGIN@11
# spent 89µs making 1 call to Exporter::import |
| 12 | |||||
| 13 | # spent 2.78ms (1.71+1.07) within DateTime::Duration::BEGIN@13 which was called:
# once (1.71ms+1.07ms) by DateTime::BEGIN@12 at line 20 | ||||
| 14 | 1 | 7µs | 1 | 64µs | fallback => 1, # spent 64µs making 1 call to overload::import |
| 15 | '+' => '_add_overload', | ||||
| 16 | '-' => '_subtract_overload', | ||||
| 17 | '*' => '_multiply_overload', | ||||
| 18 | '<=>' => '_compare_overload', | ||||
| 19 | 'cmp' => '_compare_overload', | ||||
| 20 | 1 | 859µs | 1 | 2.78ms | ); # spent 2.78ms making 1 call to DateTime::Duration::BEGIN@13 |
| 21 | |||||
| 22 | 2 | 1.95ms | 2 | 121µs | # spent 66µs (11+55) within DateTime::Duration::BEGIN@22 which was called:
# once (11µs+55µs) by DateTime::BEGIN@12 at line 22 # spent 66µs making 1 call to DateTime::Duration::BEGIN@22
# spent 55µs making 1 call to constant::import |
| 23 | |||||
| 24 | 1 | 2µs | my @all_units = qw( months days minutes seconds nanoseconds ); | ||
| 25 | |||||
| 26 | # XXX - need to reject non-integers but accept infinity, NaN, & | ||||
| 27 | # 1.56e+18 | ||||
| 28 | sub new { | ||||
| 29 | my $class = shift; | ||||
| 30 | my %p = validate( | ||||
| 31 | @_, { | ||||
| 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 => { | ||||
| 41 | type => SCALAR, default => undef, | ||||
| 42 | regex => qr/^(?:wrap|limit|preserve)$/ | ||||
| 43 | }, | ||||
| 44 | } | ||||
| 45 | ); | ||||
| 46 | |||||
| 47 | my $self = bless {}, $class; | ||||
| 48 | |||||
| 49 | $self->{months} = ( $p{years} * 12 ) + $p{months}; | ||||
| 50 | |||||
| 51 | $self->{days} = ( $p{weeks} * 7 ) + $p{days}; | ||||
| 52 | |||||
| 53 | $self->{minutes} = ( $p{hours} * 60 ) + $p{minutes}; | ||||
| 54 | |||||
| 55 | $self->{seconds} = $p{seconds}; | ||||
| 56 | |||||
| 57 | if ( $p{nanoseconds} ) { | ||||
| 58 | $self->{nanoseconds} = $p{nanoseconds}; | ||||
| 59 | $self->_normalize_nanoseconds; | ||||
| 60 | } | ||||
| 61 | else { | ||||
| 62 | |||||
| 63 | # shortcut - if they don't need nanoseconds | ||||
| 64 | $self->{nanoseconds} = 0; | ||||
| 65 | } | ||||
| 66 | |||||
| 67 | $self->{end_of_month} = ( | ||||
| 68 | defined $p{end_of_month} ? $p{end_of_month} | ||||
| 69 | : $self->{months} < 0 ? 'preserve' | ||||
| 70 | : 'wrap' | ||||
| 71 | ); | ||||
| 72 | |||||
| 73 | return $self; | ||||
| 74 | } | ||||
| 75 | |||||
| 76 | # make the signs of seconds, nanos the same; 0 < abs(nanos) < MAX_NANOS | ||||
| 77 | # NB this requires nanoseconds != 0 (callers check this already) | ||||
| 78 | sub _normalize_nanoseconds { | ||||
| 79 | my $self = shift; | ||||
| 80 | |||||
| 81 | return | ||||
| 82 | if ( $self->{nanoseconds} == DateTime::INFINITY() | ||||
| 83 | || $self->{nanoseconds} == DateTime::NEG_INFINITY() | ||||
| 84 | || $self->{nanoseconds} eq DateTime::NAN() ); | ||||
| 85 | |||||
| 86 | my $seconds = $self->{seconds} + $self->{nanoseconds} / MAX_NANOSECONDS; | ||||
| 87 | $self->{seconds} = int($seconds); | ||||
| 88 | $self->{nanoseconds} = $self->{nanoseconds} % MAX_NANOSECONDS; | ||||
| 89 | $self->{nanoseconds} -= MAX_NANOSECONDS if $seconds < 0; | ||||
| 90 | } | ||||
| 91 | |||||
| 92 | sub clone { bless { %{ $_[0] } }, ref $_[0] } | ||||
| 93 | |||||
| 94 | sub years { abs( $_[0]->in_units('years') ) } | ||||
| 95 | sub months { abs( $_[0]->in_units( 'months', 'years' ) ) } | ||||
| 96 | sub weeks { abs( $_[0]->in_units('weeks') ) } | ||||
| 97 | sub days { abs( $_[0]->in_units( 'days', 'weeks' ) ) } | ||||
| 98 | sub hours { abs( $_[0]->in_units('hours') ) } | ||||
| 99 | sub minutes { abs( $_[0]->in_units( 'minutes', 'hours' ) ) } | ||||
| 100 | sub seconds { abs( $_[0]->in_units('seconds') ) } | ||||
| 101 | sub nanoseconds { abs( $_[0]->in_units( 'nanoseconds', 'seconds' ) ) } | ||||
| 102 | |||||
| 103 | sub is_positive { $_[0]->_has_positive && !$_[0]->_has_negative } | ||||
| 104 | sub is_negative { !$_[0]->_has_positive && $_[0]->_has_negative } | ||||
| 105 | |||||
| 106 | sub _has_positive { | ||||
| 107 | ( grep { $_ > 0 } @{ $_[0] }{@all_units} ) ? 1 : 0; | ||||
| 108 | } | ||||
| 109 | |||||
| 110 | sub _has_negative { | ||||
| 111 | ( grep { $_ < 0 } @{ $_[0] }{@all_units} ) ? 1 : 0; | ||||
| 112 | } | ||||
| 113 | |||||
| 114 | sub is_zero { | ||||
| 115 | return 0 if grep { $_ != 0 } @{ $_[0] }{@all_units}; | ||||
| 116 | return 1; | ||||
| 117 | } | ||||
| 118 | |||||
| 119 | sub delta_months { $_[0]->{months} } | ||||
| 120 | sub delta_days { $_[0]->{days} } | ||||
| 121 | sub delta_minutes { $_[0]->{minutes} } | ||||
| 122 | sub delta_seconds { $_[0]->{seconds} } | ||||
| 123 | sub delta_nanoseconds { $_[0]->{nanoseconds} } | ||||
| 124 | |||||
| 125 | sub deltas { | ||||
| 126 | map { $_ => $_[0]->{$_} } @all_units; | ||||
| 127 | } | ||||
| 128 | |||||
| 129 | sub in_units { | ||||
| 130 | my $self = shift; | ||||
| 131 | my @units = @_; | ||||
| 132 | |||||
| 133 | my %units = map { $_ => 1 } @units; | ||||
| 134 | |||||
| 135 | my %ret; | ||||
| 136 | |||||
| 137 | my ( $months, $days, $minutes, $seconds ) | ||||
| 138 | = @{$self}{qw( months days minutes seconds )}; | ||||
| 139 | |||||
| 140 | if ( $units{years} ) { | ||||
| 141 | $ret{years} = int( $months / 12 ); | ||||
| 142 | $months -= $ret{years} * 12; | ||||
| 143 | } | ||||
| 144 | |||||
| 145 | if ( $units{months} ) { | ||||
| 146 | $ret{months} = $months; | ||||
| 147 | } | ||||
| 148 | |||||
| 149 | if ( $units{weeks} ) { | ||||
| 150 | $ret{weeks} = int( $days / 7 ); | ||||
| 151 | $days -= $ret{weeks} * 7; | ||||
| 152 | } | ||||
| 153 | |||||
| 154 | if ( $units{days} ) { | ||||
| 155 | $ret{days} = $days; | ||||
| 156 | } | ||||
| 157 | |||||
| 158 | if ( $units{hours} ) { | ||||
| 159 | $ret{hours} = int( $minutes / 60 ); | ||||
| 160 | $minutes -= $ret{hours} * 60; | ||||
| 161 | } | ||||
| 162 | |||||
| 163 | if ( $units{minutes} ) { | ||||
| 164 | $ret{minutes} = $minutes; | ||||
| 165 | } | ||||
| 166 | |||||
| 167 | if ( $units{seconds} ) { | ||||
| 168 | $ret{seconds} = $seconds; | ||||
| 169 | $seconds = 0; | ||||
| 170 | } | ||||
| 171 | |||||
| 172 | if ( $units{nanoseconds} ) { | ||||
| 173 | $ret{nanoseconds} = $seconds * MAX_NANOSECONDS + $self->{nanoseconds}; | ||||
| 174 | } | ||||
| 175 | |||||
| 176 | wantarray ? @ret{@units} : $ret{ $units[0] }; | ||||
| 177 | } | ||||
| 178 | |||||
| 179 | sub is_wrap_mode { $_[0]->{end_of_month} eq 'wrap' ? 1 : 0 } | ||||
| 180 | sub is_limit_mode { $_[0]->{end_of_month} eq 'limit' ? 1 : 0 } | ||||
| 181 | sub is_preserve_mode { $_[0]->{end_of_month} eq 'preserve' ? 1 : 0 } | ||||
| 182 | |||||
| 183 | sub end_of_month_mode { $_[0]->{end_of_month} } | ||||
| 184 | |||||
| 185 | sub calendar_duration { | ||||
| 186 | my $self = shift; | ||||
| 187 | |||||
| 188 | return ( ref $self ) | ||||
| 189 | ->new( map { $_ => $self->{$_} } qw( months days end_of_month ) ); | ||||
| 190 | } | ||||
| 191 | |||||
| 192 | sub clock_duration { | ||||
| 193 | my $self = shift; | ||||
| 194 | |||||
| 195 | return ( ref $self ) | ||||
| 196 | ->new( map { $_ => $self->{$_} } | ||||
| 197 | qw( minutes seconds nanoseconds end_of_month ) ); | ||||
| 198 | } | ||||
| 199 | |||||
| 200 | sub inverse { | ||||
| 201 | my $self = shift; | ||||
| 202 | my %p = @_; | ||||
| 203 | |||||
| 204 | my %new; | ||||
| 205 | foreach my $u (@all_units) { | ||||
| 206 | $new{$u} = $self->{$u}; | ||||
| 207 | |||||
| 208 | # avoid -0 bug | ||||
| 209 | $new{$u} *= -1 if $new{$u}; | ||||
| 210 | } | ||||
| 211 | |||||
| 212 | $new{end_of_month} = $p{end_of_month} | ||||
| 213 | if exists $p{end_of_month}; | ||||
| 214 | |||||
| 215 | return ( ref $self )->new(%new); | ||||
| 216 | } | ||||
| 217 | |||||
| 218 | sub add_duration { | ||||
| 219 | my ( $self, $dur ) = @_; | ||||
| 220 | |||||
| 221 | foreach my $u (@all_units) { | ||||
| 222 | $self->{$u} += $dur->{$u}; | ||||
| 223 | } | ||||
| 224 | |||||
| 225 | $self->_normalize_nanoseconds if $self->{nanoseconds}; | ||||
| 226 | |||||
| 227 | return $self; | ||||
| 228 | } | ||||
| 229 | |||||
| 230 | sub add { | ||||
| 231 | my $self = shift; | ||||
| 232 | |||||
| 233 | return $self->add_duration( ( ref $self )->new(@_) ); | ||||
| 234 | } | ||||
| 235 | |||||
| 236 | sub subtract_duration { return $_[0]->add_duration( $_[1]->inverse ) } | ||||
| 237 | |||||
| 238 | sub subtract { | ||||
| 239 | my $self = shift; | ||||
| 240 | |||||
| 241 | return $self->subtract_duration( ( ref $self )->new(@_) ); | ||||
| 242 | } | ||||
| 243 | |||||
| 244 | sub multiply { | ||||
| 245 | my $self = shift; | ||||
| 246 | my $multiplier = shift; | ||||
| 247 | |||||
| 248 | foreach my $u (@all_units) { | ||||
| 249 | $self->{$u} *= $multiplier; | ||||
| 250 | } | ||||
| 251 | |||||
| 252 | $self->_normalize_nanoseconds if $self->{nanoseconds}; | ||||
| 253 | |||||
| 254 | return $self; | ||||
| 255 | } | ||||
| 256 | |||||
| 257 | sub compare { | ||||
| 258 | my ( $class, $dur1, $dur2, $dt ) = @_; | ||||
| 259 | |||||
| 260 | $dt ||= DateTime->now; | ||||
| 261 | |||||
| 262 | return DateTime->compare( | ||||
| 263 | $dt->clone->add_duration($dur1), | ||||
| 264 | $dt->clone->add_duration($dur2) | ||||
| 265 | ); | ||||
| 266 | } | ||||
| 267 | |||||
| 268 | sub _add_overload { | ||||
| 269 | my ( $d1, $d2, $rev ) = @_; | ||||
| 270 | |||||
| 271 | ( $d1, $d2 ) = ( $d2, $d1 ) if $rev; | ||||
| 272 | |||||
| 273 | if ( DateTime::Helpers::isa( $d2, 'DateTime' ) ) { | ||||
| 274 | $d2->add_duration($d1); | ||||
| 275 | return; | ||||
| 276 | } | ||||
| 277 | |||||
| 278 | # will also work if $d1 is a DateTime.pm object | ||||
| 279 | return $d1->clone->add_duration($d2); | ||||
| 280 | } | ||||
| 281 | |||||
| 282 | sub _subtract_overload { | ||||
| 283 | my ( $d1, $d2, $rev ) = @_; | ||||
| 284 | |||||
| 285 | ( $d1, $d2 ) = ( $d2, $d1 ) if $rev; | ||||
| 286 | |||||
| 287 | Carp::croak( | ||||
| 288 | "Cannot subtract a DateTime object from a DateTime::Duration object") | ||||
| 289 | if DateTime::Helpers::isa( $d2, 'DateTime' ); | ||||
| 290 | |||||
| 291 | return $d1->clone->subtract_duration($d2); | ||||
| 292 | } | ||||
| 293 | |||||
| 294 | sub _multiply_overload { | ||||
| 295 | my $self = shift; | ||||
| 296 | |||||
| 297 | my $new = $self->clone; | ||||
| 298 | |||||
| 299 | return $new->multiply(@_); | ||||
| 300 | } | ||||
| 301 | |||||
| 302 | sub _compare_overload { | ||||
| 303 | Carp::croak( 'DateTime::Duration does not overload comparison.' | ||||
| 304 | . ' See the documentation on the compare() method for details.' | ||||
| 305 | ); | ||||
| 306 | } | ||||
| 307 | |||||
| 308 | 1 | 6µs | 1; | ||
| 309 | |||||
| 310 | # ABSTRACT: Duration objects for date math | ||||
| 311 | |||||
| 312 | __END__ |