Skip to content

Instantly share code, notes, and snippets.

@Altreus
Created March 31, 2022 16:14
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/17359580fbfcedd703ccc12e05741324 to your computer and use it in GitHub Desktop.
Save Altreus/17359580fbfcedd703ccc12e05741324 to your computer and use it in GitHub Desktop.
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 ));
return gather {
for 1 .. 7 {
my $later = $start.later(:days( $_ - 1 ));
if $later.month != $date.month {
take Nil;
}
else {
take $later;
}
}
};
}
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