/
ch-1.pl
51 lines (40 loc) · 899 Bytes
/
ch-1.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;