Skip to content

Commit d2cdc97

Browse files
Add files via upload
1 parent 3c0b8cf commit d2cdc97

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Project for Week 4 of "Python Programming Essentials".
3+
Collection of functions to process dates.
4+
5+
Be sure to read the project description page for further information
6+
about the expected behavior of the program.
7+
"""
8+
9+
import datetime
10+
11+
def days_in_month(year, month):
12+
"""
13+
Inputs:
14+
year - an integer between datetime.MINYEAR and datetime.MAXYEAR
15+
representing the year
16+
month - an integer between 1 and 12 representing the month
17+
18+
Returns:
19+
The number of days in the input month.
20+
"""
21+
first_day_this_month = datetime.date(year, month, 1)
22+
if month == 12:
23+
first_day_next_month = datetime.date(year+1, (month%12)+1, 1)
24+
num_days_this_month = first_day_next_month - first_day_this_month
25+
return num_days_this_month.days
26+
else:
27+
first_day_next_month = datetime.date(year, month+1, 1)
28+
num_days_this_month = first_day_next_month - first_day_this_month
29+
return num_days_this_month.days
30+
31+
def is_valid_date(year, month, day):
32+
"""
33+
Inputs:
34+
year - an integer representing the year
35+
month - an integer representing the month
36+
day - an integer representing the day
37+
38+
Returns:
39+
True if year-month-day is a valid date and
40+
False otherwise
41+
"""
42+
if (datetime.MINYEAR <= year <= datetime.MAXYEAR) and (1 <= month <= 12) and (0 < day <= days_in_month(year, month)):
43+
return True
44+
else:
45+
return False
46+
47+
def days_between(year1, month1, day1, year2, month2, day2):
48+
"""
49+
Inputs:
50+
year1 - an integer representing the year of the first date
51+
month1 - an integer representing the month of the first date
52+
day1 - an integer representing the day of the first date
53+
year2 - an integer representing the year of the second date
54+
month2 - an integer representing the month of the second date
55+
day2 - an integer representing the day of the second date
56+
57+
Returns:
58+
The number of days from the first date to the second date.
59+
Returns 0 if either date is invalid or the second date is
60+
before the first date.
61+
"""
62+
if (not(is_valid_date(year1, month1, day1)) or not(is_valid_date(year2, month2, day2))):
63+
return 0
64+
elif datetime.date(year2, month2, day2) < datetime.date(year1, month1, day1):
65+
return 0
66+
else:
67+
days_between_dates = datetime.date(year2, month2, day2) - datetime.date(year1, month1, day1)
68+
return days_between_dates.days
69+
70+
def age_in_days(year, month, day):
71+
"""
72+
Inputs:
73+
year - an integer representing the birthday year
74+
month - an integer representing the birthday month
75+
day - an integer representing the birthday day
76+
77+
Returns:
78+
The age of a person with the input birthday as of today.
79+
Returns 0 if the input date is invalid or if the input
80+
date is in the future.
81+
"""
82+
todays_date = datetime.date.today()
83+
if not(is_valid_date(year, month, day)):
84+
return 0
85+
elif year > todays_date.year:
86+
return 0
87+
else:
88+
age_in_num_days = days_between(year, month, day, todays_date.year, todays_date.month, todays_date.day)
89+
return age_in_num_days

0 commit comments

Comments
 (0)