blob: 991537d2e80295fa954ff249a1d626408765f6d5 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumf8ae54d1994-02-07 13:45:27 +00002
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
8import sys
9import time
10import calendar
11
Neal Norwitzce96f692006-03-17 06:49:51 +000012def raw_input(prompt):
13 sys.stdout.write(prompt)
14 sys.stdout.flush()
15 return sys.stdin.readline()
16
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000017def main():
Moshe Zadka273ad452001-02-20 16:32:24 +000018 # 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 Winter6f2df4d2007-07-17 20:59:35 +000024 year = int(input('In which year were you born? '))
Moshe Zadka273ad452001-02-20 16:32:24 +000025 if 0<=year<100:
Collin Winter6f2df4d2007-07-17 20:59:35 +000026 print("I'll assume that by", year, end=' ')
Moshe Zadka273ad452001-02-20 16:32:24 +000027 year = year + 1900
Collin Winter6f2df4d2007-07-17 20:59:35 +000028 print('you mean', year, 'and not the early Christian era')
Moshe Zadka273ad452001-02-20 16:32:24 +000029 elif not (1850<=year<=2002):
Collin Winter6f2df4d2007-07-17 20:59:35 +000030 print("It's hard to believe you were born in", year)
Moshe Zadka273ad452001-02-20 16:32:24 +000031 return
32 #
33 if sys.argv[2:]:
34 month = int(sys.argv[2])
35 else:
Collin Winter6f2df4d2007-07-17 20:59:35 +000036 month = int(input('And in which month? (1-12) '))
Moshe Zadka273ad452001-02-20 16:32:24 +000037 if not (1<=month<=12):
Collin Winter6f2df4d2007-07-17 20:59:35 +000038 print('There is no month numbered', month)
Moshe Zadka273ad452001-02-20 16:32:24 +000039 return
40 #
41 if sys.argv[3:]:
42 day = int(sys.argv[3])
43 else:
Collin Winter6f2df4d2007-07-17 20:59:35 +000044 day = int(input('And on what day of that month? (1-31) '))
Moshe Zadka273ad452001-02-20 16:32:24 +000045 if month == 2 and calendar.isleap(year):
46 maxday = 29
47 else:
48 maxday = calendar.mdays[month]
49 if not (1<=day<=maxday):
Collin Winter6f2df4d2007-07-17 20:59:35 +000050 print('There are no', day, 'days in that month!')
Moshe Zadka273ad452001-02-20 16:32:24 +000051 return
52 #
53 bdaytuple = (year, month, day)
54 bdaydate = mkdate(bdaytuple)
Collin Winter6f2df4d2007-07-17 20:59:35 +000055 print('You were born on', format(bdaytuple))
Moshe Zadka273ad452001-02-20 16:32:24 +000056 #
57 todaytuple = time.localtime()[:3]
58 todaydate = mkdate(todaytuple)
Collin Winter6f2df4d2007-07-17 20:59:35 +000059 print('Today is', format(todaytuple))
Moshe Zadka273ad452001-02-20 16:32:24 +000060 #
61 if bdaytuple > todaytuple:
Collin Winter6f2df4d2007-07-17 20:59:35 +000062 print('You are a time traveler. Go back to the future!')
Moshe Zadka273ad452001-02-20 16:32:24 +000063 return
64 #
65 if bdaytuple == todaytuple:
Collin Winter6f2df4d2007-07-17 20:59:35 +000066 print('You were born today. Have a nice life!')
Moshe Zadka273ad452001-02-20 16:32:24 +000067 return
68 #
69 days = todaydate - bdaydate
Collin Winter6f2df4d2007-07-17 20:59:35 +000070 print('You have lived', days, 'days')
Moshe Zadka273ad452001-02-20 16:32:24 +000071 #
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 Winter6f2df4d2007-07-17 20:59:35 +000077 print('You are', age, 'years old')
Moshe Zadka273ad452001-02-20 16:32:24 +000078 #
79 if todaytuple[1:] == bdaytuple[1:]:
Collin Winter6f2df4d2007-07-17 20:59:35 +000080 print('Congratulations! Today is your', nth(age), 'birthday')
81 print('Yesterday was your', end=' ')
Moshe Zadka273ad452001-02-20 16:32:24 +000082 else:
Collin Winter6f2df4d2007-07-17 20:59:35 +000083 print('Today is your', end=' ')
84 print(nth(days - age), 'unbirthday')
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000085
Collin Winter6f2df4d2007-07-17 20:59:35 +000086def format(xxx_todo_changeme):
87 (year, month, day) = xxx_todo_changeme
Moshe Zadka273ad452001-02-20 16:32:24 +000088 return '%d %s %d' % (day, calendar.month_name[month], year)
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000089
90def nth(n):
Moshe Zadka273ad452001-02-20 16:32:24 +000091 if n == 1: return '1st'
92 if n == 2: return '2nd'
93 if n == 3: return '3rd'
94 return '%dth' % n
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000095
Collin Winter6f2df4d2007-07-17 20:59:35 +000096def mkdate(xxx_todo_changeme1):
Moshe Zadka273ad452001-02-20 16:32:24 +000097 # 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 Winter6f2df4d2007-07-17 20:59:35 +0000100 (year, month, day) = xxx_todo_changeme1
Moshe Zadka273ad452001-02-20 16:32:24 +0000101 days = year*365 # years, roughly
Benjamin Petersond7b03282008-09-13 15:58:53 +0000102 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
Moshe Zadka273ad452001-02-20 16:32:24 +0000105 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 Rossumf8ae54d1994-02-07 13:45:27 +0000112
Johannes Gijsbers7a8c43e2004-09-11 16:34:35 +0000113if __name__ == "__main__":
114 main()