-
Notifications
You must be signed in to change notification settings - Fork 0
/
seasons.py
44 lines (31 loc) · 1009 Bytes
/
seasons.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
from datetime import date
import sys
import inflect
import re
def main():
birth_date = input("Date of Birth: ")
if valid_date_format(birth_date) and date_existed(birth_date):
print(age_as_minutes(birth_date))
else:
sys.exit(1)
def valid_date_format(date):
"""Validates a date in the format YYYY-MM-DD"""
match = re.match(r"^\d{4}-\d{2}-\d{2}$", date)
if match:
return True
else:
return False
def date_existed(iso_date):
"""Takes a date (in ISO format) and determines if it existed"""
return date.fromisoformat(iso_date) < date.today()
def age_as_minutes(birth_date):
"""Returns someone's age as minutes"""
birth = date.fromisoformat(birth_date)
today = date.today()
timedelta = today - birth
minutes = timedelta.days * 24 * 60
p = inflect.engine()
number_of_minutes = p.number_to_words(minutes, andword="").capitalize()
return f"{number_of_minutes} minutes"
if __name__ == "__main__":
main()