blob: 056ad44c463bea15f42eb909f0abdacde2777b23 [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 if sys.argv[1:]:
14 year = int(sys.argv[1])
15 else:
16 year = int(raw_input('In which year were you born? '))
Georg Brandl0b798a92009-10-10 21:43:21 +000017 if 0 <= year < 100:
Moshe Zadka273ad452001-02-20 16:32:24 +000018 print "I'll assume that by", year,
19 year = year + 1900
20 print 'you mean', year, 'and not the early Christian era'
Georg Brandl0b798a92009-10-10 21:43:21 +000021 elif not (1850 <= year <= time.localtime()[0]):
Moshe Zadka273ad452001-02-20 16:32:24 +000022 print "It's hard to believe you were born in", year
23 return
Georg Brandl0b798a92009-10-10 21:43:21 +000024
Moshe Zadka273ad452001-02-20 16:32:24 +000025 if sys.argv[2:]:
26 month = int(sys.argv[2])
27 else:
28 month = int(raw_input('And in which month? (1-12) '))
Georg Brandl0b798a92009-10-10 21:43:21 +000029 if not (1 <= month <= 12):
Moshe Zadka273ad452001-02-20 16:32:24 +000030 print 'There is no month numbered', month
31 return
Georg Brandl0b798a92009-10-10 21:43:21 +000032
Moshe Zadka273ad452001-02-20 16:32:24 +000033 if sys.argv[3:]:
34 day = int(sys.argv[3])
35 else:
36 day = int(raw_input('And on what day of that month? (1-31) '))
37 if month == 2 and calendar.isleap(year):
38 maxday = 29
39 else:
40 maxday = calendar.mdays[month]
Georg Brandl0b798a92009-10-10 21:43:21 +000041 if not (1 <= day <= maxday):
Moshe Zadka273ad452001-02-20 16:32:24 +000042 print 'There are no', day, 'days in that month!'
43 return
Georg Brandl0b798a92009-10-10 21:43:21 +000044
Moshe Zadka273ad452001-02-20 16:32:24 +000045 bdaytuple = (year, month, day)
46 bdaydate = mkdate(bdaytuple)
47 print 'You were born on', format(bdaytuple)
Georg Brandl0b798a92009-10-10 21:43:21 +000048
Moshe Zadka273ad452001-02-20 16:32:24 +000049 todaytuple = time.localtime()[:3]
50 todaydate = mkdate(todaytuple)
51 print 'Today is', format(todaytuple)
Georg Brandl0b798a92009-10-10 21:43:21 +000052
Moshe Zadka273ad452001-02-20 16:32:24 +000053 if bdaytuple > todaytuple:
54 print 'You are a time traveler. Go back to the future!'
55 return
Georg Brandl0b798a92009-10-10 21:43:21 +000056
Moshe Zadka273ad452001-02-20 16:32:24 +000057 if bdaytuple == todaytuple:
58 print 'You were born today. Have a nice life!'
59 return
Georg Brandl0b798a92009-10-10 21:43:21 +000060
Moshe Zadka273ad452001-02-20 16:32:24 +000061 days = todaydate - bdaydate
62 print 'You have lived', days, 'days'
Georg Brandl0b798a92009-10-10 21:43:21 +000063
Moshe Zadka273ad452001-02-20 16:32:24 +000064 age = 0
65 for y in range(year, todaytuple[0] + 1):
66 if bdaytuple < (y, month, day) <= todaytuple:
67 age = age + 1
Georg Brandl0b798a92009-10-10 21:43:21 +000068
Moshe Zadka273ad452001-02-20 16:32:24 +000069 print 'You are', age, 'years old'
Georg Brandl0b798a92009-10-10 21:43:21 +000070
Moshe Zadka273ad452001-02-20 16:32:24 +000071 if todaytuple[1:] == bdaytuple[1:]:
72 print 'Congratulations! Today is your', nth(age), 'birthday'
73 print 'Yesterday was your',
74 else:
75 print 'Today is your',
76 print nth(days - age), 'unbirthday'
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000077
78def format((year, month, day)):
Moshe Zadka273ad452001-02-20 16:32:24 +000079 return '%d %s %d' % (day, calendar.month_name[month], year)
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000080
81def nth(n):
Moshe Zadka273ad452001-02-20 16:32:24 +000082 if n == 1: return '1st'
83 if n == 2: return '2nd'
84 if n == 3: return '3rd'
85 return '%dth' % n
Guido van Rossumf8ae54d1994-02-07 13:45:27 +000086
87def mkdate((year, month, day)):
Georg Brandl0b798a92009-10-10 21:43:21 +000088 # January 1st, in 0 A.D. is arbitrarily defined to be day 1,
Moshe Zadka273ad452001-02-20 16:32:24 +000089 # even though that day never actually existed and the calendar
90 # was different then...
Georg Brandl0b798a92009-10-10 21:43:21 +000091 days = year*365 # years, roughly
Andrew M. Kuchling2a9b9cb2008-09-13 01:43:28 +000092 days = days + (year+3)//4 # plus leap years, roughly
93 days = days - (year+99)//100 # minus non-leap years every century
94 days = days + (year+399)//400 # plus leap years every 4 centirues
Moshe Zadka273ad452001-02-20 16:32:24 +000095 for i in range(1, month):
96 if i == 2 and calendar.isleap(year):
97 days = days + 29
98 else:
99 days = days + calendar.mdays[i]
100 days = days + day
101 return days
Guido van Rossumf8ae54d1994-02-07 13:45:27 +0000102
Johannes Gijsbers7a8c43e2004-09-11 16:34:35 +0000103if __name__ == "__main__":
104 main()