Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 2 | |
| 3 | # Calculate your unbirthday count (see Alice in Wonderland). |
| 4 | # This is defined as the number of days from your birth until today |
| 5 | # that weren't your birthday. (The day you were born is not counted). |
| 6 | # Leap years make it interesting. |
| 7 | |
| 8 | import sys |
| 9 | import time |
| 10 | import calendar |
| 11 | |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 12 | def raw_input(prompt): |
| 13 | sys.stdout.write(prompt) |
| 14 | sys.stdout.flush() |
| 15 | return sys.stdin.readline() |
| 16 | |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 17 | def main(): |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 18 | # Note that the range checks below also check for bad types, |
| 19 | # e.g. 3.14 or (). However syntactically invalid replies |
| 20 | # will raise an exception. |
| 21 | if sys.argv[1:]: |
| 22 | year = int(sys.argv[1]) |
| 23 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 24 | year = int(input('In which year were you born? ')) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 25 | if 0<=year<100: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 26 | print("I'll assume that by", year, end=' ') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 27 | year = year + 1900 |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 28 | print('you mean', year, 'and not the early Christian era') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 29 | elif not (1850<=year<=2002): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 30 | print("It's hard to believe you were born in", year) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 31 | return |
| 32 | # |
| 33 | if sys.argv[2:]: |
| 34 | month = int(sys.argv[2]) |
| 35 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 36 | month = int(input('And in which month? (1-12) ')) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 37 | if not (1<=month<=12): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 38 | print('There is no month numbered', month) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 39 | return |
| 40 | # |
| 41 | if sys.argv[3:]: |
| 42 | day = int(sys.argv[3]) |
| 43 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 44 | day = int(input('And on what day of that month? (1-31) ')) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 45 | if month == 2 and calendar.isleap(year): |
| 46 | maxday = 29 |
| 47 | else: |
| 48 | maxday = calendar.mdays[month] |
| 49 | if not (1<=day<=maxday): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 50 | print('There are no', day, 'days in that month!') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 51 | return |
| 52 | # |
| 53 | bdaytuple = (year, month, day) |
| 54 | bdaydate = mkdate(bdaytuple) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 55 | print('You were born on', format(bdaytuple)) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 56 | # |
| 57 | todaytuple = time.localtime()[:3] |
| 58 | todaydate = mkdate(todaytuple) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 59 | print('Today is', format(todaytuple)) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 60 | # |
| 61 | if bdaytuple > todaytuple: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 62 | print('You are a time traveler. Go back to the future!') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 63 | return |
| 64 | # |
| 65 | if bdaytuple == todaytuple: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 66 | print('You were born today. Have a nice life!') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 67 | return |
| 68 | # |
| 69 | days = todaydate - bdaydate |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 70 | print('You have lived', days, 'days') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 71 | # |
| 72 | age = 0 |
| 73 | for y in range(year, todaytuple[0] + 1): |
| 74 | if bdaytuple < (y, month, day) <= todaytuple: |
| 75 | age = age + 1 |
| 76 | # |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 77 | print('You are', age, 'years old') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 78 | # |
| 79 | if todaytuple[1:] == bdaytuple[1:]: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 80 | print('Congratulations! Today is your', nth(age), 'birthday') |
| 81 | print('Yesterday was your', end=' ') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 82 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 83 | print('Today is your', end=' ') |
| 84 | print(nth(days - age), 'unbirthday') |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 85 | |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 86 | def format(xxx_todo_changeme): |
| 87 | (year, month, day) = xxx_todo_changeme |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 88 | return '%d %s %d' % (day, calendar.month_name[month], year) |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 89 | |
| 90 | def nth(n): |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 91 | if n == 1: return '1st' |
| 92 | if n == 2: return '2nd' |
| 93 | if n == 3: return '3rd' |
| 94 | return '%dth' % n |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 95 | |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 96 | def mkdate(xxx_todo_changeme1): |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 97 | # Januari 1st, in 0 A.D. is arbitrarily defined to be day 1, |
| 98 | # even though that day never actually existed and the calendar |
| 99 | # was different then... |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 100 | (year, month, day) = xxx_todo_changeme1 |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 101 | days = year*365 # years, roughly |
| 102 | days = days + (year+3)/4 # plus leap years, roughly |
| 103 | days = days - (year+99)/100 # minus non-leap years every century |
| 104 | days = days + (year+399)/400 # plus leap years every 4 centirues |
| 105 | for i in range(1, month): |
| 106 | if i == 2 and calendar.isleap(year): |
| 107 | days = days + 29 |
| 108 | else: |
| 109 | days = days + calendar.mdays[i] |
| 110 | days = days + day |
| 111 | return days |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 112 | |
Johannes Gijsbers | 7a8c43e | 2004-09-11 16:34:35 +0000 | [diff] [blame] | 113 | if __name__ == "__main__": |
| 114 | main() |