[Datetime utils] Datetime & timestamp utils for Python #py3 #datetime # utils
def dt2ts(dt: Optional[datetime]) -> Optional[int]:
return int(time.mktime(dt.timetuple())) if dt is not None else None
def dt2ts_ms(dt: Optional[datetime]) -> Optional[int]:
return dt2ts(dt) * 1000 if dt is not None else None
def ts2dt(ts: Optional[int]) -> Optional[datetime]:
return datetime.utcfromtimestamp(int(ts)) if ts is not None else None
def ts_ms2dt(ts: Optional[int]) -> Optional[datetime]:
return ts2dt(int(ts/1000) if ts is not None else None