forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_to_weekday.py
174 lines (151 loc) · 4.92 KB
/
date_to_weekday.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from __future__ import annotations
from calendar import day_name
from datetime import datetime
"""
Family of functions for calculating the day of week name from a date
"""
def day_of_week(datetime_obj: datetime) -> str:
"""
Calculates the day name of the given datetime object
:param datetime_obj:
:return: String
>>> day_of_week(datetime(year=2035, month=8, day=7))
'Tuesday'
>>> day_of_week(datetime(year=2021, month=8, day=7))
'Saturday'
>>> day_of_week(datetime(year=2021, month=8, day=1))
'Sunday'
>>> day_of_week(datetime(year=2021, month=1, day=1))
'Friday'
>>> day_of_week(datetime(year=2021, month=1, day=31))
'Sunday'
>>> day_of_week(datetime(year=1900, month=1, day=1))
'Monday'
>>> day_of_week(datetime(year=2000, month=1, day=1))
'Saturday'
"""
day_of_week: int = datetime_obj.weekday() # Monday = 0 .. Sunday = 6
return day_name[day_of_week]
def day_of_week_fmt(date_string: str, format: str = "%Y%m%d") -> str:
"""
Calculates the day name of the given date string decoded by format.
See time.strptime for format parameters.
:param date_string: date in dd/mm/yyyy format
:param format: strptime compatible date format
:return: String
>>> day_of_week_fmt("20211026")
'Tuesday'
>>> day_of_week_fmt("2021/10/26", "%Y/%m/%d")
'Tuesday'
>>> day_of_week_fmt("2021-10-26", "%Y-%m-%d")
'Tuesday'
>>> day_of_week_fmt("7/8/2021", "%d/%m/%Y")
'Saturday'
>>> day_of_week_fmt("7/8/2021", "%m/%d/%Y")
'Thursday'
"""
datetime_obj = datetime.strptime(date_string, format)
return day_of_week(datetime_obj)
def day_of_week_dmy(date_string: str) -> str:
"""
Calculates the day name of the given date string in day/month/year format
:param datestring: date in dd/mm/yyyy format
:return: String
>>> day_of_week_dmy("7/8/2035")
'Tuesday'
>>> day_of_week_dmy("7/8/2021")
'Saturday'
>>> day_of_week_dmy("7/08/2021")
'Saturday'
>>> day_of_week_dmy("07/8/2021")
'Saturday'
>>> day_of_week_dmy("07/08/2021")
'Saturday'
>>> day_of_week_dmy("1/1/2021")
'Friday'
>>> day_of_week_dmy("31/1/2021")
'Sunday'
"""
return day_of_week_fmt(date_string, format="%d/%m/%Y")
def day_of_week_mdy(date_string: str) -> str:
"""
Calculates the day name of the given date string.
:param date_mdy: date in mm/dd/yyyy format
:return: String
>>> day_of_week_mdy("8/7/2035")
'Tuesday'
>>> day_of_week_mdy("8/7/2021")
'Saturday'
>>> day_of_week_mdy("8/07/2021")
'Saturday'
>>> day_of_week_mdy("1/31/2021")
'Sunday'
>>> day_of_week_mdy("01/31/2021")
'Sunday'
"""
return day_of_week_fmt(date_string, format="%m/%d/%Y")
def day_of_week_ymd(date_string: str) -> str:
"""
>>> day_of_week_ymd("2035/07/08")
'Sunday'
>>> day_of_week_ymd("2021/7/8")
'Thursday'
>>> day_of_week_ymd("2021/1/1")
'Friday'
"""
return day_of_week_fmt(date_string, format="%Y/%m/%d")
if __name__ == "__main__":
print(
"Tuesday August 7, 2035:\n ",
day_of_week_fmt("7/8/2035", format="%d/%m/%Y"),
day_of_week_fmt("8/07/2035", format="%m/%d/%Y"),
day_of_week_fmt("2035-08-07", format="%Y-%m-%d"),
day_of_week_fmt("20350807"),
day_of_week_dmy("7/8/2035"),
day_of_week_mdy("8/7/2035"),
day_of_week_ymd("2035/08/07"),
)
print(
"Sunday August 7, 2021:\n ",
day_of_week_fmt("7/8/2021", format="%d/%m/%Y"),
day_of_week_fmt("8/07/2021", format="%m/%d/%Y"),
day_of_week_fmt("2021-08-07", format="%Y-%m-%d"),
day_of_week_fmt("20210807"),
day_of_week_dmy("7/8/2021"),
day_of_week_mdy("8/7/2021"),
day_of_week_ymd("2021/08/07"),
)
print(
"Friday January 1, 2021:\n ",
day_of_week_fmt("1/1/2021", format="%d/%m/%Y"),
day_of_week_fmt("1/1/2021", format="%m/%d/%Y"),
day_of_week_fmt("2021-01-01", format="%Y-%m-%d"),
day_of_week_fmt("20210101"),
day_of_week_dmy("1/1/2021"),
day_of_week_mdy("1/1/2021"),
day_of_week_ymd("2021/01/1"),
)
print(
"Friday October 1, 2021:\n ",
day_of_week_fmt("1/10/2021", format="%d/%m/%Y"),
day_of_week_fmt("10/1/2021", format="%m/%d/%Y"),
day_of_week_fmt("2021-10-01", format="%Y-%m-%d"),
day_of_week_fmt("20211001"),
day_of_week_dmy("1/10/2021"),
day_of_week_mdy("10/1/2021"),
day_of_week_ymd("2021/10/01"),
)
print(
"Monday January 1, 1900: \n ",
day_of_week_fmt("19000101"),
day_of_week_dmy("1/1/1900"),
day_of_week_mdy("1/1/1900"),
day_of_week_ymd("1900/1/01"),
)
print(
"Saturday January 1, 2000: \n ",
day_of_week_fmt("20000101"),
day_of_week_dmy("1/1/2000"),
day_of_week_mdy("1/1/2000"),
day_of_week_ymd("2000/1/01"),
)