date.py 825 B

123456789101112131415161718192021222324252627282930313233343536
  1. # -*- coding: utf-8 -*-
  2. import arrow
  3. TIME_FMT = "YYYYMMDDHHmmss"
  4. def get_time_str(
  5. delta: int = 0, flag: str = "now", fmt: str | None = TIME_FMT
  6. ) -> str:
  7. """
  8. Return a Beijing time strings.
  9. :param delta: time delta(seconds)
  10. :param flag:
  11. :param fmt: time string's format
  12. :return: a '%Y%m%d%H%M%S' format strings
  13. """
  14. utc = arrow.utcnow()
  15. local = utc.to("Asia/Shanghai")
  16. if flag == "ago":
  17. delta = -delta
  18. t = local.shift(seconds=delta)
  19. elif flag == "later":
  20. t = local.shift(seconds=delta)
  21. else:
  22. t = local
  23. time_str = t.format(fmt)
  24. return time_str
  25. def get_quarter_minutes(time_str: str) -> str:
  26. temp = arrow.get(time_str, TIME_FMT).timestamp() // (15 * 60) * (15 * 60)
  27. return arrow.get(temp).time().strftime("%H%M%S")