Browse code

Added the code, added the README

haywalk authored on 16/12/2021 03:31:01
Showing 2 changed files

... ...
@@ -1,2 +1,13 @@
1
-# doomsday
2
-Gets the day of the week for any date in the Gregorian calendar, using John Conway's Doomsday rule.
1
+# Conway's Doomsday Algorithm
2
+
3
+This is a little program I wrote that takes in a date in ISO 8601 format (YYYY-MM-DD) and
4
+prints out the day of the week on that date.
5
+
6
+It uses John Horton Conway's [Doomsday rule](https://en.wikipedia.org/wiki/Doomsday_rule)
7
+to find the day of the week, by first calculating the 'anchor day' for the given century,
8
+then that of the given year, then finding the given day's offset from the first doomsday of
9
+the month, which is its offset from the year's anchor day.
10
+
11
+This program is written in Python 3.9. It is licensed under the
12
+[GNU GPL, version 3](https://www.gnu.org/licenses/gpl-3.0.en.html). It was
13
+written by Hayden Walker ([www.haywalk.ca](https://www.haywalk.ca/)) on 2021-12-15. 
3 14
new file mode 100644
... ...
@@ -0,0 +1,116 @@
1
+'''
2
+doomsday.py
3
+
4
+Copyright 2021 Hayden D. Walker <planethaywalk@aol.com>
5
+
6
+This program is free software: you can redistribute it and/or modify
7
+it under the terms of the GNU General Public License as published by
8
+the Free Software Foundation, either version 3 of the License, or
9
+(at your option) any later version.
10
+
11
+This program is distributed in the hope that it will be useful,
12
+but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+GNU General Public License for more details.
15
+
16
+You should have received a copy of the GNU General Public License
17
+along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
+
19
+Conway's Doomsday Rule
20
+
21
+Given a date in the Gregorian calendar, (YYYY-MM-DD), calculate the day
22
+of the week using John Horton Conway's Doomsday Algorithm.
23
+
24
+Program by Hayden Walker (www.haywalk.ca)
25
+Written 2021-12-15
26
+'''
27
+
28
+def is_leap_year(year):
29
+	'''
30
+	Check if a year is a leap year
31
+	'''
32
+	return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
33
+
34
+def get_century_anchor(year):
35
+	'''
36
+	Return the century anchor day for a given year
37
+	'''
38
+	# Get century from year
39
+	century = year // 100
40
+	
41
+	# Calculate and return anchor day
42
+	century_anchor_day = (5 * (century % 4) % 7 + 2) % 7 - 1
43
+	return century_anchor_day
44
+
45
+def get_year_anchor(year):
46
+	'''
47
+	Return the anchor day for a given year
48
+	'''
49
+	# Get the century anchor day
50
+	century_anchor = get_century_anchor(year)
51
+	
52
+	# Get the last digits of the year
53
+	last_digits = year % 100
54
+	
55
+	# Calculate offset from century anchor day
56
+	a = last_digits // 12
57
+	b = last_digits % 12
58
+	c = b // 4
59
+	century_offset = a + b + c
60
+	
61
+	# Calculate and return the year anchor day
62
+	year_anchor = (century_anchor + century_offset) % 7
63
+	return year_anchor
64
+
65
+def get_day_number(year, month, day):
66
+	'''
67
+	Return the day of the week (0-6) for a given date
68
+	'''
69
+	# First doomsdays of each month for a common year
70
+	doomsdays_common = {1:3, 2:7, 3:7, 4:4, 5:2, 6:6, 7:4, 8:1,
71
+		9:5, 10:3, 11:7, 12:5}
72
+
73
+	# First doomsdays of each month for a leap year
74
+	doomsdays_leap = {1:4, 2:1, 3:7, 4:4, 5:2, 6:6, 7:4, 8:1,
75
+		9:5, 10:3, 11:7, 12:5}
76
+
77
+	# Get first doomsday for this month
78
+	if is_leap_year(year):
79
+		this_dd = doomsdays_leap[month]
80
+	else:
81
+		this_dd = doomsdays_common[month]
82
+
83
+	# Get the year anchor
84
+	year_anchor = get_year_anchor(year)
85
+	
86
+	# Calculate and return the day of the week based on distance from
87
+	# the first doomsday of the month
88
+	this_day = (year_anchor + (day - this_dd)) % 7
89
+	return this_day
90
+
91
+def get_day(year, month, day):
92
+	'''
93
+	For a given date, return the name of the day of the week
94
+	'''
95
+	# Get the day number
96
+	day_number = get_day_number(year, month, day)
97
+	
98
+	# Days of the week
99
+	days = ['Monday', 'Tuesday', 'Wednesday',
100
+		'Thursday', 'Friday', 'Saturday', 'Sunday']
101
+	
102
+	return days[day_number]
103
+
104
+# Get input from user
105
+user_input = input().split('-')
106
+
107
+# Get year, month, and day from input
108
+year = int(user_input[0])
109
+month = int(user_input[1])
110
+day = int(user_input[2])
111
+
112
+# Calculate day of the week
113
+weekday = get_day(year, month, day)
114
+
115
+# Print out result
116
+print(weekday)