Created
December 8, 2019 16:55
-
-
Save adamcrussell/d4b00d6458d0d7eb039155e340542ee6 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 037
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
## | |
# Write a script to calculate the total number of | |
# weekdays (Monday-Friday) in each month of the year 2019. | |
## | |
use constant YEAR => 2019; | |
use constant JANUARY => 1; | |
use constant FEBRUARY => 2; | |
use constant MARCH => 3; | |
use constant APRIL => 4; | |
use constant MAY => 5; | |
use constant JUNE => 6; | |
use constant JULY => 7; | |
use constant AUGUST => 8; | |
use constant SEPTEMBER => 9; | |
use constant OCTOBER => 10; | |
use constant NOVEMBER => 11; | |
use constant DECEMBER => 12; | |
use constant SATURDAY => 6; | |
use constant SUNDAY => 0; | |
sub month_name{ | |
my($m)= @_; | |
return "January" if $m == 1; | |
return "February" if $m == 2; | |
return "March" if $m == 3; | |
return "April" if $m == 4; | |
return "May" if $m == 5; | |
return "June" if $m == 6; | |
return "July" if $m == 7; | |
return "August" if $m == 8; | |
return "September" if $m == 9; | |
return "October" if $m == 10; | |
return "November" if $m == 11; | |
return "December" if $m == 12; | |
} | |
sub day_of_week{ | |
my($year, $month, $day) = @_; | |
my @month_value = (0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4); | |
$year-- if($month == JANUARY || $month == FEBRUARY); | |
return ($year + int($year/4) - int($year/100) + int($year/400) + $month_value[$month-1] + $day) % 7 | |
} | |
MAIN:{ | |
my $weekdays; | |
my $is_leap_year = (((YEAR % 4) == 0) && ((YEAR % 100) != 0)) || ((YEAR % 4) == 0 && (YEAR % 400) == 0); | |
for my $month (JANUARY..DECEMBER){ | |
for my $day (1 .. 31){ | |
last if($month == FEBRUARY && $day > 28 && !$is_leap_year); | |
last if($month == FEBRUARY && $day > 29 && $is_leap_year); | |
last if(($month == APRIL || $month == JUNE || $month == SEPTEMBER || $month == NOVEMBER) && $day > 30); | |
my $dow = day_of_week(YEAR, $month, $day); | |
if($dow != SATURDAY && $dow != SUNDAY){ | |
$weekdays++; | |
} | |
} | |
print month_name($month) . ": $weekdays days\n"; | |
$weekdays = 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
## | |
# Write a script to find out the DayLight gain/loss | |
# in the month of December 2019 as compared to | |
# November 2019 in the city of London. | |
## | |
use LWP::UserAgent; | |
use HTML::TableExtract; | |
use DateTime::Duration; | |
use constant NOVEMBER_URL => "https://www.timeanddate.com/sun/uk/london?month=11&year=2019"; | |
use constant DECEMBER_URL => "https://www.timeanddate.com/sun/uk/london?month=12&year=2019"; | |
sub get_daytime{ | |
my ($url) = @_; | |
my $daytime = new DateTime::Duration; | |
my $ua = new LWP::UserAgent( | |
ssl_opts => {verify_hostname => 0} | |
); | |
my $response = $ua->get($url); | |
my $parser = new HTML::TableExtract(headers => [qw/Daylength/]); | |
$parser->parse($response->decoded_content); | |
for my $t ($parser ->tables){ | |
for my $row ($t->rows){ | |
if($row->[0] && $row->[0]=~m/[0-9]/){ | |
my @fields = split(/:/, $row->[0]); | |
my $d = new DateTime::Duration( | |
hours => $fields[0], | |
minutes => $fields[1], | |
seconds => $fields[2] | |
); | |
$daytime->add_duration($d); | |
} | |
} | |
} | |
return $daytime; | |
} | |
MAIN:{ | |
my $november_daytime = get_daytime(NOVEMBER_URL); | |
my $december_daytime = get_daytime(DECEMBER_URL); | |
my $delta = $december_daytime - $november_daytime; | |
unless($delta->is_zero){ | |
print "loss of " if DateTime::Duration->compare($december_daytime, $november_daytime) < 0; | |
print "gain of " if DateTime::Duration->compare($december_daytime, $november_daytime) > 0; | |
print $delta->hours() . " hours "; | |
print $delta->minutes() . " minutes "; | |
print $delta->seconds() . " seconds\n"; | |
} | |
else{ | |
print "no change.\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment