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 is not None:
20 nd._date = datetime.date(year, month, day)
21 return nd
23 if yo and type(other) == datetime.timedelta:
24 return Date(yo._date + other)
25 else:
26 return NotImplemented
28 if yo:
29 if type(other) == datetime.date:
30 return yo._date == other
31 elif type(other) == Date:
32 if other:
33 return yo._date == other._date
34 return False
35 else:
36 if type(other) == datetime.date:
37 return False
38 elif type(other) == Date:
39 if other:
40 return False
41 return True
42 return NotImplemented
44 if yo:
45 attribute = yo._date.__getattribute__(name)
46 return attribute
47 else:
48 raise AttributeError('null Date object has no attribute %s' % name)
50 if yo:
51 if type(other) == datetime.date:
52 return yo._date >= other
53 elif type(other) == Date:
54 if other:
55 return yo._date >= other._date
56 return False
57 else:
58 if type(other) == datetime.date:
59 return False
60 elif type(other) == Date:
61 if other:
62 return False
63 return True
64 return NotImplemented
66 if yo:
67 if type(other) == datetime.date:
68 return yo._date > other
69 elif type(other) == Date:
70 if other:
71 return yo._date > other._date
72 return True
73 else:
74 if type(other) == datetime.date:
75 return False
76 elif type(other) == Date:
77 if other:
78 return False
79 return False
80 return NotImplemented
84 if yo:
85 if type(other) == datetime.date:
86 return yo._date <= other
87 elif type(other) == Date:
88 if other:
89 return yo._date <= other._date
90 return False
91 else:
92 if type(other) == datetime.date:
93 return True
94 elif type(other) == Date:
95 if other:
96 return True
97 return True
98 return NotImplemented
100 if yo:
101 if type(other) == datetime.date:
102 return yo._date < other
103 elif type(other) == Date:
104 if other:
105 return yo._date < other._date
106 return False
107 else:
108 if type(other) == datetime.date:
109 return True
110 elif type(other) == Date:
111 if other:
112 return True
113 return False
114 return NotImplemented
116 if yo:
117 if type(other) == datetime.date:
118 return yo._date != other
119 elif type(other) == Date:
120 if other:
121 return yo._date != other._date
122 return True
123 else:
124 if type(other) == datetime.date:
125 return True
126 elif type(other) == Date:
127 if other:
128 return True
129 return False
130 return NotImplemented
132 if yo._date:
133 return True
134 return False
135 __radd__ = __add__
146 if yo:
147 return "Date(%d, %d, %d)" % yo.timetuple()[:3]
148 else:
149 return "Date()"
151 if yo:
152 return yo.isoformat()
153 return "no date"
164 if yo:
165 return yo._date
166 return None
167 @classmethod
172 @classmethod
175 @classmethod
177 if yyyymmdd == ' ':
178 return cls()
179 return cls(datetime.date(int(yyyymmdd[:4]), int(yyyymmdd[4:6]), int(yyyymmdd[6:])))
181 if yo:
182 return yo._date.strftime(format)
183 return '<no date>'
184 @classmethod
188 if yo:
189 return "%04d%02d%02d" % yo.timetuple()[:3]
190 else:
191 return ' '
192 Date.max = Date(datetime.date.max)
193 Date.min = Date(datetime.date.min)
195 "adds null capable datetime.datetime constructs"
196 __slots__ = ['_datetime']
197 - def __new__(cls, year=None, month=0, day=0, hour=0, minute=0, second=0, microsec=0):
230 if yo:
231 attribute = yo._datetime.__getattribute__(name)
232 return attribute
233 else:
234 raise AttributeError('null DateTime object has no attribute %s' % name)
318 if yo._datetime is not False:
319 return True
320 return False
321 __radd__ = __add__
332 if yo:
333 return "DateTime(%d, %d, %d, %d, %d, %d, %d, %d, %d)" % yo._datetime.timetuple()[:]
334 else:
335 return "DateTime()"
337 if yo:
338 return yo.isoformat()
339 return "no datetime"
349 @classmethod
355 if yo:
356 return Date(yo.year, yo.month, yo.day)
357 return Date()
359 if yo:
360 return yo._datetime
361 return None
362 @classmethod
368 @classmethod
371 @classmethod
375 if yo:
376 return Time(yo.hour, yo.minute, yo.second, yo.microsecond)
377 return Time()
378 @classmethod
381 @classmethod
384 DateTime.max = DateTime(datetime.datetime.max)
385 DateTime.min = DateTime(datetime.datetime.min)
387 "adds null capable datetime.time constructs"
388 __slots__ = ['_time']
389 - def __new__(cls, hour=None, minute=0, second=0, microsec=0):
401 if yo and type(other) == datetime.timedelta:
402 return Time(yo._time + other)
403 else:
404 return NotImplemented
406 if yo:
407 if type(other) == datetime.time:
408 return yo._time == other
409 elif type(other) == Time:
410 if other:
411 return yo._time == other._time
412 return False
413 else:
414 if type(other) == datetime.time:
415 return False
416 elif type(other) == Time:
417 if other:
418 return False
419 return True
420 return NotImplemented
422 if yo:
423 attribute = yo._time.__getattribute__(name)
424 return attribute
425 else:
426 raise AttributeError('null Time object has no attribute %s' % name)
428 if yo:
429 if type(other) == datetime.time:
430 return yo._time >= other
431 elif type(other) == Time:
432 if other:
433 return yo._time >= other._time
434 return False
435 else:
436 if type(other) == datetime.time:
437 return False
438 elif type(other) == Time:
439 if other:
440 return False
441 return True
442 return NotImplemented
444 if yo:
445 if type(other) == datetime.time:
446 return yo._time > other
447 elif type(other) == DateTime:
448 if other:
449 return yo._time > other._time
450 return True
451 else:
452 if type(other) == datetime.time:
453 return False
454 elif type(other) == Time:
455 if other:
456 return False
457 return False
458 return NotImplemented
462 if yo:
463 if type(other) == datetime.time:
464 return yo._time <= other
465 elif type(other) == Time:
466 if other:
467 return yo._time <= other._time
468 return False
469 else:
470 if type(other) == datetime.time:
471 return True
472 elif type(other) == Time:
473 if other:
474 return True
475 return True
476 return NotImplemented
478 if yo:
479 if type(other) == datetime.time:
480 return yo._time < other
481 elif type(other) == Time:
482 if other:
483 return yo._time < other._time
484 return False
485 else:
486 if type(other) == datetime.time:
487 return True
488 elif type(other) == Time:
489 if other:
490 return True
491 return False
492 return NotImplemented
494 if yo:
495 if type(other) == datetime.time:
496 return yo._time != other
497 elif type(other) == Time:
498 if other:
499 return yo._time != other._time
500 return True
501 else:
502 if type(other) == datetime.time:
503 return True
504 elif type(other) == Time:
505 if other:
506 return True
507 return False
508 return NotImplemented
510 if yo._time is not False:
511 return True
512 return False
513 __radd__ = __add__
524 if yo:
525 return "Time(%d, %d, %d, %d)" % (yo.hour, yo.minute, yo.second, yo.microsecond)
526 else:
527 return "Time()"
529 if yo:
530 return yo.isoformat()
531 return "no time"
541 Time.max = Time(datetime.time.max)
542 Time.min = Time(datetime.time.min)
543