timezone_utils.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import logging
  2. from datetime import datetime
  3. logger = logging.getLogger(__name__)
  4. timezones = [
  5. {"offset": "-12:00", "value": "Etc/+12"},
  6. {"offset": "-11:00", "value": "Pacific/Pago_Pago"},
  7. {"offset": "-10:00", "value": "Pacific/Honolulu"},
  8. {"offset": "-09:00", "value": "America/Anchorage"},
  9. {"offset": "-08:00", "value": "America/Los_Angeles"},
  10. {"offset": "-07:00", "value": "America/Phoenix"},
  11. {"offset": "-06:00", "value": "America/Guatemala"},
  12. {"offset": "-05:00", "value": "America/Bogota"},
  13. {"offset": "-04:00", "value": "America/Halifax"},
  14. {"offset": "-03:30", "value": "America/St_Johns"},
  15. {"offset": "-03:00", "value": "America/Sao_Paulo"},
  16. {"offset": "-02:00", "value": "America/Godthab"},
  17. {"offset": "-01:00", "value": "Atlantic/Azores"},
  18. {"offset": "+00:00", "value": "Europe/London"},
  19. {"offset": "+01:00", "value": "Europe/Amsterdam"},
  20. {"offset": "+02:00", "value": "Asia/Amman"},
  21. {"offset": "+03:00", "value": "Asia/Baghdad"},
  22. {"offset": "+03:30", "value": "Asia/Tehran"},
  23. {"offset": "+04:00", "value": "Asia/Dubai"},
  24. {"offset": "+04:30", "value": "Asia/Kabul"},
  25. {"offset": "+05:00", "value": "Asia/Karachi"},
  26. {"offset": "+05:30", "value": "Asia/Kolkata"},
  27. {"offset": "+05:45", "value": "Asia/Kathmandu"},
  28. {"offset": "+06:00", "value": "Asia/Almaty"},
  29. {"offset": "+06:30", "value": "Asia/Yangon"},
  30. {"offset": "+07:00", "value": "Asia/Bangkok"},
  31. {"offset": "+08:00", "value": "Asia/Shanghai"},
  32. {"offset": "+09:00", "value": "Asia/Irkutsk"},
  33. {"offset": "+09:30", "value": "Australia/Adelaide"},
  34. {"offset": "+10:00", "value": "Australia/Brisbane"},
  35. {"offset": "+11:00", "value": "Asia/Magadan"},
  36. {"offset": "+12:00", "value": "Pacific/Auckland"},
  37. {"offset": "+13:00", "value": "Pacific/Tongatapu"},
  38. ]
  39. def get_current_timezone_info():
  40. current_tz = datetime.now().astimezone().tzinfo
  41. offset = current_tz.utcoffset(None)
  42. hours, remainder = divmod(offset.total_seconds(), 3600)
  43. minutes = remainder // 60
  44. sign = "+" if hours >= 0 else "-"
  45. current_offset = f"{sign}{abs(int(hours)):02d}:{abs(int(minutes)):02d}"
  46. current_timezone = next(
  47. (tz for tz in timezones if tz["offset"] == current_offset),
  48. {"offset": None, "value": None},
  49. )
  50. return current_timezone