timestamp.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # coding: utf-8
  2. from __future__ import print_function, absolute_import, division, unicode_literals
  3. import datetime
  4. import copy
  5. # ToDo: at least on PY3 you could probably attach the tzinfo correctly to the object
  6. # a more complete datetime might be used by safe loading as well
  7. if False: # MYPY
  8. from typing import Any, Dict, Optional, List # NOQA
  9. class TimeStamp(datetime.datetime):
  10. def __init__(self, *args, **kw):
  11. # type: (Any, Any) -> None
  12. self._yaml = dict(t=False, tz=None, delta=0) # type: Dict[Any, Any]
  13. def __new__(cls, *args, **kw): # datetime is immutable
  14. # type: (Any, Any) -> Any
  15. return datetime.datetime.__new__(cls, *args, **kw) # type: ignore
  16. def __deepcopy__(self, memo):
  17. # type: (Any) -> Any
  18. ts = TimeStamp(self.year, self.month, self.day, self.hour, self.minute, self.second)
  19. ts._yaml = copy.deepcopy(self._yaml)
  20. return ts
  21. def replace(self, year=None, month=None, day=None, hour=None,
  22. minute=None, second=None, microsecond=None, tzinfo=True,
  23. fold=None):
  24. if year is None:
  25. year = self.year
  26. if month is None:
  27. month = self.month
  28. if day is None:
  29. day = self.day
  30. if hour is None:
  31. hour = self.hour
  32. if minute is None:
  33. minute = self.minute
  34. if second is None:
  35. second = self.second
  36. if microsecond is None:
  37. microsecond = self.microsecond
  38. if tzinfo is True:
  39. tzinfo = self.tzinfo
  40. if fold is None:
  41. fold = self.fold
  42. ts = type(self)(year, month, day, hour, minute, second, microsecond, tzinfo, fold=fold)
  43. ts._yaml = copy.deepcopy(self._yaml)
  44. return ts