tools.py 459 B

123456789101112131415
  1. # from dateutil.parser import isoparse
  2. from datetime import date, datetime, timezone
  3. def timestamp2utc_dt(timestamp: int, _format: str = "%Y-%m-%d %H:%M:%S") -> [str, datetime]:
  4. if len(str(timestamp)) == 13: # _ms
  5. timestamp = int(timestamp / 1000)
  6. utc_dt = datetime.utcfromtimestamp(timestamp)
  7. if _format:
  8. return utc_dt.strftime(_format)
  9. return utc_dt
  10. if __name__ == '__main__':
  11. print(timestamp2utc_dt(1574232571377))