Skip to content

Instantly share code, notes, and snippets.

@Altreus
Last active March 31, 2022 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Altreus/8d797886a34afe219762f08d6719c899 to your computer and use it in GitHub Desktop.
Save Altreus/8d797886a34afe219762f08d6719c899 to your computer and use it in GitHub Desktop.
If I use push, all weeks are the last week
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;
}
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