|
2 | 2 |
|
3 | 3 |
|
4 | 4 | def print_header():
|
5 |
| - print('-----------------------------------') |
6 |
| - print(' BIRTHDAY APP') |
7 |
| - print('-----------------------------------') |
| 5 | + print('---------------------------') |
| 6 | + print(' BIRTHDAY APP') |
| 7 | + print('---------------------------') |
8 | 8 | print()
|
9 | 9 |
|
10 | 10 |
|
11 | 11 | 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]: ")) |
16 | 16 |
|
17 |
| - birthday = datetime.datetime(year, month, day) |
| 17 | + birthday = datetime.date(year, month, day) |
18 | 18 | return birthday
|
19 | 19 |
|
20 | 20 |
|
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 |
27 | 26 |
|
28 | 27 |
|
29 | 28 | def print_birthday_information(days):
|
30 | 29 | if days < 0:
|
31 |
| - print('Your birthday is in {} days!'.format(-days)) |
| 30 | + print("You had your birthday {} days ago this year.".format(-days)) |
32 | 31 | 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)) |
34 | 33 | else:
|
35 |
| - print('Happy birthday!!!') |
| 34 | + print("Happy birthday!!!") |
36 | 35 |
|
37 | 36 |
|
38 | 37 | def main():
|
39 | 38 | print_header()
|
40 | 39 | 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) |
43 | 42 | print_birthday_information(number_of_days)
|
44 | 43 |
|
45 | 44 |
|
|
0 commit comments