diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2022-07-31 21:19:12 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2022-07-31 21:19:12 +0200 |
| commit | 675100aaea984b1fb393bc9b3fae3908a56d73d6 (patch) | |
| tree | dae626ccf6896efa5ac8284ce86eb686d3702d87 | |
| parent | 925d4a68cbfd366160835a465ea3dacc774c6203 (diff) | |
| download | perlweeklychallenge-club-675100aaea984b1fb393bc9b3fae3908a56d73d6.tar.gz perlweeklychallenge-club-675100aaea984b1fb393bc9b3fae3908a56d73d6.tar.bz2 perlweeklychallenge-club-675100aaea984b1fb393bc9b3fae3908a56d73d6.zip | |
feat(challenge-175/lubos-kolouch/perl/ch-1/pl): Challenge 175 Task 1 LK Perl
| -rw-r--r-- | challenge-175/lubos-kolouch/perl/ch-1.pl | 51 |
1 files changed, 51 insertions, 0 deletions
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; |
