3 from types import InstanceType
9 def calculate(self, year, month):
10 return [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] + (month == 2 and self.isleap(year))
12 def isleap(self, year):
13 return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
17 for year in range (1,9999):
18 for month in range(1,12):
19 if DaysInMonth().calculate(year, month) <> calendar.monthrange(year, month)[1]:
20 print "Failed on %s-%s."%(year, month)
28 def weekday(self, year, month, day):
29 secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
30 tuple = localtime(secs)
35 daysInMonth = DaysInMonth()
38 def __init__(self,year,month,day,hour=0,minute=0,second=0,microsecond=0):
39 dates = ['year','month','day','hour','minute']
41 for item in [year,month,day,hour,minute]:
42 if type(item) not in [type(1), type(1L)]:
43 raise TypeError("The variable '%s' should be an integer."%dates[counter])
46 if type(second) not in [type(1), type(1L)]:# and type(second) <> type(1.004):
47 raise ValueError("The variable 'second' should be an Integer or a Long.")# or a float.")
49 # Very basic error checking and initialisation.
50 if year < MINYEAR or year > MAXYEAR:
51 raise ValueError('The year value must be between %s and %s inclusive.'%(MINYEAR, MAXYEAR))
54 if month < 1 or month > 12:
55 raise ValueError('The month value must be between 1 and 12 inclusive.')
58 if day < 1 or day > daysInMonth.calculate(year, month):
59 raise ValueError('The day value must be between 1 and %s inclusive.'%daysInMonth.calculate(year, month))
62 if hour < 0 or hour > 23:
63 raise ValueError('The hour value must be between 0 and 23 inclusive.')
66 if minute < 0 or minute > 59:
67 raise ValueError('The minutes value must be between 0 and 59 inclusive.')
70 if second < 0 or second > 59:
71 raise ValueError('The seconds value must be between 0 and 59 inclusive.')
74 if microsecond < 0 or microsecond > 1000000:
75 raise ValueError('The microseconds value must be between 0 and 1000000 inclusive.')
77 self.microsecond = microsecond
81 return datetime(now[0],now[1],now[2],now[3],now[4],now[5])
82 now = staticmethod(now)
85 # Comparison Operators.
88 def _compareDate(self, other):
89 if self.year == other.year:
90 if self.month == other.month:
91 if self.day == other.day:
93 elif self.day > other.day:
97 elif self.month > other.month:
101 elif self.year > other.year:
106 def _compareTime(self, other):
107 if self.hour == other.hour:
108 if self.minute == other.minute:
109 if self.second == other.second:
111 elif self.second > other.second:
115 elif self.minute > other.minute:
119 elif self.hour > other.hour:
125 def __cmp__(self, other):
126 if type(other) is type(None):
127 raise Exception('Comparison of %s (%s) with %s (%s) is not supported'%(self,type(self),other,type(other)))
128 elif type(other) is InstanceType:
129 # if other.__class__.__name__ == self.__class__.__name__:
130 if other.__class__.__name__ == 'date':
131 return self._compareDate(other)
132 elif other.__class__.__name__ == 'time':
133 return self._compareTime(other)
134 elif other.__class__.__name__ == 'datetime':
135 date = self._compareDate(other)
137 return self._compareTime(other)
141 raise Exception('Comparison of %s (%s) with %s (%s) is not supported'%(self,type(self),other,type(other)))
143 # raise Exception('Comparison of %s (%s) with %s (%s) is not supported'%(self,self.__class__,other,other.__class__))
145 raise Exception('Comparison of %s (%s) with %s (%s) is not supported'%(self,type(self),other,type(other)))
147 def __eq__(self, other):
148 if type(other) is InstanceType:
149 if other.__class__.__name__ == self.__class__.__name__:
150 if other.__class__.__name__ == 'date':
151 if self._compareDate(other) == 0:
155 elif other.__class__.__name__ == 'time':
156 if self._compareTime(other) == 0:
160 elif other.__class__.__name__ == 'datetime':
161 date = self._compareDate(other)
163 if self._compareTime(other) == 0:
176 def __ne__(self, other):
177 if self.__eq__(other):
183 return self.isoformat()
186 return "datetime.datetime(%s,%s,%s,%s,%s,%s)"%(self.year, self.month, self.day, self.hour, self.minute, self.second)
188 def __getitem__(self, item):
191 elif item == 'month':
197 elif item == 'minute':
199 elif item == 'second':
202 raise KeyError("'%s' is not a valid attribute for a Date class."%item)
208 def _addZeros(self,num,s):
210 while( len(s) < num ):
215 return str(self._addZeros(4,self.year))+"-"+str(self._addZeros(2,self.month))+"-"+str(self._addZeros(2,self.day))
218 return str(self._addZeros(2,self.hour))+":"+str(self._addZeros(2,self.minute))+":"+str(self._addZeros(2,self.second))#str(self._addZeros(2,int(self.second)))+'.'+s
220 def strftime(self, format):
221 #raise Exception(self.timetuple())
222 return t.strftime(format, self.timetuple())
231 sql = self.isoformat()
232 wday = calendar.weekday(int(sql[0:4]),int(sql[5:7]),int(sql[8:10]))
233 return (int(sql[0:4]),int(sql[5:7]),int(sql[8:10]),int(sql[11:13]),int(sql[14:16]),int(sql[17:19]),wday,0,-1)#,0,0,-1) ,0,0,-1)
236 return self._isodate() + ' ' + self._isotime()
239 class date(datetime):
240 def __init__(self,year,month,day):
242 dates = ['year','month','day']
244 for item in [year,month,day]:
245 if type(item) not in [ type(1), type(1L)]:
246 raise TypeError("The variable '%s' should be an Integer or a Long."%dates[counter])
250 # Very basic error checking and initialisation.
251 if year < MINYEAR or year > MAXYEAR:
252 raise ValueError('The year value must be between %s and %s inclusive.'%(MINYEAR, MAXYEAR))
255 if month < 1 or month > 12:
256 raise ValueError('The month value must be between 1 and 12 inclusive.')
259 if day < 1 or day > daysInMonth.calculate(year, month):
260 raise ValueError('The day value must be between 1 and %s inclusive.'%daysInMonth.calculate(year, month))
265 return "datetime.date(%s,%s,%s)"%(self.year,self.month, self.day)
268 return self._isodate()
271 sql = self.isoformat()
272 wday = calendar.weekday(int(sql[0:4]),int(sql[5:7]),int(sql[8:10]))
273 return (int(sql[0:4]),int(sql[5:7]),int(sql[8:10]),0,0,0,wday,0,-1)
277 return date(now[0],now[1],now[2])
278 now = staticmethod(now)
280 def in_x_days(number_of_days=0):
281 secs = t.mktime(t.localtime())
282 day_secs = secs+24*60*60*number_of_days
283 day = t.localtime(day_secs)
284 return date(day[0],day[1],day[2])
285 in_x_days=staticmethod(in_x_days)
288 return date.in_x_days(1)
289 tomorrow = staticmethod(tomorrow)
291 class time(datetime):
293 def __init__(self,hour=0,minute=0,second=0,microsecond=0):
295 dates = ['hour','minute']
297 for item in [hour,minute]:
298 if type(item) not in [type(1), type(1L)]:
299 raise TypeError("The variable '%s' should be an Integer or a Long."%dates[counter])
302 if type(second) <> type(1):# and type(second) <> type(1.004):
303 raise ValueError("The variable 'second' should be an integer.")# or a float.")
305 # Very basic error checking and initialisation.
307 if hour < 0 or hour > 23:
308 raise ValueError('The hour value must be between 0 and 23 inclusive.')
311 if minute < 0 or minute > 59:
312 raise ValueError('The minutes value must be between 0 and 59 inclusive.')
315 if second < 0 or second > 59:
316 raise ValueError('The seconds value must be between 0 and 59 inclusive.')
319 if microsecond < 0 or microsecond > 1000000:
320 raise ValueError('The microseconds value must be between 0 and 1000000 inclusive.')
322 self.microsecond = microsecond
325 return "datetime.time(%s,%s,%s)"%(self.hour, self.minute, self.second)
328 return self._isotime()
331 raise AttributeError('time objects do not have a timetuple method.')
335 return time(now[3],now[4],now[5])