Part 3 – Santa Takes a Break
Prologue
A Christmas ditty sung to the tune of Santa Claus is Coming to Town:
He’s making a list, | |
He’s checking it closely, | |
He’s gonna find out who’s Rakuing mostly, | |
Santa Claus is Rakuing along. |
Santa Claus Operations Update 3, 2021
Santa was tired. He wanted to brag about his new reporting and analysis tools as he had planned, but he knew he had to rest up for his big day coming up soon (sorry, my friend Juan Merelo, for the quick shift!).
He was sure that boys and girls around the world would be anxiously awaiting his visit, but he also realized he was just a stand-in for the real gift of Christmas annually celebrated during the Christian Advent season. He knew the First Sunday of Advent was related to Christmas in a way that varied year-to-year somewhat like Easter, which needs a complex algorithm to calculate its actual calendar date, but more regular with just a simple rule to follow. (Actually, he knew there are at least three rules that could be applied, but each rule delivering the correct result.)
He thought it would be relaxing to see how Raku’s powerful, built-in Date system could be applied to the task.
He started by writing down the three rules he knew:
- Find the Sunday closest to November 30 (The Feast of St. Andrew), either before or after. If November 30th is a Sunday, then that’s First Advent and St. Andrew gets moved.
- Find the Sunday following the last Thursday in November.
- Find the 4th Sunday before Christmas, not counting the Sunday which may be Christmas.
“Those look pretty easy to implement,” he thought to himself, “let’s see what I can do without calling in the experts over in IT!
“The Date object should make the job easy enough. Let’s try the first method.
my $d = Date.new($y, 11, 30); # Feast of St. Andrew | |
my $dow = $d.day-of-week; # 1..7 (Mon..Sun) | |
# sun mon tue wed thu fri sat sun | |
# 7 1 2 3 4 5 6 7 | |
# 0 1 2 3 -3 -2 -1 0 | |
if $dow == 7 { | |
# bingo! | |
return $d | |
} | |
elsif $dow < 4 { | |
# closest to previous Sunday | |
return $d – $dow | |
} | |
else { | |
# closest to following Sunday | |
return $d + (7 – $dow) | |
} |
“Now the second method.
my $d = Date.new($y, 11, 30); # last day of November | |
my $dow = $d.day-of-week; | |
while $dow != 4 { | |
$d -= 1; | |
$dow = $d.day-of-week; | |
} | |
# found last Thursday in November | |
# following Sunday is 3 days hence | |
$d += 3 |
“And finally, the third method.
my $d = Date.new($y, 12, 25); # Christmas | |
my $dow = $d.day-of-week; | |
if $dow == 7 { | |
# Christmas is on Sunday, count 28 days back. | |
return $d – 28 | |
} | |
else { | |
# find prev Sunday, count 21 days back from that | |
# sun mon tue wed thu fri sat sun | |
# 7 1 2 3 4 5 6 7 | |
# 0 1 2 3 -3 -2 -1 0 | |
return $d – $dow – 21 | |
} |
“Which method should I choose? They all work properly as I know from running each against a set of data collected from several sources. I know, I should choose which one is fastest since it will probably be part of a calendar creation module someday!
“If I were really serious I would run them using Raku’s Telemetry
class, but I’ll leave that to the experts. But, what I can do is run each over many iterations and measure elapsed time using the Raku GNU::Time
module, then compare results and then judge the best.
Santa started to work on speed testing but soon discovered the author of GNU::Time
didn’t give a good example of how to use it for this situation and he didn’t have time to experiment any more–he knew from experience that programming in Raku is addictive (just ask Mrs. Claus!), and he couldn’t take a chance on being late for his date for Christmas. So he did the next best thing: he filed an issue with GNU::Time
.
He concluded that, since he couldn’t easily determine a clear winner, he would select method 3 since it seemed to him to be the most elegant of the three, and the simplest. After all, TIMTOWTDI!
Summary
Raku has numerous capabilities that endear it to even novice programmers, but for those who have to pay attention to time and dates, its Date
and DateTime
classes take the Christmas Pudding!
Note: See new Raku modules Date::Christian::Advent
and Date::Easter
. Both use App::Mi6
for management and use the new, and recommended, Zef module repository.
Santa’s Epilogue
Don’t forget the “reason for the season:” ✝
As I always end these jottings, in the words of Charles Dickens’ Tiny Tim, “may God bless Us , Every one!” [1]
Footnotes
- A Christmas Carol, a short story by Charles Dickens (1812-1870), a well-known and popular Victorian author whose many works include The Pickwick Papers, Oliver Twist, David Copperfield, Bleak House, Great Expectations, and A Tale of Two Cities.
3 thoughts on “Day 21 – Santa Claus is Rakuing Along”