aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-12-03 11:19:54 +0000
committerGitHub <noreply@github.com>2019-12-03 11:19:54 +0000
commit91015d97c23c47b8abb94a1812b33175d976e6af (patch)
tree58d13e6ba3c807fbc42e5d8692319cea5f7c9a83
parentbc68aba32bfce4a11ea2dc58dc7bead9c294d766 (diff)
parent837e9742b398b0d03fe415cc4edae1f6898fcabb (diff)
downloadperlweeklychallenge-club-91015d97c23c47b8abb94a1812b33175d976e6af.tar.gz
perlweeklychallenge-club-91015d97c23c47b8abb94a1812b33175d976e6af.tar.bz2
perlweeklychallenge-club-91015d97c23c47b8abb94a1812b33175d976e6af.zip
Merge pull request #998 from choroba/ech37
Add solutions to 037 (Workday count + Daylight loss) by E. Choroba
-rwxr-xr-xchallenge-037/e-choroba/perl5/ch-1.pl33
-rwxr-xr-xchallenge-037/e-choroba/perl5/ch-2.pl38
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-037/e-choroba/perl5/ch-1.pl b/challenge-037/e-choroba/perl5/ch-1.pl
new file mode 100755
index 0000000000..9369afe213
--- /dev/null
+++ b/challenge-037/e-choroba/perl5/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+use Time::Piece;
+use Time::Seconds qw{ ONE_DAY };
+
+sub days_in_month {
+ my ($month) = @_;
+ my $date = 'Time::Piece'->strptime("2019 $month 1 12:00",
+ '%Y %b %d %H:%M');
+ my $count = 0;
+ while ($date->month eq $month) {
+ ++$count unless grep $date->day eq $_, qw( Sat Sun );
+ $date += ONE_DAY;
+ }
+ return $count
+}
+
+use Test::More tests => 12;
+
+is days_in_month('Jan'), 23, 'Jan';
+is days_in_month('Feb'), 20, 'Feb';
+is days_in_month('Mar'), 21, 'Mar';
+is days_in_month('Apr'), 22, 'Apr';
+is days_in_month('May'), 23, 'May';
+is days_in_month('Jun'), 20, 'Jun';
+is days_in_month('Jul'), 23, 'Jul';
+is days_in_month('Aug'), 22, 'Aug';
+is days_in_month('Sep'), 21, 'Sep';
+is days_in_month('Oct'), 23, 'Oct';
+is days_in_month('Nov'), 21, 'Nov';
+is days_in_month('Dec'), 22, 'Dec';
diff --git a/challenge-037/e-choroba/perl5/ch-2.pl b/challenge-037/e-choroba/perl5/ch-2.pl
new file mode 100755
index 0000000000..83ac8e0cdb
--- /dev/null
+++ b/challenge-037/e-choroba/perl5/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+use Syntax::Construct qw{ /r };
+
+use WWW::Mechanize;
+use HTML::TableExtract;
+
+my $url_template
+ = 'https://www.timeanddate.com/sun/uk/london?month=%m%&year=2019';
+my $mech = 'WWW::Mechanize'->new;
+
+my %seconds;
+for my $month (11, 12) {
+ my $url = $url_template =~ s/%m%/$month/r;
+ $mech->get($url);
+ my $te = 'HTML::TableExtract'->new;
+ $te->parse($mech->content);
+ my $table = $te->first_table_found;
+ for ($table->rows) {
+ my $time = $_->[3] or next;
+
+ my ($h, $m, $s) = $time =~ /^([0-9]+):([0-9]+):([0-9]+)$/
+ or next;
+ $s += $m * 60 + $h * 60 * 60;
+ $seconds{$month} += $s;
+ }
+}
+
+my $s = $seconds{11} - $seconds{12};
+my $seconds = $s % 60;
+my $m = ($s - $seconds) / 60;
+my $minutes = $m % 60;
+my $hours = ($m - $minutes) / 60;
+
+say sprintf "%d seconds or %d:%02d:%02d", $s, $hours, $minutes, $seconds;