| 1 | package DateTime::PP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '1.68'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | ## no critic (Variables::ProhibitPackageVars) | ||||||||||||||||||||||||||||
| 9 | 1 | $DateTime::IsPurePerl = 1; | |||||||||||||||||||||||||||
| 10 | ## use critic | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | my @MonthLengths = ( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | my @LeapYearMonthLengths = @MonthLengths; | |||||||||||||||||||||||||||
| 15 | 1 | $LeapYearMonthLengths[1]++; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | my @EndOfLastMonthDayOfYear; | |||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | 1 | my $x = 0; | |||||||||||||||||||||||||||
| 20 | 1 | foreach my $length (@MonthLengths) { | |||||||||||||||||||||||||||
| 21 | 12 | push @EndOfLastMonthDayOfYear, $x; | |||||||||||||||||||||||||||
| 22 | 12 | $x += $length; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | my @EndOfLastMonthDayOfLeapYear = @EndOfLastMonthDayOfYear; | |||||||||||||||||||||||||||
| 27 | 1 | $EndOfLastMonthDayOfLeapYear[$_]++ for 2 .. 11; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub _time_as_seconds { | ||||||||||||||||||||||||||
|
Subroutine
_time_as_seconds: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||
| 31 | 1 | shift; | |||||||||||||||||||||||||||
| 32 | 1 | my ( $hour, $min, $sec ) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $hour ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$hour ||= 0
MC/DC0%0/1
$hour ||= 0
$hour
Truth table50%1/2
A
A= $hour
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | $min ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$min ||= 0
MC/DC0%0/1
$min ||= 0
$min
Truth table50%1/2
A
A= $min
| |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | $sec ||= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$sec ||= 0
MC/DC0%0/1
$sec ||= 0
$sec
Truth table50%1/2
A
A= $sec
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | my $secs = $hour * 3600 + $min * 60 + $sec; | |||||||||||||||||||||||||||
| 39 | 1 | return $secs; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | sub _rd2ymd { | ||||||||||||||||||||||||||
|
Subroutine
_rd2ymd: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||
| 43 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | my $d = shift; | |||||||||||||||||||||||||||
| 47 | 1 | my $rd = $d; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | my $yadj = 0; | |||||||||||||||||||||||||||
| 50 | 1 | my ( $c, $y, $m ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # add 306 days to make relative to Mar 1, 0 | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | if ( ( $d += 306 ) <= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # avoid ambiguity in C division of negatives | ||||||||||||||||||||||||||||
| 56 | 0 | $yadj = -( -$d / 146097 + 1 ); | |||||||||||||||||||||||||||
| 57 | 0 | $d -= $yadj * 146097; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | $c = ( $d * 4 - 1 ) | |||||||||||||||||||||||||||
| 61 | / 146097; # calc # of centuries $d is after 29 Feb of yr 0 | ||||||||||||||||||||||||||||
| 62 | 1 | $d -= $c * 146097 / 4; # (4 centuries = 146097 days) | |||||||||||||||||||||||||||
| 63 | 1 | $y = ( $d * 4 - 1 ) / 1461; # calc number of years into the century, | |||||||||||||||||||||||||||
| 64 | 1 | $d -= $y * 1461 / 4; # again March-based (4 yrs =~ 146[01] days) | |||||||||||||||||||||||||||
| 65 | 1 | $m = ( $d * 12 + 1093 ) | |||||||||||||||||||||||||||
| 66 | / 367; # get the month (3..14 represent March through | ||||||||||||||||||||||||||||
| 67 | 1 | $d -= ( $m * 367 - 1094 ) / 12; # February of following year) | |||||||||||||||||||||||||||
| 68 | 1 | $y += $c * 100 + $yadj * 400; # get the real year, which is off by | |||||||||||||||||||||||||||
| 69 | # one if month is January or February | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | if ( $m > 12 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 1 | ++$y; | |||||||||||||||||||||||||||
| 73 | 1 | $m -= 12; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | if ( $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | my $dow; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | if ( $rd < -6 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | $dow = ( $rd + 6 ) % 7; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | $dow += $dow ? 8 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | 1 | $dow = ( ( $rd + 6 ) % 7 ) + 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | my $doy = $class->_end_of_last_month_day_of_year( $y, $m ); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | $doy += $d; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | my $quarter; | |||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 1 | ▶ | no integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | 1 | $quarter = int( ( 1 / 3.1 ) * $m ) + 1; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | my $qm = ( 3 * $quarter ) - 2; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | my $doq | |||||||||||||||||||||||||||
| 100 | = ( $doy - $class->_end_of_last_month_day_of_year( $y, $qm ) ); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | return ( $y, $m, $d, $dow, $doy, $quarter, $doq ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return ( $y, $m, $d ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 28 | ▶ | sub _ymd2rd { | ||||||||||||||||||||||||||
|
Subroutine
_ymd2rd: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||
| 109 | 28 | shift; # ignore class | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 28 | my ( $y, $m, $d ) = @_; | |||||||||||||||||||||||||||
| 113 | 28 | my $adj; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # make month in range 3..14 (treat Jan & Feb as months 13..14 of | ||||||||||||||||||||||||||||
| 116 | # prev year) | ||||||||||||||||||||||||||||
| 117 | 28 | ▶ | if ( $m <= 2 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 118 | 17 | $y -= ( $adj = ( 14 - $m ) / 12 ); | |||||||||||||||||||||||||||
| 119 | 17 | $m += 12 * $adj; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | elsif ( $m > 14 ) { | ||||||||||||||||||||||||||||
| 122 | 0 | $y += ( $adj = ( $m - 3 ) / 12 ); | |||||||||||||||||||||||||||
| 123 | 0 | $m -= 12 * $adj; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # make year positive (oh, for a use integer 'sane_div'!) | ||||||||||||||||||||||||||||
| 127 | 28 | ▶ | if ( $y < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $d -= 146097 * ( $adj = ( 399 - $y ) / 400 ); | |||||||||||||||||||||||||||
| 129 | 0 | $y += 400 * $adj; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # add: day of month, days of previous 0-11 month period that began | ||||||||||||||||||||||||||||
| 133 | # w/March, days of previous 0-399 year period that began w/March | ||||||||||||||||||||||||||||
| 134 | # of a 400-multiple year), days of any 400-year periods before | ||||||||||||||||||||||||||||
| 135 | # that, and finally subtract 306 days to adjust from Mar 1, year | ||||||||||||||||||||||||||||
| 136 | # 0-relative to Jan 1, year 1-relative (whew) | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | $d | ||||||||||||||||||||||||||||
| 139 | 28 | += ( $m * 367 - 1094 ) / 12 | |||||||||||||||||||||||||||
| 140 | + $y % 100 * 1461 / 4 | ||||||||||||||||||||||||||||
| 141 | + ( $y / 100 * 36524 + $y / 400 ) | ||||||||||||||||||||||||||||
| 142 | - 306; | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | sub _seconds_as_components { | ||||||||||||||||||||||||||
|
Subroutine
_seconds_as_components: called CC=6 SCAR=21.9
| |||||||||||||||||||||||||||||
| 146 | 1 | shift; | |||||||||||||||||||||||||||
| 147 | 1 | my $secs = shift; | |||||||||||||||||||||||||||
| 148 | 1 | my $utc_secs = shift; | |||||||||||||||||||||||||||
| 149 | 1 | ▶ | my $modifier = shift || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | $secs -= $modifier; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | my $hour = $secs / 3600; | |||||||||||||||||||||||||||
| 156 | 1 | $secs -= $hour * 3600; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | my $minute = $secs / 60; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | my $second = $secs - ( $minute * 60 ); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | ▶ | if ( $utc_secs && $utc_secs >= 86400 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$utc_secs and $utc_secs >= 86400
MC/DC0%0/2
$utc_secs and $utc_secs >= 86400
$utc_secs
$utc_secs >= 86400
Truth table33%1/3
A and B
A= $utc_secsB= $utc_secs >= 86400
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # there is no such thing as +3 or more leap seconds! | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | die "Invalid UTC RD seconds value: $utc_secs" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | if $utc_secs > 86401; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | $second += $utc_secs - 86400 + 60; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | $minute = 59; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | $hour--; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | $hour = 23 if $hour < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 1 | return ( $hour, $minute, $second ); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 2 | ▶ | sub _end_of_last_month_day_of_year { | ||||||||||||||||||||||||||
|
Subroutine
_end_of_last_month_day_of_year: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 180 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 2 | my ( $y, $m ) = @_; | |||||||||||||||||||||||||||
| 183 | 2 | $m--; | |||||||||||||||||||||||||||
| 184 | return ( | ||||||||||||||||||||||||||||
| 185 | 2 | ▶ | $class->_is_leap_year($y) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | ? $EndOfLastMonthDayOfLeapYear[$m] | ||||||||||||||||||||||||||||
| 187 | : $EndOfLastMonthDayOfYear[$m] | ||||||||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | ▶ | sub _is_leap_year { | ||||||||||||||||||||||||||
|
Subroutine
_is_leap_year: called CC=6 SCAR=26.5
| |||||||||||||||||||||||||||||
| 192 | 2 | shift; | |||||||||||||||||||||||||||
| 193 | 2 | my $year = shift; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # According to Bjorn Tackmann, this line prevents an infinite loop | ||||||||||||||||||||||||||||
| 196 | # when running the tests under Qemu. I cannot reproduce this on | ||||||||||||||||||||||||||||
| 197 | # Ubuntu or with Strawberry Perl on Win2K. | ||||||||||||||||||||||||||||
| 198 | 2 | ▶ | return 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$year == "Inf" or $year == "-Inf"
MC/DC0%0/2
$year == "Inf" or $year == "-Inf"
$year == "Inf"
$year == "-Inf"
Truth table33%1/3
A or B
A= $year == "Inf"B= $year == "-Inf"
| |||||||||||||||||||||||||||||
| 199 | if $year == DateTime::INFINITY() || $year == DateTime::NEG_INFINITY(); | ||||||||||||||||||||||||||||
| 200 | 2 | ▶ | return 0 if $year % 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | ▶ | return 1 if $year % 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | return 0 if $year % 400; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | return 1; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | sub _day_length { DateTime::LeapSecond::day_length( $_[1] ) } | ||||||||||||||||||||||||||
|
Subroutine
_day_length: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub _accumulated_leap_seconds { DateTime::LeapSecond::leap_seconds( $_[1] ) } | ||||||||||||||||||||||||||
|
Subroutine
_accumulated_leap_seconds: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 1 | my @subs = qw( | |||||||||||||||||||||||||||
| 212 | _time_as_seconds | ||||||||||||||||||||||||||||
| 213 | _rd2ymd | ||||||||||||||||||||||||||||
| 214 | _ymd2rd | ||||||||||||||||||||||||||||
| 215 | _seconds_as_components | ||||||||||||||||||||||||||||
| 216 | _end_of_last_month_day_of_year | ||||||||||||||||||||||||||||
| 217 | _is_leap_year | ||||||||||||||||||||||||||||
| 218 | _day_length | ||||||||||||||||||||||||||||
| 219 | _accumulated_leap_seconds | ||||||||||||||||||||||||||||
| 220 | ); | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1 | for my $sub (@subs) { | |||||||||||||||||||||||||||
| 223 | ## no critic (TestingAndDebugging::ProhibitNoStrict) | ||||||||||||||||||||||||||||
| 224 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 225 | 8 | *{ 'DateTime::' . $sub } = __PACKAGE__->can($sub); | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # This is down here so that _ymd2rd is available when it loads, | ||||||||||||||||||||||||||||
| 229 | # because it will load DateTime::LeapSecond, which needs | ||||||||||||||||||||||||||||
| 230 | # DateTime->_ymd2rd to be available when it is loading | ||||||||||||||||||||||||||||
| 231 | 1 | require DateTime::PPExtra; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 1 | 1; | |||||||||||||||||||||||||||