Package dbf :: Module dates
[hide private]

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