# File name: doomsday.py
# Copyright: The AustSTEM Foundation Limited
# Author: Tony Strasser corrected from unattributed code on codepad.org
# Date created: 22 Sep 2018
# Date last modified: 1 January 2019
# MicroPython Version: 1.9.4 for the Kookaberry
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
# To the fullest extent permitted by law, AustSTEM absolutely disclaims
# all warranties, expressed or implied, including, but not limited to,
# implied warranties of merchantability and fitness for any particular purpose.
# AustSTEM gives no warranty that this software will be free of errors,
# or that defects in the software will be corrected,
# See the GNU General Public License for more details.
#
# A python implementation of Conway's Doomsday algorithm
# Used to work out the day of the week given a date.
# To use call calc_dow(day, month, year)
# Begin code-
def set_century(year):
start_days = (2, 0, 5, 3)
c = int(year/100)
x = int(c % 4)
return start_days[x]
def set_year(year):
y = int(year % 100)
y12 = int(y/12)
yr = int(y % 12)
yr4 = int(yr/4)
return int((y12+yr+yr4) % 7)
def set_month(month, year):
if month % 2:
if month == 1 :
if year % 4 == 0:
mday = 4
else:
mday = 3
else:
if month in (9, 11):
mday = month - 4
else:
mday = month + 4
else:
if month == 2:
if year % 4 == 0:
mday = 29
else:
mday = 28
else:
mday = month
return mday
def day_offset(month, day, year):
mday = set_month(month, year)
offset = (day-mday) % 7
return offset
def calc_dow(day, month, year):
days = ('Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
c = set_century(year)
y = set_year(year)
m = day_offset(month, day, year)
dow = days[int((c+y+m) % 7)]
return dow
def dow_index(day, month, year):
# Calculates index for day of week 0-6 is Sunday to Saturday
c = set_century(year)
y = set_year(year)
m = day_offset(month, day, year)
return int((c+y+m) % 7)