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.
13 # Note that the range checks below also check for bad types,
14 # e.g. 3.14 or (). However syntactically invalid replies
15 # will raise an exception.
17 year
= eval(sys
.argv
[1])
19 year
= input('In which year were you born? ')
20 if year
in range(100):
21 print 'I\'ll assume that by', year
,
23 print 'you mean', year
, 'and not the early Christian era'
24 elif year
not in range(1850, 2000):
25 print 'It\'s hard to believe you were born in', year
29 month
= eval(sys
.argv
[2])
31 month
= input('And in which month? (1-12) ')
32 if month
not in range(1, 13):
33 print 'There is no month numbered', month
37 day
= eval(sys
.argv
[3])
39 day
= input('And on what day of that month? (1-31) ')
40 if month
== 2 and calendar
.isleap(year
):
43 maxday
= calendar
.mdays
[month
]
44 if day
not in range(1, maxday
+1):
45 print 'There are no', day
, 'days in that month!'
48 bdaytuple
= (year
, month
, day
)
49 bdaydate
= mkdate(bdaytuple
)
50 print 'You were born on', format(bdaytuple
)
52 todaytuple
= time
.localtime(time
.time())[:3]
53 todaydate
= mkdate(todaytuple
)
54 print 'Today is', format(todaytuple
)
56 if bdaytuple
> todaytuple
:
57 print 'You are a time traveler. Go back to the future!'
60 if bdaytuple
== todaytuple
:
61 print 'You were born today. Have a nice life!'
64 days
= todaydate
- bdaydate
65 print 'You have lived', days
, 'days'
68 for y
in range(year
, todaytuple
[0] + 1):
69 if bdaytuple
< (y
, month
, day
) <= todaytuple
:
72 print 'You are', age
, 'years old'
74 if todaytuple
[1:] == bdaytuple
[1:]:
75 print 'Congratulations! Today is your', nth(age
), 'birthday'
76 print 'Yesterday was your',
78 print 'Today is your',
79 print nth(days
- age
), 'unbirthday'
81 def format((year
, month
, day
)):
82 return '%d %s %d' % (day
, calendar
.month_name
[month
], year
)
85 if n
== 1: return '1st'
86 if n
== 2: return '2nd'
87 if n
== 3: return '3rd'
90 def mkdate((year
, month
, day
)):
91 # Januari 1st, in 0 A.D. is arbitrarily defined to be day 1,
92 # even though that day never actually existed and the calendar
93 # was different then...
94 days
= year
*365 # years, roughly
95 days
= days
+ (year
+3)/4 # plus leap years, roughly
96 days
= days
- (year
+99)/100 # minus non-leap years every century
97 days
= days
+ (year
+399)/400 # plus leap years every 4 centirues
98 for i
in range(1, month
):
99 if i
== 2 and calendar
.isleap(year
):
102 days
= days
+ calendar
.mdays
[i
]