aboutsummaryrefslogtreecommitdiff
path: root/challenge-037
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-12-03 14:44:23 +0000
committerGitHub <noreply@github.com>2019-12-03 14:44:23 +0000
commit767e55dc35ccd43fc170b56ad7b2567d0b978dd4 (patch)
tree691f37d034592f9ce8d6fdc2097e6e77e28294ce /challenge-037
parent2a7e4667852f31e5188c8a2aebd7a536a2838069 (diff)
parent7c8714a50acefd87e8f9e2f7900c92738184a53b (diff)
downloadperlweeklychallenge-club-767e55dc35ccd43fc170b56ad7b2567d0b978dd4.tar.gz
perlweeklychallenge-club-767e55dc35ccd43fc170b56ad7b2567d0b978dd4.tar.bz2
perlweeklychallenge-club-767e55dc35ccd43fc170b56ad7b2567d0b978dd4.zip
Merge pull request #1000 from jacoby/master
Time and Place
Diffstat (limited to 'challenge-037')
-rw-r--r--challenge-037/dave-jacoby/blog.txt1
-rw-r--r--challenge-037/dave-jacoby/perl5/ch-1.pl39
-rw-r--r--challenge-037/dave-jacoby/perl5/ch-2.pl88
3 files changed, 128 insertions, 0 deletions
diff --git a/challenge-037/dave-jacoby/blog.txt b/challenge-037/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..8938bc9872
--- /dev/null
+++ b/challenge-037/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2019/12/02/sunrise-and-sunset-in-perl.html \ No newline at end of file
diff --git a/challenge-037/dave-jacoby/perl5/ch-1.pl b/challenge-037/dave-jacoby/perl5/ch-1.pl
new file mode 100644
index 0000000000..6819061228
--- /dev/null
+++ b/challenge-037/dave-jacoby/perl5/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw{ postderef say signatures state switch };
+no warnings
+ qw{ experimental::postderef experimental::smartmatch experimental::signatures };
+
+use DateTime;
+
+my $start = DateTime->new(
+ year => 2019,
+ month => 1,
+ day => 1,
+ hour => 12,
+ minute => 0,
+ second => 0,
+ time_zone => 'floating',
+);
+
+my $day = DateTime::Duration->new( days => 1 );
+
+my $list = {};
+my $names = {};
+
+# in DateTime, Monday is 1 and Sunday is 7, so weekdays are 1-5,
+# or < 6.
+while ( $start->year == 2019 ) {
+ if ( $start->day_of_week < 6 ) { $list->{ $start->month }++ }
+ $names->{ $start->month } //= $start->month_abbr;
+ $start->add($day);
+}
+
+for my $m ( 1 .. 12 ) {
+ my $month = $names->{$m};
+ my $c = $list->{$m};
+ say qq{$month: $c days};
+}
diff --git a/challenge-037/dave-jacoby/perl5/ch-2.pl b/challenge-037/dave-jacoby/perl5/ch-2.pl
new file mode 100644
index 0000000000..32ec3b5b3a
--- /dev/null
+++ b/challenge-037/dave-jacoby/perl5/ch-2.pl
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw{ postderef say signatures state switch };
+no warnings
+ qw{ experimental::postderef experimental::smartmatch experimental::signatures };
+
+use DateTime;
+use DateTime::Duration;
+use DateTime::Event::Sunrise;
+use List::Util qw{sum};
+
+# The problem for this week is determing the gain/loss of sunlight between
+# November 2019 and December 2019. Links were given to provide sunrise
+# and sunset, but Perl already gives us that info, with DateTime::Event::Sunrise,
+# with Google giving us the location of London.
+
+my $london_lat = 51.509865;
+my $london_lon = -0.118092;
+my $dtes = DateTime::Event::Sunrise->new(
+ latitude => $london_lat,
+ longitude => $london_lon,
+);
+
+# here we create a day duration so we can leap forward
+# and a starting day of Nov 1
+my $day = DateTime::Duration->new( days => 1 );
+my $start = DateTime->new(
+ year => 2019,
+ month => 11,
+ day => 1,
+ hour => 12,
+ minute => 0,
+ second => 0,
+ time_zone => 'Europe/London',
+);
+
+# This while loop gives us every day between Nov 1 and Dec 31,
+# which we handle with a while loop, collecting a list of
+# day lengths per month.
+my $months = {};
+while ( $start->year == 2019 ) {
+ my $day_length = day_length( $start, $dtes );
+ push $months->{ $start->month_name }->@*, $day_length;
+ $start->add($day);
+}
+
+# The question is to determine the daylight gain/loss
+# between these months, but the problem is that November
+# has 30 days and December has 31, so the shorter days
+# would be offset by the extra days.
+#
+# I chose to show the difference in average day length,
+# but moving to the difference in total sunlight time
+# would simply require removing the averaging and
+# using the sum of seconds of daytime
+for my $m ( 'November', 'December' ) {
+ my $sum = sum $months->{$m}->@*;
+ my $days = scalar $months->{$m}->@*;
+ my $average = int $sum / $days;
+ my ( $hours, $minutes ) = seconds_to_hours($average);
+ say qq{$m: $hours hours, $minutes minutes};
+}
+
+exit;
+
+# Here we return the number of hours and minutes
+# that a certain number of seconds contains. Could
+# possibly be done better with DateTime::Duration
+sub seconds_to_hours ( $seconds ) {
+ my $hours = int $seconds / 3600;
+ my $rem = $seconds % 3600;
+ my $minutes = int $rem / 60;
+ return $hours, $minutes;
+}
+
+# $dtes is a location to work with, and $day is a DateTime object
+# which uses the date but not the time parts to determine sunrise
+# and sunset as DateTime objects, which we can export in epoch
+# format to get the number of seconds since the Unix epoch.
+# Sunset - Sunrise gives us the number of second in a day.
+sub day_length ( $day, $dtes ) {
+ my $sunrise = $dtes->sunrise_datetime($day);
+ my $sunset = $dtes->sunset_datetime($day);
+ return $sunset->epoch - $sunrise->epoch;
+}