Skip to content

Commit

Permalink
Add max day check for Date.later as well
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Dec 24, 2015
1 parent 888ecd8 commit 2ef8881
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/core/Date.pm
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,24 @@ my class Date does Dateish {
when 'week' | 'weeks' { $day-delta = 7 * $amount; proceed }

when 'month' | 'months' {
my $month = $!month;
my $year = $!year;
my int $month = $!month;
my int $year = $!year;
$month += $amount;
$year += floor(($month - 1) / 12);
$month = ($month - 1) % 12 + 1;
# If we overflow on days in the month, rather than throw an
# exception, we just clip to the last of the month
my $day = $!day min $.days-in-month($year, $month);
$date = self.new($year,$month,$day);
$date = Date.new($year,$month,$!day > 28
?? $!day min self!DAYS-IN-MONTH($year,$month)
!! $!day);
succeed;
}

when 'year' | 'years' {
my $year = $!year + $amount;
$date = self.new($year,$!month,$!day);
my int $year = $!year + $amount;
$date = Date.new($year,$!month,$!day > 28
?? $!day min self!DAYS-IN-MONTH($year,$!month)
!! $!day);
succeed;
}

Expand Down

0 comments on commit 2ef8881

Please sign in to comment.