adamcrussell

Weekdays and Daytimes

This week's Perl Weekly Challenge dealt with calculations related to days of the week (part 1) and times (part 2). 

Part 1 was related enough to some previous challenges (in particular Challenge 019) where I was able to re-use code which implemented Sakamoto's algorithm. 

Part 2 seemed to put me in a situation where I made an exception to my rule of not using CPAN modules, at least not for the core part of the challenge. My solution did some screen scraping of a web page and then a time calculation. Neither of those things seemed worth the extraordinary effort of re-inventing!

Part 1

Here we are asked to Write a script to calculate the total number of weekdays (Mon-Fri) in each month of the year 2019. The code which implements Sakamoto's algorithm and returns the counts is below. For the sake of saving space I left out some code which is mostly just use constant declarations.

ch-1.pl
ch-1.pl

Sample Run 

$ perl perl5/ch-1.pl 

January: 23 days
February: 20 days
March: 21 days
April: 22 days
May: 23 days
June: 20 days
July: 23 days
August: 22 days
September: 21 days
October: 23 days
November: 21 days
December: 22 days

What I did

This re-uses code from Challenge 019 and Challenge 013 which asked for similar calculations which were done with Sakamoto's algorithm. I don't think I have anything new to say this time around but see those two earlier challenges for details.

Part 2

In Part 2 we are asked to 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. A website is given which contains some data for this calculation. My approach is to grab the data from the given sites with LWP and then perform the necessary calculation with DateTime::Duration

ch-2.pl
ch-2.pl

Sample Run

$ perl perl5/ch-2.pl 

loss of 19 hours 48 minutes 72 seconds

What I did

Using LWP I fetch the contents of the page which contains a table (shown below) with the values (Daylength) we are interested in. The table is parsed with HTML::TableExtract and the values are computed with DateTime::Duration.

Sun in London. From https://www.timeanddate.com/sun/uk/london?month=11&year=2019.
Sun in London. From https://www.timeanddate.com/sun/uk/london?month=11&year=2019.





Comments for this post were locked by the author