Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

64 linhas
1.9 KiB

  1. from __future__ import annotations
  2. import copy
  3. import datetime
  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. if False: # MYPY
  9. from typing import Any, Dict, Optional, List # NOQA
  10. class TimeStamp(datetime.datetime):
  11. def __init__(self, *args: Any, **kw: Any) -> None:
  12. self._yaml: Dict[str, Any] = dict(t=False, tz=None, delta=0)
  13. def __new__(cls, *args: Any, **kw: Any) -> Any: # datetime is immutable
  14. return datetime.datetime.__new__(cls, *args, **kw)
  15. def __deepcopy__(self, memo: Any) -> Any:
  16. ts = TimeStamp(self.year, self.month, self.day, self.hour, self.minute, self.second)
  17. ts._yaml = copy.deepcopy(self._yaml)
  18. return ts
  19. def replace(
  20. self,
  21. year: Any = None,
  22. month: Any = None,
  23. day: Any = None,
  24. hour: Any = None,
  25. minute: Any = None,
  26. second: Any = None,
  27. microsecond: Any = None,
  28. tzinfo: Any = True,
  29. fold: Any = None,
  30. ) -> Any:
  31. if year is None:
  32. year = self.year
  33. if month is None:
  34. month = self.month
  35. if day is None:
  36. day = self.day
  37. if hour is None:
  38. hour = self.hour
  39. if minute is None:
  40. minute = self.minute
  41. if second is None:
  42. second = self.second
  43. if microsecond is None:
  44. microsecond = self.microsecond
  45. if tzinfo is True:
  46. tzinfo = self.tzinfo
  47. if fold is None:
  48. fold = self.fold
  49. ts = type(self)(year, month, day, hour, minute, second, microsecond, tzinfo, fold=fold)
  50. ts._yaml = copy.deepcopy(self._yaml)
  51. return ts
  52. def __str__(self) -> str:
  53. return self.isoformat('T' if self._yaml['t'] else ' ')