Package dbf :: Package old :: Module dates
[hide private]

Source Code for Module dbf.old.dates

  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 # date object is already False 21 elif year is not None: 22 nd._date = datetime.date(year, month, day) 23 return nd
24 - def __add__(yo, other):
25 if yo and type(other) == datetime.timedelta: 26 return Date(yo._date + other) 27 else: 28 return NotImplemented
29 - def __eq__(yo, other):
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
45 - def __getattr__(yo, name):
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)
51 - def __ge__(yo, other):
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
67 - def __gt__(yo, other):
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
83 - def __hash__(yo):
84 return yo._date.__hash__()
85 - def __le__(yo, other):
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
101 - def __lt__(yo, other):
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
117 - def __ne__(yo, other):
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
133 - def __nonzero__(yo):
134 if yo._date: 135 return True 136 return False
137 __radd__ = __add__
138 - def __rsub__(yo, other):
139 if yo and type(other) == datetime.date: 140 return other - yo._date 141 elif yo and type(other) == Date: 142 return other._date - yo._date 143 elif yo and type(other) == datetime.timedelta: 144 return Date(other - yo._date) 145 else: 146 return NotImplemented
147 - def __repr__(yo):
148 if yo: 149 return "Date(%d, %d, %d)" % yo.timetuple()[:3] 150 else: 151 return "Date()"
152 - def __str__(yo):
153 if yo: 154 return yo.isoformat() 155 return "no date"
156 - def __sub__(yo, other):
157 if yo and type(other) == datetime.date: 158 return yo._date - other 159 elif yo and type(other) == Date: 160 return yo._date - other._date 161 elif yo and type(other) == datetime.timedelta: 162 return Date(yo._date - other) 163 else: 164 return NotImplemented
165 - def date(yo):
166 if yo: 167 return yo._date 168 return None
169 @classmethod
170 - def fromordinal(cls, number):
171 if number: 172 return cls(datetime.date.fromordinal(number)) 173 return cls()
174 @classmethod
175 - def fromtimestamp(cls, timestamp):
176 return cls(datetime.date.fromtimestamp(timestamp))
177 @classmethod
178 - def fromymd(cls, yyyymmdd):
179 if yyyymmdd in ('', ' ','no date'): 180 return cls() 181 return cls(datetime.date(int(yyyymmdd[:4]), int(yyyymmdd[4:6]), int(yyyymmdd[6:])))
182 - def strftime(yo, format):
183 if yo: 184 return yo._date.strftime(format) 185 return '<no date>'
186 @classmethod
187 - def today(cls):
188 return cls(datetime.date.today())
189 - def ymd(yo):
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)
196 -class DateTime(object):
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):
200 """year may be a datetime.datetime""" 201 ndt = object.__new__(cls) 202 ndt._datetime = False 203 if type(year) == datetime.datetime: 204 ndt._datetime = year 205 elif type(year) == DateTime: 206 ndt._datetime = year._datetime 207 elif year is not None: 208 ndt._datetime = datetime.datetime(year, month, day, hour, minute, second, microsec) 209 return ndt
210 - def __add__(yo, other):
211 if yo and type(other) == datetime.timedelta: 212 return DateTime(yo._datetime + other) 213 else: 214 return NotImplemented
215 - def __eq__(yo, other):
216 if yo: 217 if type(other) == datetime.datetime: 218 return yo._datetime == other 219 elif type(other) == DateTime: 220 if other: 221 return yo._datetime == other._datetime 222 return False 223 else: 224 if type(other) == datetime.datetime: 225 return False 226 elif type(other) == DateTime: 227 if other: 228 return False 229 return True 230 return NotImplemented
231 - def __getattr__(yo, name):
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)
237 - def __ge__(yo, other):
238 if yo: 239 if type(other) == datetime.datetime: 240 return yo._datetime >= other 241 elif type(other) == DateTime: 242 if other: 243 return yo._datetime >= other._datetime 244 return False 245 else: 246 if type(other) == datetime.datetime: 247 return False 248 elif type(other) == DateTime: 249 if other: 250 return False 251 return True 252 return NotImplemented
253 - def __gt__(yo, other):
254 if yo: 255 if type(other) == datetime.datetime: 256 return yo._datetime > other 257 elif type(other) == DateTime: 258 if other: 259 return yo._datetime > other._datetime 260 return True 261 else: 262 if type(other) == datetime.datetime: 263 return False 264 elif type(other) == DateTime: 265 if other: 266 return False 267 return False 268 return NotImplemented
269 - def __hash__(yo):
270 return yo._datetime.__hash__()
271 - def __le__(yo, other):
272 if yo: 273 if type(other) == datetime.datetime: 274 return yo._datetime <= other 275 elif type(other) == DateTime: 276 if other: 277 return yo._datetime <= other._datetime 278 return False 279 else: 280 if type(other) == datetime.datetime: 281 return True 282 elif type(other) == DateTime: 283 if other: 284 return True 285 return True 286 return NotImplemented
287 - def __lt__(yo, other):
288 if yo: 289 if type(other) == datetime.datetime: 290 return yo._datetime < other 291 elif type(other) == DateTime: 292 if other: 293 return yo._datetime < other._datetime 294 return False 295 else: 296 if type(other) == datetime.datetime: 297 return True 298 elif type(other) == DateTime: 299 if other: 300 return True 301 return False 302 return NotImplemented
303 - def __ne__(yo, other):
304 if yo: 305 if type(other) == datetime.datetime: 306 return yo._datetime != other 307 elif type(other) == DateTime: 308 if other: 309 return yo._datetime != other._datetime 310 return True 311 else: 312 if type(other) == datetime.datetime: 313 return True 314 elif type(other) == DateTime: 315 if other: 316 return True 317 return False 318 return NotImplemented
319 - def __nonzero__(yo):
320 if yo._datetime is not False: 321 return True 322 return False
323 __radd__ = __add__
324 - def __rsub__(yo, other):
325 if yo and type(other) == datetime.datetime: 326 return other - yo._datetime 327 elif yo and type(other) == DateTime: 328 return other._datetime - yo._datetime 329 elif yo and type(other) == datetime.timedelta: 330 return DateTime(other - yo._datetime) 331 else: 332 return NotImplemented
333 - def __repr__(yo):
334 if yo: 335 return "DateTime(%d, %d, %d, %d, %d, %d, %d, %d, %d)" % yo._datetime.timetuple()[:] 336 else: 337 return "DateTime()"
338 - def __str__(yo):
339 if yo: 340 return yo.isoformat() 341 return "no datetime"
342 - def __sub__(yo, other):
343 if yo and type(other) == datetime.datetime: 344 return yo._datetime - other 345 elif yo and type(other) == DateTime: 346 return yo._datetime - other._datetime 347 elif yo and type(other) == datetime.timedelta: 348 return DateTime(yo._datetime - other) 349 else: 350 return NotImplemented
351 @classmethod
352 - def combine(cls, date, time):
353 if Date(date) and Time(time): 354 return cls(date.year, date.month, date.day, time.hour, time.minute, time.second, time.microsecond) 355 return cls()
356 - def date(yo):
357 if yo: 358 return Date(yo.year, yo.month, yo.day) 359 return Date()
360 - def datetime(yo):
361 if yo: 362 return yo._datetime 363 return None
364 @classmethod
365 - def fromordinal(cls, number):
366 if number: 367 return cls(datetime.datetime.fromordinal(number)) 368 else: 369 return cls()
370 @classmethod
371 - def fromtimestamp(cls, timestamp):
372 return DateTime(datetime.datetime.fromtimestamp(timestamp))
373 @classmethod
374 - def now(cls):
375 return cls(datetime.datetime.now())
376 - def time(yo):
377 if yo: 378 return Time(yo.hour, yo.minute, yo.second, yo.microsecond) 379 return Time()
380 @classmethod
381 - def utcnow(cls):
382 return cls(datetime.datetime.utcnow())
383 @classmethod
384 - def today(cls):
385 return cls(datetime.datetime.today())
386 DateTime.max = DateTime(datetime.datetime.max) 387 DateTime.min = DateTime(datetime.datetime.min)
388 -class Time(object):
389 "adds null capable datetime.time constructs" 390 __slots__ = ['_time']
391 - def __new__(cls, hour=None, minute=0, second=0, microsec=0):
392 """hour may be a datetime.time""" 393 nt = object.__new__(cls) 394 nt._time = False 395 if type(hour) == datetime.time: 396 nt._time = hour 397 elif type(hour) == Time: 398 nt._time = hour._time 399 elif hour is not None: 400 nt._time = datetime.time(hour, minute, second, microsec) 401 return nt
402 - def __add__(yo, other):
403 if yo and type(other) == datetime.timedelta: 404 return Time(yo._time + other) 405 else: 406 return NotImplemented
407 - def __eq__(yo, other):
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
423 - def __getattr__(yo, name):
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)
429 - def __ge__(yo, other):
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
445 - def __gt__(yo, other):
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
461 - def __hash__(yo):
462 return yo._datetime.__hash__()
463 - def __le__(yo, other):
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
479 - def __lt__(yo, other):
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
495 - def __ne__(yo, other):
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
511 - def __nonzero__(yo):
512 if yo._time is not False: 513 return True 514 return False
515 __radd__ = __add__
516 - def __rsub__(yo, other):
517 if yo and type(other) == datetime.time: 518 return other - yo._time 519 elif yo and type(other) == Time: 520 return other._time - yo._time 521 elif yo and type(other) == datetime.timedelta: 522 return Time(other - yo._datetime) 523 else: 524 return NotImplemented
525 - def __repr__(yo):
526 if yo: 527 return "Time(%d, %d, %d, %d)" % (yo.hour, yo.minute, yo.second, yo.microsecond) 528 else: 529 return "Time()"
530 - def __str__(yo):
531 if yo: 532 return yo.isoformat() 533 return "no time"
534 - def __sub__(yo, other):
535 if yo and type(other) == datetime.time: 536 return yo._time - other 537 elif yo and type(other) == Time: 538 return yo._time - other._time 539 elif yo and type(other) == datetime.timedelta: 540 return Time(yo._time - other) 541 else: 542 return NotImplemented
543 Time.max = Time(datetime.time.max) 544 Time.min = Time(datetime.time.min) 545