-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_of_week.py
More file actions
83 lines (69 loc) · 3.13 KB
/
day_of_week.py
File metadata and controls
83 lines (69 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Dow:
"""
Calculation of the day of the week (DOW) for a given date
taking into account the change of the Julian calendar
to the Gregorian calendar introduced on October 15, 1582.
The date must be in YYYY-MM-DD format (ISO 8601).
The main formula is as follows:
(Year Code + Month Code + Century Code + Day - Leap Year Code) mod 7
"""
def __init__(self, date: str):
self.date: str = date
self.dateList: list = date.split('-')
self.year: int = int(self.dateList[0])
self.month: int = int(self.dateList[1])
self.day: int = int(self.dateList[2])
self.isGregorian: bool = self.is_gregorian()
self.yearNum: int = self.year % 100
self.yearCode: int = self.get_year_code()
self.century: int = self.year // 100
self.centuryCode: int = self.get_century_code()
self.isLeap: bool = self.is_leap_year()
self.leapCode: int = 1 if self.isLeap else 0
self.monthName: str = self.get_month_name()
self.monthCode: int = self.get_month_code()
self.dowCode: int = self.get_dow_code()
self.dow: str = self.get_dow()
self.dateLong: str = self.get_date_long()
self.info: str = self.get_info()
def is_gregorian(self) -> bool:
if (self.year > 1582) \
or (self.year == 1582 and self.month > 10) \
or (self.year == 1582 and self.month == 10 and self.day >= 15):
return True
else:
return False
def get_year_code(self) -> int:
return (self.yearNum + self.yearNum // 4) % 7
def get_month_code(self) -> int:
month_string = "033614625035"
return int(month_string[self.month-1:self.month])
def get_month_name(self) -> str:
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
return months[self.month-1]
def get_century_code(self) -> int:
century_string = "206420642064"
if self.isGregorian:
return int(century_string[self.century-14:self.century-13])
else:
return (11 - self.century) % 7
def is_leap_year(self) -> bool:
if (self.isGregorian and (self.year % 4 == 0 and self.year % 100 != 0) or (self.year % 400 == 0)) \
or (not self.isGregorian and (self.year % 4 == 0)):
return True
else:
return False
def get_dow_code(self) -> int:
return (self.yearCode + self.monthCode + self.centuryCode + self.day - self.leapCode) % 7
def get_dow(self) -> str:
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
return days[self.dowCode]
def get_date_long(self) -> str:
return (f"{self.day} of {self.monthName} {self.year} is {self.dow} "
f"({'Gregorian' if self.isGregorian else 'Julian'})")
def get_info(self) -> str:
return (f"codes: year = {self.yearCode}, month = {self.monthCode}, "
f"century = {self.centuryCode}, leap = {self.leapCode}")
a = Dow("1410-07-15")
print(a.dateLong)