Created
May 22, 2013 22:17
-
-
Save danielwatson6/5631388 to your computer and use it in GitHub Desktop.
Calculate the weekday of any given date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate the weekday of any given date | |
# Author: djwatt5 | |
# We use the floor method from the math module | |
# to calculate the year offset. | |
import math | |
# Format: day: 1-31, month: 1-12, year: 1- | |
# String option: if not specified, the result | |
# will be an integer (Sunday: 0, Monday: 1, ..., | |
# Saturday: 6). If specified, the result will be | |
# the weekday as a string. To calculate the weekday, | |
# we look for century, year, month, and day offsets | |
# and then add them up to get the weekday integer. | |
def weekday(day, month, year, string = False): | |
# Month offset: | |
# To calculate a month's offset, you must add | |
# the previous month's offset to a number. | |
# This number = 3 if the previous month has 31 days, | |
# 2 if it has 30 days, and 0 if it's february. | |
# Finally, apply modulo 7 to the result. | |
# Start from january offset (0). | |
month_offset = [0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5] | |
# Check if it's a leap year: | |
# Leap years always are multiples of 400. If they | |
# are not multiples of 400, then they must be | |
# multiples of 4 but not 100. We add the missing day | |
# to the day offset when the leap year needs to be | |
# added; that is, when it's a leap year and it's | |
# already march. | |
leap = 0 | |
if month > 2: | |
if year % 400 == 0: | |
leap = 1 | |
elif year % 100 == 0: | |
leap = 0 | |
elif year % 4 == 0: | |
leap = 1 | |
# To calculate year and century offsets, we separate | |
# the last two numbers from the year to get the year | |
# from 0 to 99, and the century is the rest of the | |
# string. Notice that the first two parts of the if | |
# statement handle exceptions; years with two or just | |
# one digit. This must be done, since python can't | |
# assign empty values to numbers. | |
year = str(abs(year)) | |
if len(year) == 2: | |
century = 0 | |
year = int(year) | |
elif len(year) == 1: | |
century = 0 | |
year = int(year) | |
else: | |
century = int(year[:-2]) | |
year = int(year[-2:]) | |
# Calculating the offsets: | |
# The day offset is the day modulo 7. We add the leap | |
# day if it's missing. The month offset is the one from | |
# the array at the top. The year is itself plus the floor | |
# of one fourth of itself. The century is the nearest multiple | |
# of 4 minus 1, minus the year, and then the double. The last | |
# two are casted to int to see an integer result printed. | |
a = (day + leap) % 7 | |
b = month_offset[month - 1] | |
c = int(year + math.floor(year / 4)) % 7 | |
d = int((39 - century) % 4) * 2 | |
# If the string option is passed, the corresponding weekday | |
# will be selected. If not, we pass directly to the result. | |
# The result is the sum of the offsets, then passed to | |
# modulo seven to get a real weekday. | |
if string: | |
weekday = {0: "Sunday", | |
1: "Monday", | |
2: "Tuesday", | |
3: "Wednesday", | |
4: "Thursday", | |
5: "Friday", | |
6: "Saturday"} | |
return weekday[(a + b + c + d) % 7] | |
return (a + b + c + d) % 7 | |
# Test: | |
print weekday(22, 5, 2013, string = True) # >>> Wednesday |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment