aboutsummaryrefslogtreecommitdiff
path: root/challenge-037/javier-luque/perl5/ch-1.pl
blob: fc8228767933a57565841f6e952b63698bab4778 (plain)
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
#!/usr/bin/perl
# Test: ./ch1.pl 2019
use strict;
use warnings;
use feature qw /say/;
use DateTime;
use DateTime::Event::Recurrence;

show_weekdays_per_year($ARGV[0]);

sub show_weekdays_per_year {
    my $year = shift || 2019;
    for my $month (1..12) {
        show_weekdays_per_month($month, $year);
    }
}

sub show_weekdays_per_month {
    my ($month, $year) = @_;

    my $working_days =
        DateTime::Event::Recurrence->weekly(
            days => [1 .. 5]
        );

    # Start of the month
    my $start = DateTime->new(
        year  => $year,
        month => $month,
        day   => 1
    );

    # End of the month
    my $end = $start->clone;
    $end->add( months => 1 )
        ->subtract( days => 1 );

    my $num_days = $working_days->as_list(
        start => $start,
        end => $end
    );

    say $start->month_abbr() . ": $num_days days";
}