-
-
Save Altreus/8d797886a34afe219762f08d6719c899 to your computer and use it in GitHub Desktop.
If I use push, all weeks are the last week
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit module Calendar; | |
sub week-as-cells ( DateTime $date is copy ) is export { | |
my $first-of-week = $*START-OF-WEEK // 1; | |
my $start = $date.earlier(:days( ($date.day-of-week - $first-of-week) % 7 )); | |
my @week = gather { | |
for 1 .. 7 { | |
my $later = $start.later(:days( $_ - 1 )); | |
if $later.month != $date.month { | |
take Nil; | |
} | |
else { | |
take $later; | |
} | |
} | |
}; | |
return @week; | |
} | |
sub month-as-table ( DateTime $date = DateTime.now.truncated-to('month') ) is export { | |
my @month; | |
my @week; | |
my $this-week = $date; | |
repeat { | |
@week = week-as-cells( $this-week ); | |
@month.push: @week; | |
$this-week = @week[* -1].?later(:1day); | |
} until any( @week.map: { .?day // 0 } ) == $date.days-in-month; | |
return @month; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub month-as-table ( DateTime $date = DateTime.now.truncated-to('month') ) is export { | |
gather { | |
my @week; | |
my $this-week = $date; | |
repeat { | |
@week = week-as-cells( $this-week ); | |
take @week; | |
$this-week = @week[* -1].?later(:1day); | |
} until any( @week.map: { .?day // 0 } ) == $date.days-in-month; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment