date_util.py 833 B

123456789101112131415161718192021222324252627282930313233
  1. from datetime import datetime
  2. def ts_cover_str(ts=0, _format='%Y-%m-%d %H:%M:%S') -> str:
  3. return datetime.fromtimestamp(ts / 1000).strftime(_format)
  4. def str_cover_date(s: str, _format='%Y-%m-%d %H:%M:%S') -> datetime:
  5. return datetime.strptime(s, _format)
  6. def timestamp_convert(ts: int) -> str:
  7. return seconds_convert(ts // 1000)
  8. def seconds_convert(seconds: int) -> str:
  9. hours = seconds // 3600
  10. minutes = (seconds % 3600) // 60
  11. seconds = seconds % 60
  12. s = ""
  13. if hours > 0:
  14. s = f"{s} {hours}小时"
  15. if minutes > 0:
  16. s = f"{s} {minutes}分"
  17. if seconds > 0:
  18. s = f"{s} {seconds}秒"
  19. return s
  20. def date_convert_dt_hh(date_str: str, _format='%Y-%m-%d %H:%M:%S') -> str:
  21. date_obj = datetime.strptime(date_str, _format)
  22. return date_obj.strftime('%Y%m%d%H')