From 675100aaea984b1fb393bc9b3fae3908a56d73d6 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 31 Jul 2022 21:19:12 +0200 Subject: feat(challenge-175/lubos-kolouch/perl/ch-1/pl): Challenge 175 Task 1 LK Perl --- challenge-175/lubos-kolouch/perl/ch-1.pl | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 challenge-175/lubos-kolouch/perl/ch-1.pl diff --git a/challenge-175/lubos-kolouch/perl/ch-1.pl b/challenge-175/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..311236484f --- /dev/null +++ b/challenge-175/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,51 @@ +package main; +use strict; +use warnings; +use DateTime; + +sub get_last_sunday { + my ( $year, $month ) = @_; + + my $day = + DateTime->last_day_of_month( year => $year, month => $month )->day; + + while (1) { + my $dt = DateTime->new( + year => $year, + month => $month, + day => $day + ); + + if ( $dt->day_of_week == 7 ) { + return $dt->ymd; + } + + $day--; + } + return 1; +} + +sub get_sundays { + my $year = shift; + + my @sundays; + + for ( 1 .. 12 ) { + push @sundays, get_last_sunday( $year, $_ ); + } + + return \@sundays; +} + +use Test::More; + +is_deeply( + get_sundays(2022), + [ + '2022-01-30', '2022-02-27', '2022-03-27', '2022-04-24', + '2022-05-29', '2022-06-26', '2022-07-31', '2022-08-28', + '2022-09-25', '2022-10-30', '2022-11-27', '2022-12-25' + ] +); +done_testing; +1; -- cgit