diff options
| -rw-r--r-- | challenge-175/bob-lied/perl/ch-1.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-175/bob-lied/perl/ch-1.pl b/challenge-175/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..1d691a5314 --- /dev/null +++ b/challenge-175/bob-lied/perl/ch-1.pl @@ -0,0 +1,59 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge 175 Task 1 Last Sunday +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# Write a script to list Last Sunday of every month in the given year. +# For example, for year 2022, we should get the following: +# 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 +#============================================================================= + +use v5.36; + +use DateTime; + +use Getopt::Long; +my $DoTest = 0; + +GetOptions("test" => \$DoTest); +exit(!runTest()) if $DoTest; + +say join("\n", lastSunday($ARGV[0])->@*); + +sub lastSunday($year) +{ + my @sundayList; + for my $month ( 1 .. 12 ) + { + my $d = DateTime->last_day_of_month( year => $year, month => $month); + while ( $d->day_of_week != 7 ) + { + $d->subtract( days => 1 ); + } + push @sundayList, $d->ymd(); + } + return \@sundayList; +} + +sub runTest +{ + use Test2::V0; + + is( lastSunday(2022), + [ qw( 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 ) ], + , "Year 2022"); + + is( lastSunday(1958), + [ qw( 1958-01-26 1958-02-23 1958-03-30 1958-04-27 + 1958-05-25 1958-06-29 1958-07-27 1958-08-31 + 1958-09-28 1958-10-26 1958-11-30 1958-12-28 ) ], + , "Year 1958"); + + done_testing; +} |
