File Coverage

blib/lib/Date/Doomsday.pm
Criterion Covered Total %
statement 16 17 94.1
branch 2 4 50.0
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 21 25 84.0


line stmt bran cond sub pod time code
1             package Date::Doomsday;
2              
3 3     3   20297 use strict;
  3         5  
  3         126  
4 3     3   16 use vars qw( @ISA @EXPORT $VERSION );
  3         4  
  3         833  
5              
6             require Exporter;
7             @ISA = qw(Exporter);
8              
9             @EXPORT = qw( doomsday );
10             $VERSION = ( qw($Revision: 1.12 $) )[1];
11              
12             =head1 NAME
13              
14             Date::Doomsday - Determine doomsday for a given year
15              
16             =head1 SYNOPSIS
17              
18             use Date::Doomsday;
19             $doomsday = doomsday(1945);
20              
21             =head1 VERSION
22              
23             $Revision: 1.12 $
24              
25             =head1 DESCRIPTION
26              
27             Doomsday is a concept invented by John Horton Conway to make it easier to
28             figure out what day of the week particular events occur in a given year.
29              
30             =head1 doomsday
31              
32             $doomsday = doomsday( 1945 );
33              
34             Returns the day of the week (in the range 0..6) of doomsday in the particular
35             year given. If no year is specified, the current year is assumed.
36              
37             =cut
38              
39             # sub doomsday {{{
40              
41             sub doomsday {
42 29     29 0 42 my $year = shift;
43              
44 29 50       72 $year = ( localtime(time) )[5] unless $year;
45              
46 29 50       65 if ($year < 1583) {
47 0         0 warn "The Gregorian calendar did not come into use until
48             1583. Your date predates the usefulness of this algorithm."
49             }
50              
51 29         52 my $century = $year - ( $year % 100 );
52              
53 29         64 my $base = ( 3, 2, 0, 5 )[ ( ($century - 1500)/100 )%4 ];
54              
55 29         52 my $twelves = int ( ( $year - $century )/12);
56 29         34 my $rem = ( $year - $century ) % 12;
57 29         38 my $fours = int ($rem/4);
58              
59 29         44 my $doomsday = $base + ($twelves + $rem + $fours)%7;
60              
61 29         90 return $doomsday % 7;
62             }#}}}
63              
64             # Docs {{{
65              
66             =head1 AUTHOR
67              
68             Rich Bowen (rbowen@rcbowen.com)
69              
70             =head1 Doomsday
71              
72             Doomsday is a simple way to find out what day of the week any event occurs, in
73             any year. It was invented by Dr John Horton Conway.
74              
75             In conjunction with Date::DayOfWeek, it can calculate the day of the
76             week for any date since the beginning of the Gregorian calendar.
77              
78             The concept of doomsday is simple: If you know this special day
79             (called "doomsday") for a given year, you can figure out the day of
80             the week for any other day that year by a few simple calculations that
81             you can do in your head, thus:
82              
83             The last day of February is doomsday. That's the 28th most years, and
84             the 29th in leap years.
85              
86             The Nth day of the Nth month is doomsday, for even values of N. That
87             is, 4/4 (April 4), 6/6, 8/8, 10/10, and 12/12, are all doomsdays.
88             (That is, if doomsday is Wednesday, as it is in 2001, then October 10
89             will also be a Wednesday.)
90              
91             For odd months, after March, the following mnemonic will help you
92             remember: "I work from 9-5 at the 7-11." (For those of you not living
93             in the USA, you might like to know that 7-11 is the name of a chain of
94             stores.) What this means is that 9/5 (September 5) and 5/9 (May 9) are
95             both doomsday. Likewise, 7/11 and 11/7 are doomsday.
96              
97             The 0th day of march is always doomsday.
98              
99             The last day of January is doomsday in most years, and the day after
100             tha last day of January (think January 32nd) is doomsday in leap
101             years.
102              
103             So, if you know the above, and you want to figure out what day of the
104             week a particular day is, you do something like the following:
105              
106             When is Christmas in 2001? Doomsday in 2001 is Wednesday. So December
107             12 is Wednesday. Count forward 2 week, and find that December 26 is a
108             Wednesday. So Christmas (December 25) is a Tuesday.
109              
110             For more information about the origins and mathematics surrounding
111             doomsday, see the following web sites:
112              
113             http://rudy.ca/doomsday.html
114              
115             http://quasar.as.utexas.edu/BillInfo/doomsday.html
116              
117             http://www.cst.cmich.edu/users/graha1sw/Pub/Doomsday/Doomsday.html
118              
119             =cut
120              
121             # }}}
122              
123             1;
124