1 """wrappers around datetime objects to allow null values"""
2
3 import datetime
4 import time
5
6
7 -class Date(object):
8 "adds null capable datetime.date constructs"
9 __slots__ = ['_date']
10 - def __new__(cls, year=None, month=0, day=0):
11 """date should be either a datetime.date, a string in yyyymmdd format,
12 or date/month/day should all be appropriate integers"""
13 nd = object.__new__(cls)
14 nd._date = False
15 if type(year) == datetime.date:
16 nd._date = year
17 elif type(year) == Date:
18 nd._date = year._date
19 elif year == 'no date':
20 pass
21 elif year is not None:
22 nd._date = datetime.date(year, month, day)
23 return nd
25 if yo and type(other) == datetime.timedelta:
26 return Date(yo._date + other)
27 else:
28 return NotImplemented
30 if yo:
31 if type(other) == datetime.date:
32 return yo._date == other
33 elif type(other) == Date:
34 if other:
35 return yo._date == other._date
36 return False
37 else:
38 if type(other) == datetime.date:
39 return False
40 elif type(other) == Date:
41 if other:
42 return False
43 return True
44 return NotImplemented
46 if yo:
47 attribute = yo._date.__getattribute__(name)
48 return attribute
49 else:
50 raise AttributeError('null Date object has no attribute %s' % name)
52 if yo:
53 if type(other) == datetime.date:
54 return yo._date >= other
55 elif type(other) == Date:
56 if other:
57 return yo._date >= other._date
58 return False
59 else:
60 if type(other) == datetime.date:
61 return False
62 elif type(other) == Date:
63 if other:
64 return False
65 return True
66 return NotImplemented
68 if yo:
69 if type(other) == datetime.date:
70 return yo._date > other
71 elif type(other) == Date:
72 if other:
73 return yo._date > other._date
74 return True
75 else:
76 if type(other) == datetime.date:
77 return False
78 elif type(other) == Date:
79 if other:
80 return False
81 return False
82 return NotImplemented
86 if yo:
87 if type(other) == datetime.date:
88 return yo._date <= other
89 elif type(other) == Date:
90 if other:
91 return yo._date <= other._date
92 return False
93 else:
94 if type(other) == datetime.date:
95 return True
96 elif type(other) == Date:
97 if other:
98 return True
99 return True
100 return NotImplemented
102 if yo:
103 if type(other) == datetime.date:
104 return yo._date < other
105 elif type(other) == Date:
106 if other:
107 return yo._date < other._date
108 return False
109 else:
110 if type(other) == datetime.date:
111 return True
112 elif type(other) == Date:
113 if other:
114 return True
115 return False
116 return NotImplemented
118 if yo:
119 if type(other) == datetime.date:
120 return yo._date != other
121 elif type(other) == Date:
122 if other:
123 return yo._date != other._date
124 return True
125 else:
126 if type(other) == datetime.date:
127 return True
128 elif type(other) == Date:
129 if other:
130 return True
131 return False
132 return NotImplemented
134 if yo._date:
135 return True
136 return False
137 __radd__ = __add__
148 if yo:
149 return "Date(%d, %d, %d)" % yo.timetuple()[:3]
150 else:
151 return "Date()"
153 if yo:
154 return yo.isoformat()
155 return "no date"
166 if yo:
167 return yo._date
168 return None
169 @classmethod
174 @classmethod
177 @classmethod
179 if yyyymmdd in ('', ' ','no date'):
180 return cls()
181 return cls(datetime.date(int(yyyymmdd[:4]), int(yyyymmdd[4:6]), int(yyyymmdd[6:])))
183 if yo:
184 return yo._date.strftime(format)
185 return '<no date>'
186 @classmethod
190 if yo:
191 return "%04d%02d%02d" % yo.timetuple()[:3]
192 else:
193 return ' '
194 Date.max = Date(datetime.date.max)
195 Date.min = Date(datetime.date.min)
197 "adds null capable datetime.datetime constructs"
198 __slots__ = ['_datetime']
199 - def __new__(cls, year=None, month=0, day=0, hour=0, minute=0, second=0, microsec=0):
232 if yo:
233 attribute = yo._datetime.__getattribute__(name)
234 return attribute
235 else:
236 raise AttributeError('null DateTime object has no attribute %s' % name)
320 if yo._datetime is not False:
321 return True
322 return False
323 __radd__ = __add__
334 if yo:
335 return "DateTime(%d, %d, %d, %d, %d, %d, %d, %d, %d)" % yo._datetime.timetuple()[:]
336 else:
337 return "DateTime()"
339 if yo:
340 return yo.isoformat()
341 return "no datetime"
351 @classmethod
357 if yo:
358 return Date(yo.year, yo.month, yo.day)
359 return Date()
361 if yo:
362 return yo._datetime
363 return None
364 @classmethod
370 @classmethod
373 @classmethod
377 if yo:
378 return Time(yo.hour, yo.minute, yo.second, yo.microsecond)
379 return Time()
380 @classmethod
383 @classmethod
386 DateTime.max = DateTime(datetime.datetime.max)
387 DateTime.min = DateTime(datetime.datetime.min)
389 "adds null capable datetime.time constructs"
390 __slots__ = ['_time']
391 - def __new__(cls, hour=None, minute=0, second=0, microsec=0):
403 if yo and type(other) == datetime.timedelta:
404 return Time(yo._time + other)
405 else:
406 return NotImplemented
408 if yo:
409 if type(other) == datetime.time:
410 return yo._time == other
411 elif type(other) == Time:
412 if other:
413 return yo._time == other._time
414 return False
415 else:
416 if type(other) == datetime.time:
417 return False
418 elif type(other) == Time:
419 if other:
420 return False
421 return True
422 return NotImplemented
424 if yo:
425 attribute = yo._time.__getattribute__(name)
426 return attribute
427 else:
428 raise AttributeError('null Time object has no attribute %s' % name)
430 if yo:
431 if type(other) == datetime.time:
432 return yo._time >= other
433 elif type(other) == Time:
434 if other:
435 return yo._time >= other._time
436 return False
437 else:
438 if type(other) == datetime.time:
439 return False
440 elif type(other) == Time:
441 if other:
442 return False
443 return True
444 return NotImplemented
446 if yo:
447 if type(other) == datetime.time:
448 return yo._time > other
449 elif type(other) == DateTime:
450 if other:
451 return yo._time > other._time
452 return True
453 else:
454 if type(other) == datetime.time:
455 return False
456 elif type(other) == Time:
457 if other:
458 return False
459 return False
460 return NotImplemented
464 if yo:
465 if type(other) == datetime.time:
466 return yo._time <= other
467 elif type(other) == Time:
468 if other:
469 return yo._time <= other._time
470 return False
471 else:
472 if type(other) == datetime.time:
473 return True
474 elif type(other) == Time:
475 if other:
476 return True
477 return True
478 return NotImplemented
480 if yo:
481 if type(other) == datetime.time:
482 return yo._time < other
483 elif type(other) == Time:
484 if other:
485 return yo._time < other._time
486 return False
487 else:
488 if type(other) == datetime.time:
489 return True
490 elif type(other) == Time:
491 if other:
492 return True
493 return False
494 return NotImplemented
496 if yo:
497 if type(other) == datetime.time:
498 return yo._time != other
499 elif type(other) == Time:
500 if other:
501 return yo._time != other._time
502 return True
503 else:
504 if type(other) == datetime.time:
505 return True
506 elif type(other) == Time:
507 if other:
508 return True
509 return False
510 return NotImplemented
512 if yo._time is not False:
513 return True
514 return False
515 __radd__ = __add__
526 if yo:
527 return "Time(%d, %d, %d, %d)" % (yo.hour, yo.minute, yo.second, yo.microsecond)
528 else:
529 return "Time()"
531 if yo:
532 return yo.isoformat()
533 return "no time"
543 Time.max = Time(datetime.time.max)
544 Time.min = Time(datetime.time.min)
545