diff options
| -rw-r--r-- | challenge-237/mark-anderson/raku/WDOM.rakumod | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/challenge-237/mark-anderson/raku/WDOM.rakumod b/challenge-237/mark-anderson/raku/WDOM.rakumod index 8e96b4b280..01bb36e95e 100644 --- a/challenge-237/mark-anderson/raku/WDOM.rakumod +++ b/challenge-237/mark-anderson/raku/WDOM.rakumod @@ -3,26 +3,20 @@ unit class WDOM is Date is export; multi method new(:$weekday-of-month!, :$day-of-week!, :$month!, :$year! where $weekday-of-month eq 'L'|'Last') { my $dt = Date.new(:$year, :$month).last-date-in-month; - my $d = $dt.day-of-week - $day-of-week; - $dt - ($d >= 0 ?? $d !! 7 + $d) -} -multi method new(:$weekday-of-month!, :$day-of-week!, :$month!, :$year! where $weekday-of-month ~~ 1..4) -{ - my $dt = Date.new(:$year, :$month); - my $wdom = $day-of-week >= $dt.day-of-week ?? $weekday-of-month - 1 - !! $weekday-of-month; - $dt += 7 * $wdom + $day-of-week - $dt.day-of-week + my $day = [$dt.day-6..$dt.day].rotate($day-of-week - $dt.day-of-week).tail; + + Date.new(:$year, :$month, :$day) } -multi method new(:$weekday-of-month!, :$day-of-week!, :$month!, :$year! where $weekday-of-month == 5) +multi method new(:$weekday-of-month!, :$day-of-week!, :$month!, :$year!) { - my $dt = Date.new(:$year, :$month).last-date-in-month; - my $d = $dt.day-of-week - $day-of-week; - $dt -= $d >= 0 ?? $d !! 7 + $d; + my $dt = Date.new(:$year, :$month); + + my $day = [1..7].rotate($day-of-week - $dt.day-of-week).head; - return $dt if $dt.day == 29|30|31; + $day += ($weekday-of-month - 1) * 7; - # Todo: Handle the exception here. Just return the 4th day-of-week? - die "There isn't a 5th $day-of-week in $month $year" + # Todo: Handle the exception here? + Date.new(:$year, :$month, :$day) } |
