Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 2 | |
| 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 | |
| 8 | import sys |
| 9 | import time |
| 10 | import calendar |
| 11 | |
| 12 | def main(): |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 13 | if sys.argv[1:]: |
| 14 | year = int(sys.argv[1]) |
| 15 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 16 | year = int(input('In which year were you born? ')) |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 17 | if 0 <= year < 100: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 18 | print("I'll assume that by", year, end=' ') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 19 | year = year + 1900 |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 20 | print('you mean', year, 'and not the early Christian era') |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 21 | elif not (1850 <= year <= time.localtime()[0]): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 22 | print("It's hard to believe you were born in", year) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 23 | return |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 24 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 25 | if sys.argv[2:]: |
| 26 | month = int(sys.argv[2]) |
| 27 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 28 | month = int(input('And in which month? (1-12) ')) |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 29 | if not (1 <= month <= 12): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 30 | print('There is no month numbered', month) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 31 | return |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 32 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 33 | if sys.argv[3:]: |
| 34 | day = int(sys.argv[3]) |
| 35 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 36 | day = int(input('And on what day of that month? (1-31) ')) |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 37 | if month == 2 and calendar.isleap(year): |
| 38 | maxday = 29 |
| 39 | else: |
| 40 | maxday = calendar.mdays[month] |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 41 | if not (1 <= day <= maxday): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 42 | print('There are no', day, 'days in that month!') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 43 | return |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 44 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 45 | bdaytuple = (year, month, day) |
| 46 | bdaydate = mkdate(bdaytuple) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 47 | print('You were born on', format(bdaytuple)) |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 48 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 49 | todaytuple = time.localtime()[:3] |
| 50 | todaydate = mkdate(todaytuple) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 51 | print('Today is', format(todaytuple)) |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 52 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 53 | if bdaytuple > todaytuple: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 54 | print('You are a time traveler. Go back to the future!') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 55 | return |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 56 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 57 | if bdaytuple == todaytuple: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 58 | print('You were born today. Have a nice life!') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 59 | return |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 60 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 61 | days = todaydate - bdaydate |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 62 | print('You have lived', days, 'days') |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 63 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 64 | age = 0 |
| 65 | for y in range(year, todaytuple[0] + 1): |
| 66 | if bdaytuple < (y, month, day) <= todaytuple: |
| 67 | age = age + 1 |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 68 | |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 69 | print('You are', age, 'years old') |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 70 | |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 71 | if todaytuple[1:] == bdaytuple[1:]: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 72 | print('Congratulations! Today is your', nth(age), 'birthday') |
| 73 | print('Yesterday was your', end=' ') |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 74 | else: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 75 | print('Today is your', end=' ') |
| 76 | print(nth(days - age), 'unbirthday') |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 78 | def format(date): |
| 79 | (year, month, day) = date |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 80 | return '%d %s %d' % (day, calendar.month_name[month], year) |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 81 | |
| 82 | def nth(n): |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 83 | if n == 1: return '1st' |
| 84 | if n == 2: return '2nd' |
| 85 | if n == 3: return '3rd' |
| 86 | return '%dth' % n |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 87 | |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 88 | def mkdate(date): |
| 89 | # January 1st, in 0 A.D. is arbitrarily defined to be day 1, |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 90 | # even though that day never actually existed and the calendar |
| 91 | # was different then... |
Georg Brandl | 22fff43 | 2009-10-27 20:19:02 +0000 | [diff] [blame] | 92 | (year, month, day) = date |
| 93 | days = year*365 # years, roughly |
Benjamin Peterson | d7b0328 | 2008-09-13 15:58:53 +0000 | [diff] [blame] | 94 | days = days + (year+3)//4 # plus leap years, roughly |
| 95 | days = days - (year+99)//100 # minus non-leap years every century |
| 96 | days = days + (year+399)//400 # plus leap years every 4 centirues |
Moshe Zadka | 273ad45 | 2001-02-20 16:32:24 +0000 | [diff] [blame] | 97 | for i in range(1, month): |
| 98 | if i == 2 and calendar.isleap(year): |
| 99 | days = days + 29 |
| 100 | else: |
| 101 | days = days + calendar.mdays[i] |
| 102 | days = days + day |
| 103 | return days |
Guido van Rossum | f8ae54d | 1994-02-07 13:45:27 +0000 | [diff] [blame] | 104 | |
Johannes Gijsbers | 7a8c43e | 2004-09-11 16:34:35 +0000 | [diff] [blame] | 105 | if __name__ == "__main__": |
| 106 | main() |