blob: 94ad448588ec89676f08e62cdd8c41bddf539190 [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:
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 Rossumf8ae54d1994-02-07 13:45:27 +000085
86def format((year, month, day)):
Moshe Zadka273ad452001-02-20 16:32:24 +000087 return '%d %s %d' % (day, calendar.month_name[month], year)
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000088
89def nth(n):
Moshe Zadka273ad452001-02-20 16:32:24 +000090 if n == 1: return '1st'
91 if n == 2: return '2nd'
92 if n == 3: return '3rd'
93 return '%dth' % n
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000094
95def mkdate((year, month, day)):
Moshe Zadka273ad452001-02-20 16:32:24 +000096 # 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 Rossumf8ae54d1994-02-07 13:45:27 +0000110
Johannes Gijsbers7a8c43e2004-09-11 16:34:35 +0000111if __name__ == "__main__":
112 main()