| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
_MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May', |
| 45 |
'June', 'July', 'August', 'September', 'October', |
| 46 |
'November', 'December' ] |
| 47 |
|
| 48 |
_DAY_NAMES = [ 'Friday', 'Saturday', 'Sunday', 'Monday', |
| 49 |
'Tuesday', 'Wednesday', 'Thursday' ] |
| 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 |
|
| 60 |
_INT_TYPES = type(1), type(1L) |
| 61 |
|
| 62 |
def _is_leap(year): |
| 63 |
if year % 4 != 0: return 0 |
| 64 |
if year % 400 == 0: return 1 |
| 65 |
return year % 100 != 0 |
| 66 |
|
| 67 |
def _days_in_year(year): |
| 68 |
return 365 + _is_leap(year) |
| 69 |
|
| 70 |
def _days_before_year(year): |
| 71 |
return year*365L + (year+3)//4 - (year+99)//100 + (year+399)//400 |
| 72 |
|
| 73 |
def _days_in_month(month, year): |
| 74 |
if month == 2 and _is_leap(year): return 29 |
| 75 |
return _DAYS_IN_MONTH[month-1] |
| 76 |
|
| 77 |
def _days_before_month(month, year): |
| 78 |
return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year)) |
| 79 |
|
| 80 |
def _date2num(date): |
| 81 |
return _days_before_year(date.year) + \ |
| 82 |
_days_before_month(date.month, date.year) + \ |
| 83 |
date.day |
| 84 |
|
| 85 |
_DI400Y = _days_before_year(400) |
| 86 |
|
| 87 |
def _num2date(n): |
| 88 |
if type(n) not in _INT_TYPES: |
| 89 |
raise TypeError, 'argument must be integer: %r' % type(n) |
| 90 |
|
| 91 |
ans = Date(1,1,1) |
| 92 |
del ans.ord, ans.month, ans.day, ans.year |
| 93 |
ans.ord = n |
| 94 |
|
| 95 |
n400 = (n-1)//_DI400Y |
| 96 |
year, n = 400 * n400, n - _DI400Y * n400 |
| 97 |
more = n // 365 |
| 98 |
dby = _days_before_year(more) |
| 99 |
if dby >= n: |
| 100 |
more = more - 1 |
| 101 |
dby = dby - _days_in_year(more) |
| 102 |
year, n = year + more, int(n - dby) |
| 103 |
|
| 104 |
try: year = int(year) |
| 105 |
except (ValueError, OverflowError): pass |
| 106 |
|
| 107 |
month = min(n//29 + 1, 12) |
| 108 |
dbm = _days_before_month(month, year) |
| 109 |
if dbm >= n: |
| 110 |
month = month - 1 |
| 111 |
dbm = dbm - _days_in_month(month, year) |
| 112 |
|
| 113 |
ans.month, ans.day, ans.year = month, n-dbm, year |
| 114 |
return ans |
| 115 |
|
| 116 |
def _num2day(n): |
| 117 |
return _DAY_NAMES[ int(n % 7) ] |
| 118 |
|
| 119 |
|
| 120 |
class Date: |
| 121 |
def __init__(self, month, day, year): |
| 122 |
if not 1 <= month <= 12: |
| 123 |
raise ValueError, 'month must be in 1..12: %r' % (month,) |
| 124 |
dim = _days_in_month(month, year) |
| 125 |
if not 1 <= day <= dim: |
| 126 |
raise ValueError, 'day must be in 1..%r: %r' % (dim, day) |
| 127 |
self.month, self.day, self.year = month, day, year |
| 128 |
self.ord = _date2num(self) |
| 129 |
|
| 130 |
|
| 131 |
def __setattr__(self, name, value): |
| 132 |
if self.__dict__.has_key(name): |
| 133 |
raise AttributeError, 'read-only attribute ' + name |
| 134 |
self.__dict__[name] = value |
| 135 |
|
| 136 |
def __cmp__(self, other): |
| 137 |
return cmp(self.ord, other.ord) |
| 138 |
|
| 139 |
|
| 140 |
def __hash__(self): |
| 141 |
return hash(self.ord) |
| 142 |
|
| 143 |
|
| 144 |
def __repr__(self): |
| 145 |
return '%.3s %2d %.3s %r' % ( |
| 146 |
self.weekday(), |
| 147 |
self.day, |
| 148 |
_MONTH_NAMES[self.month-1], |
| 149 |
self.year) |
| 150 |
|
| 151 |
|
| 152 |
def __add__(self, n): |
| 153 |
if type(n) not in _INT_TYPES: |
| 154 |
raise TypeError, 'can\'t add %r to date' % type(n) |
| 155 |
return _num2date(self.ord + n) |
| 156 |
__radd__ = __add__ |
| 157 |
|
| 158 |
|
| 159 |
def __sub__(self, other): |
| 160 |
if type(other) in _INT_TYPES: |
| 161 |
return _num2date(self.ord - other) |
| 162 |
else: |
| 163 |
return self.ord - other.ord |
| 164 |
|
| 165 |
|
| 166 |
def __rsub__(self, other): |
| 167 |
raise TypeError, 'Can\'t subtract date from integer' |
| 168 |
|
| 169 |
def weekday(self): |
| 170 |
return _num2day(self.ord) |
| 171 |
|
| 172 |
def today(): |
| 173 |
import time |
| 174 |
local = time.localtime(time.time()) |
| 175 |
return Date(local[1], local[2], local[0]) |
| 176 |
|
| 177 |
class DateTestError(Exception): |
| 178 |
pass |
| 179 |
|
| 180 |
def test(firstyear, lastyear): |
| 181 |
a = Date(9,30,1913) |
| 182 |
b = Date(9,30,1914) |
| 183 |
if repr(a) != 'Tue 30 Sep 1913': |
| 184 |
raise DateTestError, '__repr__ failure' |
| 185 |
if (not a < b) or a == b or a > b or b != b: |
| 186 |
raise DateTestError, '__cmp__ failure' |
| 187 |
if a+365 != b or 365+a != b: |
| 188 |
raise DateTestError, '__add__ failure' |
| 189 |
if b-a != 365 or b-365 != a: |
| 190 |
raise DateTestError, '__sub__ failure' |
| 191 |
try: |
| 192 |
x = 1 - a |
| 193 |
raise DateTestError, 'int-date should have failed' |
| 194 |
except TypeError: |
| 195 |
pass |
| 196 |
try: |
| 197 |
x = a + b |
| 198 |
raise DateTestError, 'date+date should have failed' |
| 199 |
except TypeError: |
| 200 |
pass |
| 201 |
if a.weekday() != 'Tuesday': |
| 202 |
raise DateTestError, 'weekday() failure' |
| 203 |
if max(a,b) is not b or min(a,b) is not a: |
| 204 |
raise DateTestError, 'min/max failure' |
| 205 |
d = {a-1:b, b:a+1} |
| 206 |
if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913): |
| 207 |
raise DateTestError, 'dictionary failure' |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
lord = _days_before_year(firstyear) |
| 213 |
y = firstyear |
| 214 |
while y <= lastyear: |
| 215 |
ford = lord + 1 |
| 216 |
lord = ford + _days_in_year(y) - 1 |
| 217 |
fd, ld = Date(1,1,y), Date(12,31,y) |
| 218 |
if (fd.ord,ld.ord) != (ford,lord): |
| 219 |
raise DateTestError, ('date->num failed', y) |
| 220 |
fd, ld = _num2date(ford), _num2date(lord) |
| 221 |
if (1,1,y,12,31,y) != \ |
| 222 |
(fd.month,fd.day,fd.year,ld.month,ld.day,ld.year): |
| 223 |
raise DateTestError, ('num->date failed', y) |
| 224 |
y = y + 1 |
| 225 |
|
| 226 |
if __name__ == '__main__': |
| 227 |
test(1850, 2150) |