Date-Day
view release on metacpan or search on metacpan
subroutine called day() which takes as arguments numerical month, day, and
year. See Synopsis for example. As far as range of usage, the day()
function will return a proper day for all dates starting from year 0
through infinity. There are no restrictions. If you pass bad data to the
function, a day will still be returned, it just won't have any meaning. So
March 45, 2001 will return a day, just one with no meaning. So make sure
you pass real dates.
After initial release of this module, I decided to elaborate on the
mathematical foundations for the algorithm used. Most other day of week
modules use the dooms day algorithm. Date::Day uses a single line formula.
Not getting into too much detail, we take the fact that March 1, 0000 fell
on a Wednesday. Knowing this, we can determine the day of week March 1
will fall on in any given year y > 0. That is:
3 + y + [y/4] - [y/100] + [y/400] mod 7
[y/4] represents the integer part of (year) divided by (4). Weekdays
correspond to the following numbers:
these numbers by 1 + j(m), where j(m), for m=1,2,...,12 (with m=1 as
March, m=2 as April, and so on), is defined by:
j(m):2,5,0,3,5,1,4,6,2,4,0,3.
So we refine our table:
Month 1 2 3 4 5 6 7 8 9 10 11 12
Value 0 3 2 5 0 3 5 1 4 6 2 4
The above table is very important in our final formula. So for example,
the month June is numerical 6, and 6 goes to value 3. 3 is then used in
our final formula.
With all the above data, it follows that month m, day 1, year y, has day
of week number:
1 + j(m) + y + [y/4] - [y/100] + [y/400] mod 7
And to get a formula that handles any given day, we use the formula:
d + j(m) + y + [y/4] - [y/100] + [y/400] mod 7 (***)
So for example, we want to find the day of week corresponding to April 8,
2002. If March is month 1, then April is month 2, hence m=2. So j(2)=5.
Thus we have:
8 + 5 + 2002 + [2002/4] - [2002/100] + [2002/400] mod 7
which is, 1 mod 7. So 4/8/2002 is a Monday.
So when you look at the code portion of Day.pm you see only two important
lines of code. One line is for the associative array that implements j(m).
The other line is for implementing formula (***). Thats it.
=head1 AUTHOR
John Von Essen, john@essenz.com
=head1 SEE ALSO
perl(1).
=cut
subroutine called day() which takes as arguments numerical month, day, and
year. See Synopsis for example. As far as range of usage, the day()
function will return a proper day for all dates starting from year 0
through infinity. There are no restrictions. If you pass bad data to the
function, a day will still be returned, it just won't have any meaning. So
March 45, 2001 will return a day, just one with no meaning. So make sure
you pass real dates.
After initial release of this module, I decided to elaborate on the
mathematical foundations for the algorithm used. Most other day of week
modules use the dooms day algorithm. Date::Day uses a single line formula.
Not getting into too much detail, we take the fact that March 1, 0000 fell
on a Wednesday. Knowing this, we can determine the day of week March 1
will fall on in any given year y > 0. That is:
3 + y + [y/4] - [y/100] + [y/400] mod 7
[y/4] represents the integer part of (year) divided by (4). Weekdays
correspond to the following numbers:
these numbers by 1 + j(m), where j(m), for m=1,2,...,12 (with m=1 as
March, m=2 as April, and so on), is defined by:
j(m):2,5,0,3,5,1,4,6,2,4,0,3.
So we refine our table:
Month 1 2 3 4 5 6 7 8 9 10 11 12
Value 0 3 2 5 0 3 5 1 4 6 2 4
The above table is very important in our final formula. So for example,
the month June is numerical 6, and 6 goes to value 3. 3 is then used in
our final formula.
With all the above data, it follows that month m, day 1, year y, has day
of week number:
1 + j(m) + y + [y/4] - [y/100] + [y/400] mod 7
And to get a formula that handles any given day, we use the formula:
d + j(m) + y + [y/4] - [y/100] + [y/400] mod 7 (***)
So for example, we want to find the day of week corresponding to April 8,
2002. If March is month 1, then April is month 2, hence m=2. So j(2)=5.
Thus we have:
8 + 5 + 2002 + [2002/4] - [2002/100] + [2002/400] mod 7
which is, 1 mod 7. So 4/8/2002 is a Monday.
So when you look at the code portion of Day.pm you see only two important
lines of code. One line is for the associative array that implements j(m).
The other line is for implementing formula (***). Thats it.
AUTHOR
John Von Essen, john@essenz.com
( run in 0.251 second using v1.01-cache-2.11-cpan-26ccb49234f )