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: |
| 24 | year = int(raw_input('In which year were you born? ')) |
| 25 | if 0<=year<100: |
| 26 | print "I'll assume that by", year, |
| 27 | year = year + 1900 |
| 28 | print 'you mean', year, 'and not the early Christian era' |
| 29 | elif not (1850<=year<=2002): |
| 30 | print "It's hard to believe you were born in", year |
| 31 | return |
| 32 | # |
| 33 | if sys.argv[2:]: |
| 34 | month = int(sys.argv[2]) |
| 35 | else: |
| 36 | month = int(raw_input('And in which month? (1-12) ')) |
| 37 | if not (1<=month<=12): |
| 38 | print 'There is no month numbered', month |
| 39 | return |
| 40 | # |
| 41 | if sys.argv[3:]: |
| 42 | day = int(sys.argv[3]) |
| 43 | else: |
| 44 | day = int(raw_input('And on what day of that month? (1-31) ')) |
| 45 | if month == 2 and calendar.isleap(year): |
| 46 | maxday = 29 |
| 47 | else: |
| 48 | maxday = calendar.mdays[month] |
| 49 | if not (1<=day<=maxday): |
| 50 | print 'There are no', day, 'days in that month!' |
| 51 | return |
| 52 | # |
| 53 | bdaytuple = (year, month, day) |
| 54 | bdaydate = mkdate(bdaytuple) |
| 55 | print 'You were born on', format(bdaytuple) |
| 56 | # |
| 57 | todaytuple = time.localtime()[:3] |
| 58 | todaydate = mkdate(todaytuple) |
| 59 | print 'Today is', format(todaytuple) |
| 60 | # |
| 61 | if bdaytuple > todaytuple: |
| 62 | print 'You are a time traveler. Go back to the future!' |
| 63 | return |
| 64 | # |
| 65 | if bdaytuple == todaytuple: |
| 66 | print 'You were born today. Have a nice life!' |
| 67 | return |
| 68 | # |
| 69 | days = todaydate - bdaydate |
| 70 | print 'You have lived', days, 'days' |
| 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 | # |
| 77 | print 'You are', age, 'years old' |
| 78 | # |
| 79 | if todaytuple[1:] == bdaytuple[1:]: |
| 80 | print 'Congratulations! Today is your', nth(age), 'birthday' |
| 81 | print 'Yesterday was your', |
| 82 | else: |
| 83 | print 'Today is your', |
| 84 | print nth(days - age), 'unbirthday' |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 85 | |
| 86 | def format((year, month, day)): |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 87 | return '%d %s %d' % (day, calendar.month_name[month], year) |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 88 | |
| 89 | def nth(n): |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 90 | if n == 1: return '1st' |
| 91 | if n == 2: return '2nd' |
| 92 | if n == 3: return '3rd' |
| 93 | return '%dth' % n |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 94 | |
| 95 | def mkdate((year, month, day)): |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 96 | # Januari 1st, in 0 A.D. is arbitrarily defined to be day 1, |
| 97 | # even though that day never actually existed and the calendar |
| 98 | # was different then... |
| 99 | days = year*365 # years, roughly |
| 100 | days = days + (year+3)/4 # plus leap years, roughly |
| 101 | days = days - (year+99)/100 # minus non-leap years every century |
| 102 | days = days + (year+399)/400 # plus leap years every 4 centirues |
| 103 | for i in range(1, month): |
| 104 | if i == 2 and calendar.isleap(year): |
| 105 | days = days + 29 |
| 106 | else: |
| 107 | days = days + calendar.mdays[i] |
| 108 | days = days + day |
| 109 | return days |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 110 | |
Johannes Gijsbers | 7a8c43e | 2004-09-11 16:34:35 +0000 | [diff] [blame] | 111 | if __name__ == "__main__": |
| 112 | main() |