codekingpro/portable-devtools
114k
1 2from __future__ import annotations3 4import copy5import datetime6 7# ToDo: at least on PY3 you could probably attach the tzinfo correctly to the object8# a more complete datetime might be used by safe loading as well9#10# add type information (iso8601, spaced)11 12if False: # MYPY13 from typing import Any, Dict, Optional, List # NOQA14 15 16class TimeStamp(datetime.datetime):17 def __init__(self, *args: Any, **kw: Any) -> None:18 self._yaml: Dict[str, Any] = dict(t=False, tz=None, delta=0)19 20 def __new__(cls, *args: Any, **kw: Any) -> Any: # datetime is immutable21 return datetime.datetime.__new__(cls, *args, **kw)22 23 def __deepcopy__(self, memo: Any) -> Any:24 ts = TimeStamp(self.year, self.month, self.day, self.hour, self.minute, self.second)25 ts._yaml = copy.deepcopy(self._yaml)26 return ts27 28 def replace(29 self,30 year: Any = None,31 month: Any = None,32 day: Any = None,33 hour: Any = None,34 minute: Any = None,35 second: Any = None,36 microsecond: Any = None,37 tzinfo: Any = True,38 fold: Any = None,39 ) -> Any:40 if year is None:41 year = self.year42 if month is None:43 month = self.month44 if day is None:45 day = self.day46 if hour is None:47 hour = self.hour48 if minute is None:49 minute = self.minute50 if second is None:51 second = self.second52 if microsecond is None:53 microsecond = self.microsecond54 if tzinfo is True:55 tzinfo = self.tzinfo56 if fold is None:57 fold = self.fold58 ts = type(self)(year, month, day, hour, minute, second, microsecond, tzinfo, fold=fold)59 ts._yaml = copy.deepcopy(self._yaml)60 return ts61 62 def __str__(self) -> str:63 return self.isoformat('T' if self._yaml['t'] else ' ')64 