From 296aebee4b1c7d91da164033ebecfa47cefbb75c Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 13 Nov 2021 13:46:32 +0100 Subject: Challenge 137 Task1 Perl LK --- challenge-138/lubos-kolouch/perl/ch-1.pl | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 challenge-138/lubos-kolouch/perl/ch-1.pl diff --git a/challenge-138/lubos-kolouch/perl/ch-1.pl b/challenge-138/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..ca8301e52f --- /dev/null +++ b/challenge-138/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,41 @@ +use strict; +use warnings; +use DateTime; +use List::Util qw/min max/; + +sub get_work_days { + my $what = shift; + + my $dt_1_1 = DateTime->new( + year => $what, + month => 1, + day => 1); + + + my $dt_31_12 = DateTime->new( + year => $what, + month => 12, + day => 31); + + + my $weeks_in_year = $dt_31_12->week_number; + $weeks_in_year += 1 if $dt_1_1->week_number != 1; + + my $result = $weeks_in_year * 5; + + # deduct left over days at the end of the year + $result -= max(5-$dt_31_12->day_of_week, 0); + + # deduct days at the beginning of the year + $result -= min($dt_1_1->day_of_week-1, 5); + + return $result; +} + +use Test::More; + +is(get_work_days(2021), 261); +is(get_work_days(2020), 262); + + +done_testing; -- cgit