+sub LastDayOfWeek {
+ my ($year, $month, $day, $matchday) = @_;
+ my $set = DateTime::Set->from_recurrence(
+ next => sub { $_[0]->truncate( to => 'day' )->add( days => 1 ) }
+ );
+
+ my $day = DateTime->new( year => $year, month => $month, day => $day );
+
+ $day = $set->next($day) while $day->day_of_week != $matchday;
+ $day;
+
+}
+