Skip to content

Commit 26a0fae

Browse files
committed
Fixes for partial time overlap causing a 2 day long birthday.
1 parent 3e4cd79 commit 26a0fae

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

apps/03_birthday/final/program.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,43 @@
22

33

44
def print_header():
5-
print('-----------------------------------')
6-
print(' BIRTHDAY APP')
7-
print('-----------------------------------')
5+
print('---------------------------')
6+
print(' BIRTHDAY APP')
7+
print('---------------------------')
88
print()
99

1010

1111
def get_birthday_from_user():
12-
print('Tell us when you were born: ')
13-
year = int(input('Year [YYYY]: '))
14-
month = int(input('Month [MM]: '))
15-
day = int(input('Day [DD]: '))
12+
print("When were you born? ")
13+
year = int(input("Year [YYYY]: "))
14+
month = int(input("Month [MM]: "))
15+
day = int(input("Day [DD]: "))
1616

17-
birthday = datetime.datetime(year, month, day)
17+
birthday = datetime.date(year, month, day)
1818
return birthday
1919

2020

21-
def compute_days_between_dates(original_date, now):
22-
date1 = now
23-
date2 = datetime.datetime(now.year, original_date.month, original_date.day)
24-
dt = date1 - date2
25-
days = int(dt.total_seconds() / 60 / 60 / 24)
26-
return days
21+
def compute_days_between_dates(original_date, target_date):
22+
this_year = datetime.date(target_date.year, original_date.month, original_date.day)
23+
24+
dt = this_year - target_date
25+
return dt.days
2726

2827

2928
def print_birthday_information(days):
3029
if days < 0:
31-
print('Your birthday is in {} days!'.format(-days))
30+
print("You had your birthday {} days ago this year.".format(-days))
3231
elif days > 0:
33-
print('You had your birthday already this year! {} days ago'.format(days))
32+
print("Your birthday is in {} days!".format(days))
3433
else:
35-
print('Happy birthday!!!')
34+
print("Happy birthday!!!")
3635

3736

3837
def main():
3938
print_header()
4039
bday = get_birthday_from_user()
41-
now = datetime.datetime.now()
42-
number_of_days = compute_days_between_dates(bday, now)
40+
today = datetime.date.today()
41+
number_of_days = compute_days_between_dates(bday, today)
4342
print_birthday_information(number_of_days)
4443

4544

apps/03_birthday/you_try/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ Key concepts introduced
2828

2929
All of these come from the module `datetime`.
3030

31-
* `datetime.datetime # actual date with time class`
31+
* `datetime.date # actual date class`
3232
* `datetime.timedelta # class for time spans`
33-
* The time right now is `datetime.datetime.now()`
34-
* The difference between two times is done via subtraction: `delta = t1 - t0`
35-
* `delta.total_seconds()` may be helpful ;)
33+
* The time right now is `datetime.date.today()`
34+
* The difference between two dates is done via subtraction: `delta = t1 - t0`
35+
* `delta.days` may be helpful ;)
3636

3737

3838

0 commit comments

Comments
 (0)