59
59
60
60
_INT_TYPES = type (1 ), type (1L )
61
61
62
- def _is_leap ( year ): # 1 if leap year, else 0
62
+ def _is_leap (year ): # 1 if leap year, else 0
63
63
if year % 4 != 0 : return 0
64
64
if year % 400 == 0 : return 1
65
65
return year % 100 != 0
66
66
67
- def _days_in_year ( year ): # number of days in year
67
+ def _days_in_year (year ): # number of days in year
68
68
return 365 + _is_leap (year )
69
69
70
- def _days_before_year ( year ): # number of days before year
70
+ def _days_before_year (year ): # number of days before year
71
71
return year * 365L + (year + 3 )/ 4 - (year + 99 )/ 100 + (year + 399 )/ 400
72
72
73
- def _days_in_month ( month , year ): # number of days in month of year
73
+ def _days_in_month (month , year ): # number of days in month of year
74
74
if month == 2 and _is_leap (year ): return 29
75
75
return _DAYS_IN_MONTH [month - 1 ]
76
76
77
- def _days_before_month ( month , year ): # number of days in year before month
77
+ def _days_before_month (month , year ): # number of days in year before month
78
78
return _DAYS_BEFORE_MONTH [month - 1 ] + (month > 2 and _is_leap (year ))
79
79
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 ) + \
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 ) + \
83
83
date .day
84
84
85
- _DI400Y = _days_before_year ( 400 ) # number of days in 400 years
85
+ _DI400Y = _days_before_year (400 ) # number of days in 400 years
86
86
87
- def _num2date ( n ): # return date with ordinal n
87
+ def _num2date (n ): # return date with ordinal n
88
88
if type (n ) not in _INT_TYPES :
89
89
raise TypeError , 'argument must be integer: %r' % type (n )
90
90
@@ -95,87 +95,87 @@ def _num2date( n ): # return date with ordinal n
95
95
n400 = (n - 1 )/ _DI400Y # # of 400-year blocks preceding
96
96
year , n = 400 * n400 , n - _DI400Y * n400
97
97
more = n / 365
98
- dby = _days_before_year ( more )
98
+ dby = _days_before_year (more )
99
99
if dby >= n :
100
100
more = more - 1
101
- dby = dby - _days_in_year ( more )
101
+ dby = dby - _days_in_year (more )
102
102
year , n = year + more , int (n - dby )
103
103
104
104
try : year = int (year ) # chop to int, if it fits
105
105
except (ValueError , OverflowError ): pass
106
106
107
- month = min ( n / 29 + 1 , 12 )
108
- dbm = _days_before_month ( month , year )
107
+ month = min (n / 29 + 1 , 12 )
108
+ dbm = _days_before_month (month , year )
109
109
if dbm >= n :
110
110
month = month - 1
111
- dbm = dbm - _days_in_month ( month , year )
111
+ dbm = dbm - _days_in_month (month , year )
112
112
113
113
ans .month , ans .day , ans .year = month , n - dbm , year
114
114
return ans
115
115
116
- def _num2day ( n ): # return weekday name of day with ordinal n
116
+ def _num2day (n ): # return weekday name of day with ordinal n
117
117
return _DAY_NAMES [ int (n % 7 ) ]
118
118
119
119
120
120
class Date :
121
- def __init__ ( self , month , day , year ):
121
+ def __init__ (self , month , day , year ):
122
122
if not 1 <= month <= 12 :
123
123
raise ValueError , 'month must be in 1..12: %r' % (month ,)
124
- dim = _days_in_month ( month , year )
124
+ dim = _days_in_month (month , year )
125
125
if not 1 <= day <= dim :
126
126
raise ValueError , 'day must be in 1..%r: %r' % (dim , day )
127
127
self .month , self .day , self .year = month , day , year
128
- self .ord = _date2num ( self )
128
+ self .ord = _date2num (self )
129
129
130
130
# don't allow setting existing attributes
131
- def __setattr__ ( self , name , value ):
131
+ def __setattr__ (self , name , value ):
132
132
if self .__dict__ .has_key (name ):
133
133
raise AttributeError , 'read-only attribute ' + name
134
134
self .__dict__ [name ] = value
135
135
136
- def __cmp__ ( self , other ):
137
- return cmp ( self .ord , other .ord )
136
+ def __cmp__ (self , other ):
137
+ return cmp (self .ord , other .ord )
138
138
139
139
# define a hash function so dates can be used as dictionary keys
140
- def __hash__ ( self ):
141
- return hash ( self .ord )
140
+ def __hash__ (self ):
141
+ return hash (self .ord )
142
142
143
143
# print as, e.g., Mon 16 Aug 1993
144
- def __repr__ ( self ):
144
+ def __repr__ (self ):
145
145
return '%.3s %2d %.3s %r' % (
146
146
self .weekday (),
147
147
self .day ,
148
148
_MONTH_NAMES [self .month - 1 ],
149
149
self .year )
150
150
151
151
# Python 1.1 coerces neither int+date nor date+int
152
- def __add__ ( self , n ):
152
+ def __add__ (self , n ):
153
153
if type (n ) not in _INT_TYPES :
154
154
raise TypeError , 'can\' t add %r to date' % type (n )
155
- return _num2date ( self .ord + n )
155
+ return _num2date (self .ord + n )
156
156
__radd__ = __add__ # handle int+date
157
157
158
158
# Python 1.1 coerces neither date-int nor date-date
159
- def __sub__ ( self , other ):
159
+ def __sub__ (self , other ):
160
160
if type (other ) in _INT_TYPES : # date-int
161
- return _num2date ( self .ord - other )
161
+ return _num2date (self .ord - other )
162
162
else :
163
163
return self .ord - other .ord # date-date
164
164
165
165
# complain about int-date
166
- def __rsub__ ( self , other ):
166
+ def __rsub__ (self , other ):
167
167
raise TypeError , 'Can\' t subtract date from integer'
168
168
169
- def weekday ( self ):
170
- return _num2day ( self .ord )
169
+ def weekday (self ):
170
+ return _num2day (self .ord )
171
171
172
172
def today ():
173
173
import time
174
174
local = time .localtime (time .time ())
175
- return Date ( local [1 ], local [2 ], local [0 ] )
175
+ return Date (local [1 ], local [2 ], local [0 ])
176
176
177
177
DateTestError = 'DateTestError'
178
- def test ( firstyear , lastyear ):
178
+ def test (firstyear , lastyear ):
179
179
a = Date (9 ,30 ,1913 )
180
180
b = Date (9 ,30 ,1914 )
181
181
if repr (a ) != 'Tue 30 Sep 1913' :
@@ -207,7 +207,7 @@ def test( firstyear, lastyear ):
207
207
# verify date<->number conversions for first and last days for
208
208
# all years in firstyear .. lastyear
209
209
210
- lord = _days_before_year ( firstyear )
210
+ lord = _days_before_year (firstyear )
211
211
y = firstyear
212
212
while y <= lastyear :
213
213
ford = lord + 1
0 commit comments