zeller.py
Beregn dag i ugen efter zeller's formel svar på http://www.experten.dk/spm/778814
Size 2.4 kB - File type text/python-sourceFile contents
#!usr/bin/python
# -*- coding: UTF-8 -*-
# File: zeller.py
"""
svar på http://www.experten/spm/778814
How do I find the day of the week for any date?
There are two popular formulas that you can use to find
the day of the week for a given date. You should be careful
when you use these formulas, though, because they only work
for the Gregorian calendar. (People in English-speaking
countries used a different calendar before September 14, 1752.)
Zeller's Rule
The following formula is named Zeller's Rule after a
Reverend Zeller. [x] means the greatest integer
that is smaller than or equal to x. You can find this
number by just dropping everything after the decimal point.
For example, [3.79] is 3. Here's the formula:
f = k + [(13*m-1)/5] + D + [D/4] + [C/4] - 2*C.
* k is the day of the month. Let's use January 29, 2064 as an example.
For this date, k = 29.
* m is the month number. Months have to be counted specially for Zeller's
Rule: March is 1, April is 2, and so on to February, which is 12.
(This makes the formula simpler, because on leap years February 29
is counted as the last day of the year.) Because of this rule,
January and February are always counted as the 11th and 12th months
of the previous year. In our example, m = 11.
* D is the last two digits of the year. Because of the month numbering,
D = 63 in our example, even though we are using a date from 2064.
* C stands for century: it's the first two digits of the year.
In our case, C = 20.
"""
def day_in_week( day, month, year ):
"""
calculate day in week according to Zeller's rule
"""
# operator // giver floor som resultat.
# k day of the month
# m is month
# D is year modulus 100
# C is year div 100
k = day
m = (month + 9) % 12 + 1
if m > 10:
D = (year % 100) - 1
else:
D = (year % 100)
C = year / 100
print k,m,C,D
f = k + ((13*m-1)//5) + D + D//4 + C//4 - 2*C
res = f % 7 # Modulo 7
if res >= 0:
return res
else:
return res + 7
if __name__ == "__main__":
import datetime
dato = datetime.datetime.now()
day, month, year = dato.day, dato.month, dato.year
print day_in_week(day, month, year)
print datetime.datetime( year, month, day ).strftime("%a")
year, month, day = 2064, 11, 29
print day_in_week(day, month, year)
print datetime.datetime( year, month, day ).strftime("%a")
Click here to get the file