timestamp.py 1.7 KB

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