| 1 | package Calendar::Any::Julian; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 6 | $Calendar::Any::Julian::VERSION = '0.5'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use POSIX qw/ceil/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use base 'Calendar::Any'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | my @MONTH_DAYS = (0,31,59,90,120,151,181,212,243,273,304,334,365); | |||||||||||||||||||||||||||
| 9 | 6 | our $default_format = "%D"; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | sub MONTH_DAYS { | ||||||||||||||||||||||||||
|
Subroutine
MONTH_DAYS: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 5 | return @MONTH_DAYS; | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 45 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 45 | my $_class = shift; | |||||||||||||||||||||||||||
| 17 | 45 | ▶ | my $class = ref $_class || $_class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||||||||||
| 18 | 45 | my $self = {}; | |||||||||||||||||||||||||||
| 19 | 45 | bless $self, $class; | |||||||||||||||||||||||||||
| 20 | 45 | ▶ | if ( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 45 | my %arg; | |||||||||||||||||||||||||||
| 22 | 45 | ▶ | if ( $_[0] =~ /-\D/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 2 | %arg = @_; | |||||||||||||||||||||||||||
| 24 | } else { | ||||||||||||||||||||||||||||
| 25 | 43 | ▶ | if ( scalar(@_) == 3 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 4 | $arg{$_} = shift for qw(-month -day -year); | |||||||||||||||||||||||||||
| 27 | } else { | ||||||||||||||||||||||||||||
| 28 | 39 | return $self->from_absolute(shift); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 6 | foreach ( qw(-month -day -year) ) { | |||||||||||||||||||||||||||
| 32 | 18 | ▶ | $self->{substr($_, 1)} = $arg{$_} if exists $arg{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 6 | $self->absolute_date(); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 6 | return $self; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | sub from_absolute { | ||||||||||||||||||||||||||
|
Subroutine
from_absolute: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 6 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | 2 | my $date = shift; | |||||||||||||||||||||||||||
| 43 | 2 | $self->{absolute} = $date; | |||||||||||||||||||||||||||
| 44 | 2 | $date++; | |||||||||||||||||||||||||||
| 45 | 2 | my $n4 = $date / 1461; | |||||||||||||||||||||||||||
| 46 | 2 | my $d0 = $date % 1461; | |||||||||||||||||||||||||||
| 47 | 2 | my $n1 = $d0 / 365; | |||||||||||||||||||||||||||
| 48 | 2 | my $day = $d0 % 365 + 1; | |||||||||||||||||||||||||||
| 49 | 2 | my $year = 4 * $n4 + $n1; | |||||||||||||||||||||||||||
| 50 | 2 | my $month; | |||||||||||||||||||||||||||
| 51 | 2 | ▶ | if ( $n1==4 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | $month = 12; | |||||||||||||||||||||||||||
| 53 | 0 | $day = 31; | |||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 2 | $year++; | |||||||||||||||||||||||||||
| 56 | 2 | $month = ceil($day/31); | |||||||||||||||||||||||||||
| 57 | 2 | ▶ | my $leap = (_is_leap_year($year) ? 1 : 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | while ($day > $MONTH_DAYS[$month] + ($month >1 ? $leap: 0)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 59 | 2 | $month++; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 2 | ▶ | $day = $day-$MONTH_DAYS[$month-1]-($month>2?$leap:0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 2 | $self->{year} = $year; | |||||||||||||||||||||||||||
| 64 | 2 | $self->{month} = $month; | |||||||||||||||||||||||||||
| 65 | 2 | $self->{day} = $day; | |||||||||||||||||||||||||||
| 66 | 2 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | sub absolute_date { | ||||||||||||||||||||||||||
|
Subroutine
absolute_date: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 6 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 4 | ▶ | if ( exists $self->{absolute} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 2 | return $self->{absolute}; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 2 | $self->assert_date(); | |||||||||||||||||||||||||||
| 76 | 2 | return $self->{absolute} = _absoulte_date($self->month, $self->day, $self->year); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | sub is_leap_year { | ||||||||||||||||||||||||||
|
Subroutine
is_leap_year: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 1 | return _is_leap_year(shift->year); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 6 | ▶ | sub day_of_year { | ||||||||||||||||||||||||||
|
Subroutine
day_of_year: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 6 | return _day_of_year($self->month, $self->day, $self->year); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 6 | ▶ | sub last_day_of_month { | ||||||||||||||||||||||||||
|
Subroutine
last_day_of_month: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 90 | 6 | return _last_day_of_month($self->month, $self->year); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 6 | ▶ | sub assert_date { | ||||||||||||||||||||||||||
|
Subroutine
assert_date: called CC=6 SCAR=24.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 95 | 6 | ▶ | if ( $self->year == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | croak('Not a valid year: should not be zero in ' . ref $self); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 6 | ▶ | if ( $self->month < 1 || $self->month > 12 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->month < 1 or $self->month > 12
MC/DC0%0/2
$self->month < 1 or $self->month > 12
$self->month < 1
$self->month > 12
Truth table33%1/3
A or B
A= $self->month < 1B= $self->month > 12
| |||||||||||||||||||||||||||||
| 99 | 0 | confess(sprintf('Not a valid month %d: should from 1 to 12 for %s', $self->month, ref $self)); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 6 | ▶ | if ( $self->day < 1 || $self->day > $self->last_day_of_month() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->day < 1 or $self->day > $self->last_day_of_month
MC/DC0%0/2
$self->day < 1 or $self->day > $self->last_day_of_month
$self->day < 1
$self->day > $self->last_day_of_month
Truth table33%1/3
A or B
A= $self->day < 1B= $self->day > $self->last_day_of_month
| |||||||||||||||||||||||||||||
| 102 | 0 | confess(sprintf('Not a valid day %d: should from 1 to %d in month %d %d for %s', | |||||||||||||||||||||||||||
| 103 | $self->day, $self->last_day_of_month, $self->month, $self->year, ref $self)); | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | #========================================================== | ||||||||||||||||||||||||||||
| 108 | # Private functions | ||||||||||||||||||||||||||||
| 109 | #========================================================== | ||||||||||||||||||||||||||||
| 110 | 2 | ▶ | sub _absoulte_date { | ||||||||||||||||||||||||||
|
Subroutine
_absoulte_date: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | 2 | my ($month, $day, $year) = @_; | |||||||||||||||||||||||||||
| 112 | 2 | int(_day_of_year($month, $day, $year) + 365*($year-1) + ($year-1)/4 -2); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 8 | ▶ | sub _day_of_year { | ||||||||||||||||||||||||||
|
Subroutine
_day_of_year: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 116 | 6 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 8 | my ($month, $day, $year) = @_; | |||||||||||||||||||||||||||
| 118 | 8 | my $day_of_year = $day + 31 * ($month-1); | |||||||||||||||||||||||||||
| 119 | 8 | ▶ | if ( $month > 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 5 | $day_of_year -= (23 + 4*$month)/10; | |||||||||||||||||||||||||||
| 121 | 5 | ▶ | if ( _is_leap_year($year) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $day_of_year++; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 8 | return $day_of_year; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 8 | ▶ | sub _is_leap_year { | ||||||||||||||||||||||||||
|
Subroutine
_is_leap_year: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 129 | 8 | my $year = shift; | |||||||||||||||||||||||||||
| 130 | 8 | ▶ | if ( $year < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $year = abs($year) - 1; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 8 | $year % 4 == 0 | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 6 | ▶ | sub _last_day_of_month { | ||||||||||||||||||||||||||
|
Subroutine
_last_day_of_month: called CC=5 SCAR=20.2
| |||||||||||||||||||||||||||||
| 137 | 6 | my ($month, $year) = @_; | |||||||||||||||||||||||||||
| 138 | 6 | ▶ | return unless $month>0 && $month<13; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$month > 0 and $month < 13
MC/DC0%0/2
$month > 0 and $month < 13
$month > 0
$month < 13
Truth table33%1/3
A and B
A= $month > 0B= $month < 13
| |||||||||||||||||||||||||||||
| 139 | 6 | ▶ | if ( $month==2 && _is_leap_year($year) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$month == 2 and _is_leap_year($year)
MC/DC0%0/2
$month == 2 and _is_leap_year($year)
$month == 2
_is_leap_year($year)
Truth table33%1/3
A and B
A= $month == 2B= _is_leap_year($year)
| |||||||||||||||||||||||||||||
| 140 | 0 | 29; | |||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||
| 142 | 6 | $MONTH_DAYS[$month]-$MONTH_DAYS[$month-1]; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 6 | 1; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||