Skip to content

Instantly share code, notes, and snippets.

@gmcclure
Created November 12, 2012 00:43
Show Gist options
  • Save gmcclure/4056946 to your computer and use it in GitHub Desktop.
Save gmcclure/4056946 to your computer and use it in GitHub Desktop.
Python routine for generating a 12-digit code that assists with determining the day of the week for any given date
#!/usr/bin/env python
import datetime, sys
try:
year = int(sys.argv[1])
except IndexError:
year = datetime.datetime.today().year
firstDayToFirstMonday = ['1st', '7th', '6th', '5th', '4th', '3rd', '2nd']
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
summary = ''
for month in range(12):
firstOfMonth = datetime.datetime(year, month + 1, 1).weekday()
firstMonday = firstDayToFirstMonday[firstOfMonth]
print months[month], firstMonday
summary += firstMonday[0]
print 'Summary:', '-'.join(summary[x:x+3] for x in range(0, 12, 3))
@gmcclure
Copy link
Author

This was taken from this URL:

http://blogs.fluidinfo.com/terry/2012/11/11/a-simple-way-to-calculate-the-day-of-the-week-for-any-day-of-a-given-year/

The explanation, in brief, follows:

The sequence for 2012 is 265-274-263-153. Suppose you’ve memorized the sequence and you need to know the day of the week for March 29th. You can trivially calculate that it’s a Thursday. You take the 3rd digit of the sequence (because March is the 3rd month), which is 5. That tells you that the 5th of March was a Monday. Then you just go backward or forward as many weeks and days as you need. The 5th was a Monday, so the 12th, 19th, and 26th were too, which means the 29th was a Thursday.

The memory task is made simpler by the fact that there are only 14 different possible sequences. Or, if you consider just the last 10 digits of the sequences (i.e., starting from March), there are only 7 possible sequences. There are only 14 different sequences, so if you use this method in the long term, you’ll find the effort of remembering a sequence will pay off when it re-appears. E.g., 2013 and 2019 both have sequence 744-163-152-742. There are other nice things you can learn that can also make the memorization and switching between years easier (see the Corresponding months section on the above Wikipedia page).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment