Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 1 | # Class Date supplies date objects that support date arithmetic. |
| 2 | # |
| 3 | # Date(month,day,year) returns a Date object. An instance prints as, |
| 4 | # e.g., 'Mon 16 Aug 1993'. |
| 5 | # |
| 6 | # Addition, subtraction, comparison operators, min, max, and sorting |
| 7 | # all work as expected for date objects: int+date or date+int returns |
| 8 | # the date `int' days from `date'; date+date raises an exception; |
| 9 | # date-int returns the date `int' days before `date'; date2-date1 returns |
| 10 | # an integer, the number of days from date1 to date2; int-date raises an |
| 11 | # exception; date1 < date2 is true iff date1 occurs before date2 (& |
| 12 | # similarly for other comparisons); min(date1,date2) is the earlier of |
| 13 | # the two dates and max(date1,date2) the later; and date objects can be |
| 14 | # used as dictionary keys. |
| 15 | # |
| 16 | # Date objects support one visible method, date.weekday(). This returns |
| 17 | # the day of the week the date falls on, as a string. |
| 18 | # |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 19 | # Date objects also have 4 read-only data attributes: |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 20 | # .month in 1..12 |
| 21 | # .day in 1..31 |
| 22 | # .year int or long int |
| 23 | # .ord the ordinal of the date relative to an arbitrary staring point |
| 24 | # |
| 25 | # The Dates module also supplies function today(), which returns the |
| 26 | # current date as a date object. |
| 27 | # |
| 28 | # Those entranced by calendar trivia will be disappointed, as no attempt |
| 29 | # has been made to accommodate the Julian (etc) system. On the other |
| 30 | # hand, at least this package knows that 2000 is a leap year but 2100 |
| 31 | # isn't, and works fine for years with a hundred decimal digits <wink>. |
| 32 | |
| 33 | # Tim Peters tim@ksr.com |
| 34 | # not speaking for Kendall Square Research Corp |
| 35 | |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 36 | # Adapted to Python 1.1 (where some hacks to overcome coercion are unnecessary) |
| 37 | # by Guido van Rossum |
| 38 | |
Martin v. Löwis | 7890c26 | 2003-06-07 19:39:56 +0000 | [diff] [blame] | 39 | # Note that as of Python 2.3, a datetime module is included in the stardard |
| 40 | # library. |
| 41 | |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 42 | # vi:set tabsize=8: |
| 43 | |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 44 | _MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May', |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 45 | 'June', 'July', 'August', 'September', 'October', |
| 46 | 'November', 'December' ] |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 47 | |
| 48 | _DAY_NAMES = [ 'Friday', 'Saturday', 'Sunday', 'Monday', |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 49 | 'Tuesday', 'Wednesday', 'Thursday' ] |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 50 | |
| 51 | _DAYS_IN_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] |
| 52 | |
| 53 | _DAYS_BEFORE_MONTH = [] |
| 54 | dbm = 0 |
| 55 | for dim in _DAYS_IN_MONTH: |
| 56 | _DAYS_BEFORE_MONTH.append(dbm) |
| 57 | dbm = dbm + dim |
| 58 | del dbm, dim |
| 59 | |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 60 | _INT_TYPES = type(1), type(1) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 62 | def _is_leap(year): # 1 if leap year, else 0 |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 63 | if year % 4 != 0: return 0 |
| 64 | if year % 400 == 0: return 1 |
| 65 | return year % 100 != 0 |
| 66 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 67 | def _days_in_year(year): # number of days in year |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 68 | return 365 + _is_leap(year) |
| 69 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 70 | def _days_before_year(year): # number of days before year |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 71 | return year*365 + (year+3)/4 - (year+99)/100 + (year+399)/400 |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 72 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 73 | def _days_in_month(month, year): # number of days in month of year |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 74 | if month == 2 and _is_leap(year): return 29 |
| 75 | return _DAYS_IN_MONTH[month-1] |
| 76 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 77 | def _days_before_month(month, year): # number of days in year before month |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 78 | return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year)) |
| 79 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 80 | def _date2num(date): # compute ordinal of date.month,day,year |
| 81 | return _days_before_year(date.year) + \ |
| 82 | _days_before_month(date.month, date.year) + \ |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 83 | date.day |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 84 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 85 | _DI400Y = _days_before_year(400) # number of days in 400 years |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 86 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 87 | def _num2date(n): # return date with ordinal n |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 88 | if type(n) not in _INT_TYPES: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 89 | raise TypeError('argument must be integer: %r' % type(n)) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 91 | ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 92 | del ans.ord, ans.month, ans.day, ans.year # un-initialize it |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 93 | ans.ord = n |
| 94 | |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 95 | n400 = (n-1)/_DI400Y # # of 400-year blocks preceding |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 96 | year, n = 400 * n400, n - _DI400Y * n400 |
| 97 | more = n / 365 |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 98 | dby = _days_before_year(more) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 99 | if dby >= n: |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 100 | more = more - 1 |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 101 | dby = dby - _days_in_year(more) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 102 | year, n = year + more, int(n - dby) |
| 103 | |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 104 | try: year = int(year) # chop to int, if it fits |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 105 | except (ValueError, OverflowError): pass |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 106 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 107 | month = min(n/29 + 1, 12) |
| 108 | dbm = _days_before_month(month, year) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 109 | if dbm >= n: |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 110 | month = month - 1 |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 111 | dbm = dbm - _days_in_month(month, year) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 112 | |
| 113 | ans.month, ans.day, ans.year = month, n-dbm, year |
| 114 | return ans |
| 115 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 116 | def _num2day(n): # return weekday name of day with ordinal n |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 117 | return _DAY_NAMES[ int(n % 7) ] |
| 118 | |
| 119 | |
| 120 | class Date: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 121 | def __init__(self, month, day, year): |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 122 | if not 1 <= month <= 12: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 123 | raise ValueError('month must be in 1..12: %r' % (month,)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 124 | dim = _days_in_month(month, year) |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 125 | if not 1 <= day <= dim: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 126 | raise ValueError('day must be in 1..%r: %r' % (dim, day)) |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 127 | self.month, self.day, self.year = month, day, year |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 128 | self.ord = _date2num(self) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 130 | # don't allow setting existing attributes |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 131 | def __setattr__(self, name, value): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 132 | if name in self.__dict__: |
| 133 | raise AttributeError('read-only attribute ' + name) |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 134 | self.__dict__[name] = value |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 135 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 136 | def __cmp__(self, other): |
| 137 | return cmp(self.ord, other.ord) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 138 | |
| 139 | # define a hash function so dates can be used as dictionary keys |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 140 | def __hash__(self): |
| 141 | return hash(self.ord) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 142 | |
| 143 | # print as, e.g., Mon 16 Aug 1993 |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 144 | def __repr__(self): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 145 | return '%.3s %2d %.3s %r' % ( |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 146 | self.weekday(), |
| 147 | self.day, |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 148 | _MONTH_NAMES[self.month-1], |
| 149 | self.year) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 151 | # Python 1.1 coerces neither int+date nor date+int |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 152 | def __add__(self, n): |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 153 | if type(n) not in _INT_TYPES: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 154 | raise TypeError('can\'t add %r to date' % type(n)) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 155 | return _num2date(self.ord + n) |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 156 | __radd__ = __add__ # handle int+date |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 158 | # Python 1.1 coerces neither date-int nor date-date |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 159 | def __sub__(self, other): |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 160 | if type(other) in _INT_TYPES: # date-int |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 161 | return _num2date(self.ord - other) |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 162 | else: |
| 163 | return self.ord - other.ord # date-date |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 165 | # complain about int-date |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 166 | def __rsub__(self, other): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 167 | raise TypeError('Can\'t subtract date from integer') |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 168 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 169 | def weekday(self): |
| 170 | return _num2day(self.ord) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 172 | def today(): |
| 173 | import time |
| 174 | local = time.localtime(time.time()) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 175 | return Date(local[1], local[2], local[0]) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 176 | |
| 177 | DateTestError = 'DateTestError' |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 178 | def test(firstyear, lastyear): |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 179 | a = Date(9,30,1913) |
| 180 | b = Date(9,30,1914) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 181 | if repr(a) != 'Tue 30 Sep 1913': |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 182 | raise DateTestError('__repr__ failure') |
Guido van Rossum | 2e61103 | 1994-10-09 22:36:28 +0000 | [diff] [blame] | 183 | if (not a < b) or a == b or a > b or b != b: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 184 | raise DateTestError('__cmp__ failure') |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 185 | if a+365 != b or 365+a != b: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 186 | raise DateTestError('__add__ failure') |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 187 | if b-a != 365 or b-365 != a: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 188 | raise DateTestError('__sub__ failure') |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 189 | try: |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 190 | x = 1 - a |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 191 | raise DateTestError('int-date should have failed') |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 192 | except TypeError: |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 193 | pass |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 194 | try: |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 195 | x = a + b |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 196 | raise DateTestError('date+date should have failed') |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 197 | except TypeError: |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 198 | pass |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 199 | if a.weekday() != 'Tuesday': |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 200 | raise DateTestError('weekday() failure') |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 201 | if max(a,b) is not b or min(a,b) is not a: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 202 | raise DateTestError('min/max failure') |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 203 | d = {a-1:b, b:a+1} |
| 204 | if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 205 | raise DateTestError('dictionary failure') |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 206 | |
| 207 | # verify date<->number conversions for first and last days for |
| 208 | # all years in firstyear .. lastyear |
| 209 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 210 | lord = _days_before_year(firstyear) |
Guido van Rossum | 102abab | 1993-10-30 12:38:16 +0000 | [diff] [blame] | 211 | y = firstyear |
| 212 | while y <= lastyear: |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 213 | ford = lord + 1 |
| 214 | lord = ford + _days_in_year(y) - 1 |
| 215 | fd, ld = Date(1,1,y), Date(12,31,y) |
| 216 | if (fd.ord,ld.ord) != (ford,lord): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 217 | raise DateTestError('date->num failed', y) |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 218 | fd, ld = _num2date(ford), _num2date(lord) |
| 219 | if (1,1,y,12,31,y) != \ |
| 220 | (fd.month,fd.day,fd.year,ld.month,ld.day,ld.year): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 221 | raise DateTestError('num->date failed', y) |
Guido van Rossum | dcd038f | 1998-09-14 15:34:45 +0000 | [diff] [blame] | 222 | y = y + 1 |