Long ago joining tables
Weekly challenge 132 — 27 September 2021
Week 132: 27 Sep 2021
You are given a date yyyy/mm/dd
.
Assuming the given date is your date of birth, write a script to find the mirror dates of the given date.
The mirror dates are your current age before your birth date, and your current age after today.
Example 1: Input: 2021/09/18 Output: 2021/09/14, 2021/09/26 On the date you were born, someone who is your current age would have been born on 2021/09/14. Someone born today will be your current age on 2021/09/26. Example 2: Input: 1975/10/10 Output: 1929/10/27, 2067/09/05 On the date you were born, someone who is your current age would have been born on 1929/10/27. Someone born today will be your current age on 2067/09/05. Example 3: Input: 1967/02/14 Output: 1912/07/08, 2076/04/30 On the date you were born, someone who is your current age, would have been born on 1912/07/08. Someone born today will be your current age on 2076/04/30.
I did not submit a solution at the time, but have written this later.
Date calculation can be a bit of a pain in Perl, but the Date::Calc
module is a great help. All that is
needed today is to get your (or someone's) age in days, and then subtract that from their birth date and add it to today.
I've provided the answers for King Charles III, Albert Einstein and William Shakespeare. Try yours below.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2021-09-27 use utf8; # Week 132 - task 1 - Mirror dates use warnings; # Peter Campbell Smith use Date::Calc qw[Delta_Days Add_Delta_Days]; mirror_dates('1948-11-14'); # King Charles III mirror_dates('1879-03-14'); # Albert Einstein mirror_dates('1564-04-23'); # William Shakespeare sub mirror_dates { my ($born, $y, $m, $d, $y2, $m2, $d2, $gap, @t); $born = $_[0]; ($y, $m, $d) = $born =~ m|(\d+).(\d+).(\d+)|; @t = localtime; $gap = Delta_Days($y, $m, $d, $t[5] + 1900, $t[4] + 1, $t[3]); ($y, $m, $d) = Add_Delta_Days($y, $m, $d, -$gap); ($y2, $m2, $d2) = Add_Delta_Days($t[5] + 1900, $t[4] + 1, $t[3], $gap); say qq[\nInput: \$born = ] . $born; say qq[Output: ] . sprintf('%04d-%02d-%02d, %04d-%02d-%02d', $y, $m, $d, $y2, $m2, $d2); }
Input: $born = 1948-11-14 Output: 1873-01-22, 2100-06-28 Input: $born = 1879-03-14 Output: 1733-09-19, 2170-02-28 Input: $born = 1564-04-23 Output: 1103-12-10, 2485-01-18
Any content of this website which has been created by Peter Campbell Smith is in the public domain