Thanks to this piece on wired I learnt about conway's doomsday algorithm to get the day of the week of any date.
Trying to wrap my head around the algorithm I decided to implement it as a learning exercise.
Here is the code, enjoy :D
Update, now with use strict!
#!/usr/bin/perl use 5.14.2; use warnings; use strict; use autodie; if(!defined($ARGV[0])){ die "gimme a date DD/MM/YYYY!\n"; } my $date = $ARGV[0]; my($day,$month,$year); if(!($date =~ m#(?<day>\d\d)/(?<month>\d\d)/(?<year>\d\d\d\d)#)){ print "$date did not match format DD/MM/YYYY\n"; die; } else{ $day = $+{day}; $month = $+{month}; $year = $+{year}; } my $isleap; if( $year =~ m/\d\d00/){ my $century = $year / 100; $isleap = $century % 4 == 0; } elsif($year =~ m/\d\d(?<decade>\d\d)/){ $isleap = $+{decade} % 4 == 0; } if(!$isleap && $month == 02 && $day == 29){ die "this ain't a leap year so $date is wrong!\n"; } my $century_anchor_day = ((5 * (($year/100)%4))%7); my $decade; if($year =~ m/\d\d(?<decade>\d\d)/){ $decade = $+{decade}; } if($decade % 2 == 0){ $decade /= 2; } else{ $decade += 11; $decade /=2; } if($decade %2 == 0){ $decade %= 7; } else{ $decade += 11; $decade %= 7; } my $anchorday_drift = 7- $decade; my @anchorday_week; my $year_anchor_day = ($century_anchor_day+$anchorday_drift)%7; given ($year_anchor_day){ when(0){@anchorday_week = qw(tuesday wednesday thursday friday sat +urday sunday monday );} when(1){@anchorday_week = qw(wednesday thursday friday saturday su +nday monday tuesday);} when(2){@anchorday_week=qw(thursday friday saturday sunday monday +tuesday wednesday);} when(3){@anchorday_week=qw(friday saturday sunday monday tuesday w +ednesday thursday);} when(4){@anchorday_week=qw(saturday sunday monday tuesday wednesda +y thursday friday);} when(5){@anchorday_week=qw(sunday sunday monday tuesday wednesday +friday saturday);} when(6){@anchorday_week = qw(monday tuesday wednesday thursday fri +day saturday sunday);} } my $doomsdates = { '01'=>3, '02'=>28, '03'=>0, '04'=>4, '05'=>9, '06'=>6, '07'=>11, '08'=>8, '09'=>5, '10'=>10, '11'=>7, '12'=>12, }; if($isleap){ $doomsdates->{'01'} = 4; $doomsdates->{'02'} = 29; } my $diff_to_doomsdates = $day - $doomsdates->{"$month"}; if($diff_to_doomsdates < 0){ $diff_to_doomsdates *= -1; } my $nbdays = $diff_to_doomsdates % 7; print "$ARGV[0] was a $anchorday_week[$nbdays]\n";</readmore>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Doomsday algorithm
by u65 (Chaplain) on Sep 06, 2015 at 14:22 UTC | |
by QuillMeantTen (Friar) on Sep 06, 2015 at 14:50 UTC | |
|
Re: Doomsday algorithm
by soonix (Chancellor) on Sep 07, 2015 at 13:56 UTC | |
by QM (Parson) on Sep 07, 2015 at 14:55 UTC | |
by soonix (Chancellor) on Sep 07, 2015 at 15:19 UTC | |
by QuillMeantTen (Friar) on Sep 08, 2015 at 07:31 UTC | |
|
Re: Doomsday algorithm
by QuillMeantTen (Friar) on Sep 06, 2015 at 14:49 UTC | |
by Athanasius (Archbishop) on Sep 06, 2015 at 16:45 UTC | |
by QuillMeantTen (Friar) on Sep 06, 2015 at 21:15 UTC | |
by u65 (Chaplain) on Sep 06, 2015 at 16:11 UTC | |
by QuillMeantTen (Friar) on Sep 06, 2015 at 21:09 UTC |