-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutdated.py
More file actions
89 lines (73 loc) · 2.49 KB
/
outdated.py
File metadata and controls
89 lines (73 loc) · 2.49 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
84
85
86
87
88
89
"""
Fortunately, computers tend to use ISO 8601, an international standard that prescribes that dates should be formatted in year-month-day (YYYY-MM-DD) order, no matter the country, formatting years with four digits, months with two digits, and days with two digits, “padding” each with leading zeroes as needed.
In a file called outdated.py, implement a program that prompts the user for a date, anno Domini, in month-day-year order, formatted like 9/8/1636 or September 8, 1636, wherein the month in the latter might be any of the values in the list below:
[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
Then output that same date in YYYY-MM-DD format. If the user’s input is not a valid date in either format, prompt the user again. Assume that every month has no more than 31 days; no need to validate whether a month has 28, 29, 30, or 31 days.
"""
months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
def date_format_first(date):
date_format = date.split("/")
if len(date_format) == 3:
for _ in date_format:
if not _.isdigit():
return False
return True
def date_format_second(date):
date_format = date.split(",")
if len(date_format) == 2:
return True
return False
def validate_date(date):
while True:
date = str(input(date)).strip()
# First Case - month-day-year - e.g.: 9/8/1636
if date_format_first(date):
date_format = date.split("/")
yyyy = int(date_format[2])
mm = int(date_format[0])
dd = int(date_format[1])
if 0 < dd <= 31 and 0 < mm <= 12:
return print(f"{yyyy:04}-{mm:02}-{dd:02}")
# Second Case - e.g.: September 8, 1636
if date_format_second(date):
date_format = date.split(",")
mm = date_format[0].split()[0]
try:
mm = months.index(mm) + 1
except ValueError:
continue
else:
dd = int(date_format[0].split()[1])
yyyy = int(date_format[1])
if 0 < dd <= 31 and 0 < mm <= 12:
return print(f"{yyyy:04}-{mm:02}-{dd:02}")
def main():
date = validate_date("Date: ")
if __name__ == "__main__":
main()