Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Streamline is-leap-year and days-in-month
Don't use junctions and give better error message when trying to use
the instance mode of the method on a class.
  • Loading branch information
lizmat committed Jul 15, 2015
1 parent 0493008 commit 0d660bb
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/core/Temporal.pm
Expand Up @@ -15,15 +15,18 @@ my role Dateish {
unless %UNITS.EXISTS-KEY($unit);
}

method is-leap-year($y = $!year) {
$y %% 4 and not $y %% 100 or $y %% 400
}

method days-in-month($year = $!year, $month = $!month) {
$month == 2 ?? self.is-leap-year($year) ?? 29 !! 28
!! $month == 4|6|9|11 ?? 30
!! 31
}
sub IS-LEAP-YEAR($y) { $y %% 4 and not $y %% 100 or $y %% 400 }
proto method is-leap-year(|) { * }
multi method is-leap-year(Dateish:D:) { IS-LEAP-YEAR($!year) }
multi method is-leap-year(Dateish: $y) { IS-LEAP-YEAR($y) }

sub DAYS-IN-MONTH($year, $month) {
state @l = 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31;
$month == 2 ?? 28 + IS-LEAP-YEAR($year) !! @l.AT-POS($month);
}
proto method days-in-month(|) { * }
multi method days-in-month(Dateish:D:) { DAYS-IN-MONTH($!year,$!month) }
multi method days-in-month(Dateish: $y, $m) { DAYS-IN-MONTH($y,$m) }

method daycount-from-ymd(Int() $y is copy, Int() $m is copy, $d) {
# taken from <http://www.merlyn.demon.co.uk/daycount.htm>
Expand Down

0 comments on commit 0d660bb

Please sign in to comment.