blob: e3b7be73f0c4f82e2b4818609c76dfbe0687b4a0 [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
12def main():
Moshe Zadka273ad452001-02-20 16:32:24 +000013 # 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.
16 if sys.argv[1:]:
17 year = int(sys.argv[1])
18 else:
19 year = int(raw_input('In which year were you born? '))
20 if 0<=year<100:
21 print "I'll assume that by", year,
22 year = year + 1900
23 print 'you mean', year, 'and not the early Christian era'
24 elif not (1850<=year<=2002):
25 print "It's hard to believe you were born in", year
26 return
27 #
28 if sys.argv[2:]:
29 month = int(sys.argv[2])
30 else:
31 month = int(raw_input('And in which month? (1-12) '))
32 if not (1<=month<=12):
33 print 'There is no month numbered', month
34 return
35 #
36 if sys.argv[3:]:
37 day = int(sys.argv[3])
38 else:
39 day = int(raw_input('And on what day of that month? (1-31) '))
40 if month == 2 and calendar.isleap(year):
41 maxday = 29
42 else:
43 maxday = calendar.mdays[month]
44 if not (1<=day<=maxday):
45 print 'There are no', day, 'days in that month!'
46 return
47 #
48 bdaytuple = (year, month, day)
49 bdaydate = mkdate(bdaytuple)
50 print 'You were born on', format(bdaytuple)
51 #
52 todaytuple = time.localtime()[:3]
53 todaydate = mkdate(todaytuple)
54 print 'Today is', format(todaytuple)
55 #
56 if bdaytuple > todaytuple:
57 print 'You are a time traveler. Go back to the future!'
58 return
59 #
60 if bdaytuple == todaytuple:
61 print 'You were born today. Have a nice life!'
62 return
63 #
64 days = todaydate - bdaydate
65 print 'You have lived', days, 'days'
66 #
67 age = 0
68 for y in range(year, todaytuple[0] + 1):
69 if bdaytuple < (y, month, day) <= todaytuple:
70 age = age + 1
71 #
72 print 'You are', age, 'years old'
73 #
74 if todaytuple[1:] == bdaytuple[1:]:
75 print 'Congratulations! Today is your', nth(age), 'birthday'
76 print 'Yesterday was your',
77 else:
78 print 'Today is your',
79 print nth(days - age), 'unbirthday'
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000080
81def format((year, month, day)):
Moshe Zadka273ad452001-02-20 16:32:24 +000082 return '%d %s %d' % (day, calendar.month_name[month], year)
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000083
84def nth(n):
Moshe Zadka273ad452001-02-20 16:32:24 +000085 if n == 1: return '1st'
86 if n == 2: return '2nd'
87 if n == 3: return '3rd'
88 return '%dth' % n
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000089
90def mkdate((year, month, day)):
Moshe Zadka273ad452001-02-20 16:32:24 +000091 # 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):
100 days = days + 29
101 else:
102 days = days + calendar.mdays[i]
103 days = days + day
104 return days
Guido van Rossumf8ae54d1994-02-07 13:45:27 +0000105
106main()