Skip to content

Commit 4c2425a

Browse files
committed
date_to_weekday: new implementation replaces buggy original
1 parent 4bbe7ab commit 4c2425a

File tree

1 file changed

+131
-15
lines changed

1 file changed

+131
-15
lines changed

other/date_to_weekday.py

Lines changed: 131 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,144 @@
33
from calendar import day_name
44
from datetime import datetime
55

6+
"""
7+
Family of functions for calculating the day of week name from a date
8+
"""
69

7-
def date_to_weekday(inp_date: str) -> str:
10+
11+
def day_of_week(datetime_obj: datetime) -> str:
812
"""
9-
It returns the day name of the given date string.
10-
:param inp_date:
13+
Calculates the day name of the given datetime object
14+
:param datetime_obj:
1115
:return: String
12-
>>> date_to_weekday("7/8/2035")
16+
>>> day_of_week(datetime(year=2035, month=8, day=7))
1317
'Tuesday'
14-
>>> date_to_weekday("7/8/2021")
18+
>>> day_of_week(datetime(year=2021, month=8, day=7))
1519
'Saturday'
16-
>>> date_to_weekday("1/1/2021")
20+
>>> day_of_week(datetime(year=2021, month=8, day=1))
21+
'Sunday'
22+
>>> day_of_week(datetime(year=2021, month=1, day=1))
1723
'Friday'
24+
>>> day_of_week(datetime(year=2021, month=1, day=31))
25+
'Sunday'
26+
"""
27+
day_of_week: int = datetime_obj.weekday() # Monday = 0 .. Sunday = 6
28+
return day_name[day_of_week]
29+
30+
31+
def day_of_week_fmt(date_string: str, format: str = "%Y%m%d") -> str:
1832
"""
19-
year: int | str
20-
day, month, year = (int(x) for x in inp_date.split("/"))
21-
if year % 100 == 0:
22-
year = "00"
23-
new_base_date: str = f"{day}/{month}/{year%100} 0:0:0"
24-
date_time_obj: datetime = datetime.strptime(new_base_date, "%d/%m/%y %H:%M:%S")
25-
out_put_day: int = date_time_obj.weekday()
26-
return day_name[out_put_day]
33+
Calculates the day name of the given date string decoded by format.
34+
35+
See time.strptime for format parameters.
36+
37+
:param date_string: date in dd/mm/yyyy format
38+
:param format: strptime compatible date format
39+
:return: String
40+
41+
>>> day_of_week_fmt("20211026")
42+
'Tuesday'
43+
>>> day_of_week_fmt("2021/10/26", "%Y/%m/%d")
44+
'Tuesday'
45+
>>> day_of_week_fmt("2021-10-26", "%Y-%m-%d")
46+
'Tuesday'
47+
>>> day_of_week_fmt("7/8/2021", "%d/%m/%Y")
48+
'Saturday'
49+
>>> day_of_week_fmt("7/8/2021", "%m/%d/%Y")
50+
'Thursday'
51+
"""
52+
datetime_obj = datetime.strptime(date_string, format)
53+
return day_of_week(datetime_obj)
54+
55+
56+
def day_of_week_dmy(date_string: str) -> str:
57+
"""
58+
Calculates the day name of the given date string in day/month/year format
59+
60+
:param datestring: date in dd/mm/yyyy format
61+
:return: String
62+
>>> day_of_week_dmy("7/8/2035")
63+
'Tuesday'
64+
>>> day_of_week_dmy("7/8/2021")
65+
'Saturday'
66+
>>> day_of_week_dmy("7/08/2021")
67+
'Saturday'
68+
>>> day_of_week_dmy("07/8/2021")
69+
'Saturday'
70+
>>> day_of_week_dmy("07/08/2021")
71+
'Saturday'
72+
>>> day_of_week_dmy("1/1/2021")
73+
'Friday'
74+
>>> day_of_week_dmy("31/1/2021")
75+
'Sunday'
76+
"""
77+
return day_of_week_fmt(date_string, format="%d/%m/%Y")
78+
79+
80+
def day_of_week_mdy(date_string: str) -> str:
81+
"""
82+
Calculates the day name of the given date string.
83+
:param date_mdy: date in mm/dd/yyyy format
84+
:return: String
85+
>>> day_of_week_mdy("8/7/2035")
86+
'Tuesday'
87+
>>> day_of_week_mdy("8/7/2021")
88+
'Saturday'
89+
>>> day_of_week_mdy("8/07/2021")
90+
'Saturday'
91+
>>> day_of_week_mdy("1/31/2021")
92+
'Sunday'
93+
>>> day_of_week_mdy("01/31/2021")
94+
'Sunday'
95+
"""
96+
return day_of_week_fmt(date_string, format="%m/%d/%Y")
97+
98+
99+
def day_of_week_ymd(date_string: str) -> str:
100+
"""
101+
>>> day_of_week_ymd("2035/07/08")
102+
'Sunday'
103+
>>> day_of_week_ymd("2021/7/8")
104+
'Thursday'
105+
>>> day_of_week_ymd("2021/1/1")
106+
'Friday'
107+
"""
108+
return day_of_week_fmt(date_string, format="%Y/%m/%d")
27109

28110

29111
if __name__ == "__main__":
30-
print(date_to_weekday("1/1/2021"), end=" ")
112+
print("Tuesday August 7, 2035:\n ",
113+
day_of_week_fmt("7/8/2035", format="%d/%m/%Y"),
114+
day_of_week_fmt("8/07/2035", format="%m/%d/%Y"),
115+
day_of_week_fmt("2035-08-07", format="%Y-%m-%d"),
116+
day_of_week_fmt("20350807"),
117+
day_of_week_dmy("7/8/2035"),
118+
day_of_week_mdy("8/7/2035"),
119+
day_of_week_ymd("2035/08/07"))
120+
121+
print("Sunday August 7, 2021:\n ",
122+
day_of_week_fmt("7/8/2021", format="%d/%m/%Y"),
123+
day_of_week_fmt("8/07/2021", format="%m/%d/%Y"),
124+
day_of_week_fmt("2021-08-07", format="%Y-%m-%d"),
125+
day_of_week_fmt("20210807"),
126+
day_of_week_dmy("7/8/2021"),
127+
day_of_week_mdy("8/7/2021"),
128+
day_of_week_ymd("2021/08/07"))
129+
130+
print("Friday January 1, 2021:\n ",
131+
day_of_week_fmt("1/1/2021", format="%d/%m/%Y"),
132+
day_of_week_fmt("1/1/2021", format="%m/%d/%Y"),
133+
day_of_week_fmt("2021-01-01", format="%Y-%m-%d"),
134+
day_of_week_fmt("20210101"),
135+
day_of_week_dmy("1/1/2021"),
136+
day_of_week_mdy("1/1/2021"),
137+
day_of_week_ymd("2021/01/1"))
138+
139+
print("Friday October 1, 2021:\n ",
140+
day_of_week_fmt("1/10/2021", format="%d/%m/%Y"),
141+
day_of_week_fmt("10/1/2021", format="%m/%d/%Y"),
142+
day_of_week_fmt("2021-10-01", format="%Y-%m-%d"),
143+
day_of_week_fmt("20211001"),
144+
day_of_week_dmy("1/10/2021"),
145+
day_of_week_mdy("10/1/2021"),
146+
day_of_week_ymd("2021/10/01"))

0 commit comments

Comments
 (0)