+++ /dev/null
-#! /usr/bin/env python\r
-\r
-# Calculate your unbirthday count (see Alice in Wonderland).\r
-# This is defined as the number of days from your birth until today\r
-# that weren't your birthday. (The day you were born is not counted).\r
-# Leap years make it interesting.\r
-\r
-import sys\r
-import time\r
-import calendar\r
-\r
-def main():\r
- if sys.argv[1:]:\r
- year = int(sys.argv[1])\r
- else:\r
- year = int(raw_input('In which year were you born? '))\r
- if 0 <= year < 100:\r
- print "I'll assume that by", year,\r
- year = year + 1900\r
- print 'you mean', year, 'and not the early Christian era'\r
- elif not (1850 <= year <= time.localtime()[0]):\r
- print "It's hard to believe you were born in", year\r
- return\r
-\r
- if sys.argv[2:]:\r
- month = int(sys.argv[2])\r
- else:\r
- month = int(raw_input('And in which month? (1-12) '))\r
- if not (1 <= month <= 12):\r
- print 'There is no month numbered', month\r
- return\r
-\r
- if sys.argv[3:]:\r
- day = int(sys.argv[3])\r
- else:\r
- day = int(raw_input('And on what day of that month? (1-31) '))\r
- if month == 2 and calendar.isleap(year):\r
- maxday = 29\r
- else:\r
- maxday = calendar.mdays[month]\r
- if not (1 <= day <= maxday):\r
- print 'There are no', day, 'days in that month!'\r
- return\r
-\r
- bdaytuple = (year, month, day)\r
- bdaydate = mkdate(bdaytuple)\r
- print 'You were born on', format(bdaytuple)\r
-\r
- todaytuple = time.localtime()[:3]\r
- todaydate = mkdate(todaytuple)\r
- print 'Today is', format(todaytuple)\r
-\r
- if bdaytuple > todaytuple:\r
- print 'You are a time traveler. Go back to the future!'\r
- return\r
-\r
- if bdaytuple == todaytuple:\r
- print 'You were born today. Have a nice life!'\r
- return\r
-\r
- days = todaydate - bdaydate\r
- print 'You have lived', days, 'days'\r
-\r
- age = 0\r
- for y in range(year, todaytuple[0] + 1):\r
- if bdaytuple < (y, month, day) <= todaytuple:\r
- age = age + 1\r
-\r
- print 'You are', age, 'years old'\r
-\r
- if todaytuple[1:] == bdaytuple[1:]:\r
- print 'Congratulations! Today is your', nth(age), 'birthday'\r
- print 'Yesterday was your',\r
- else:\r
- print 'Today is your',\r
- print nth(days - age), 'unbirthday'\r
-\r
-def format((year, month, day)):\r
- return '%d %s %d' % (day, calendar.month_name[month], year)\r
-\r
-def nth(n):\r
- if n == 1: return '1st'\r
- if n == 2: return '2nd'\r
- if n == 3: return '3rd'\r
- return '%dth' % n\r
-\r
-def mkdate((year, month, day)):\r
- # January 1st, in 0 A.D. is arbitrarily defined to be day 1,\r
- # even though that day never actually existed and the calendar\r
- # was different then...\r
- days = year*365 # years, roughly\r
- days = days + (year+3)//4 # plus leap years, roughly\r
- days = days - (year+99)//100 # minus non-leap years every century\r
- days = days + (year+399)//400 # plus leap years every 4 centirues\r
- for i in range(1, month):\r
- if i == 2 and calendar.isleap(year):\r
- days = days + 29\r
- else:\r
- days = days + calendar.mdays[i]\r
- days = days + day\r
- return days\r
-\r
-if __name__ == "__main__":\r
- main()\r