Re[2]: [Tutor] help with dates

Andy Dulson Andy Dulson <lists@discorice.org>
Tue Jul 29 18:42:05 2003


Tuesday, July 29, 2003, Karl Pflästerer wrote:

KP> Then you need a formula to compute leap years;

I'm a complete newbie when it comes to Python, but thought this might
be of interest. When you have your function to compute leap years, the
following formula might be useful. It gives the day of the week on
which a given date falls. Let

d = the day of the month (ie 1st)

m = month (starting with 1 for March, 2 for April,... and 12 for
February)

c = century (first two digits of the year)

y = the year in the century (the last two digits of the year)

L = 1 if the year is leap year, 0 otherwise

w = the day of the week (Sunday = 0, Monday = 1, ... Saturday = 6)

Then:

w = d + [2.6m-0.2] + y + [y/4] -2c + [c/4] - (L+1)[m/11] (mod 7)

[x] represents the greatest integer function, the largest n such that
n <= x, ie math.floor(x)

This is copied virtually verbatim from my number theory lecture notes.
The proof was an optional exercise so I, um, don't have it to hand.
They give a nice example to shows it works though. It works for dates
from 1582 onwards, when the Gregorian calendar was adopted,
apparently.

Hope this is useful, or at the very least interesting.

Andy