_unix.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import datetime
  2. import os
  3. import re
  4. from babel.localtime._helpers import (
  5. _get_tzinfo,
  6. _get_tzinfo_from_file,
  7. _get_tzinfo_or_raise,
  8. )
  9. def _tz_from_env(tzenv: str) -> datetime.tzinfo:
  10. if tzenv[0] == ':':
  11. tzenv = tzenv[1:]
  12. # TZ specifies a file
  13. if os.path.exists(tzenv):
  14. return _get_tzinfo_from_file(tzenv)
  15. # TZ specifies a zoneinfo zone.
  16. return _get_tzinfo_or_raise(tzenv)
  17. def _get_localzone(_root: str = '/') -> datetime.tzinfo:
  18. """Tries to find the local timezone configuration.
  19. This method prefers finding the timezone name and passing that to
  20. zoneinfo or pytz, over passing in the localtime file, as in the later
  21. case the zoneinfo name is unknown.
  22. The parameter _root makes the function look for files like /etc/localtime
  23. beneath the _root directory. This is primarily used by the tests.
  24. In normal usage you call the function without parameters.
  25. """
  26. tzenv = os.environ.get('TZ')
  27. if tzenv:
  28. return _tz_from_env(tzenv)
  29. # This is actually a pretty reliable way to test for the local time
  30. # zone on operating systems like OS X. On OS X especially this is the
  31. # only one that actually works.
  32. try:
  33. link_dst = os.readlink('/etc/localtime')
  34. except OSError:
  35. pass
  36. else:
  37. pos = link_dst.find('/zoneinfo/')
  38. if pos >= 0:
  39. # On occasion, the `/etc/localtime` symlink has a double slash, e.g.
  40. # "/usr/share/zoneinfo//UTC", which would make `zoneinfo.ZoneInfo`
  41. # complain (no absolute paths allowed), and we'd end up returning
  42. # `None` (as a fix for #1092).
  43. # Instead, let's just "fix" the double slash symlink by stripping
  44. # leading slashes before passing the assumed zone name forward.
  45. zone_name = link_dst[pos + 10:].lstrip("/")
  46. tzinfo = _get_tzinfo(zone_name)
  47. if tzinfo is not None:
  48. return tzinfo
  49. # Now look for distribution specific configuration files
  50. # that contain the timezone name.
  51. tzpath = os.path.join(_root, 'etc/timezone')
  52. if os.path.exists(tzpath):
  53. with open(tzpath, 'rb') as tzfile:
  54. data = tzfile.read()
  55. # Issue #3 in tzlocal was that /etc/timezone was a zoneinfo file.
  56. # That's a misconfiguration, but we need to handle it gracefully:
  57. if data[:5] != b'TZif2':
  58. etctz = data.strip().decode()
  59. # Get rid of host definitions and comments:
  60. if ' ' in etctz:
  61. etctz, dummy = etctz.split(' ', 1)
  62. if '#' in etctz:
  63. etctz, dummy = etctz.split('#', 1)
  64. return _get_tzinfo_or_raise(etctz.replace(' ', '_'))
  65. # CentOS has a ZONE setting in /etc/sysconfig/clock,
  66. # OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
  67. # Gentoo has a TIMEZONE setting in /etc/conf.d/clock
  68. # We look through these files for a timezone:
  69. timezone_re = re.compile(r'\s*(TIME)?ZONE\s*=\s*"(?P<etctz>.+)"')
  70. for filename in ('etc/sysconfig/clock', 'etc/conf.d/clock'):
  71. tzpath = os.path.join(_root, filename)
  72. if not os.path.exists(tzpath):
  73. continue
  74. with open(tzpath) as tzfile:
  75. for line in tzfile:
  76. match = timezone_re.match(line)
  77. if match is not None:
  78. # We found a timezone
  79. etctz = match.group("etctz")
  80. return _get_tzinfo_or_raise(etctz.replace(' ', '_'))
  81. # No explicit setting existed. Use localtime
  82. for filename in ('etc/localtime', 'usr/local/etc/localtime'):
  83. tzpath = os.path.join(_root, filename)
  84. if not os.path.exists(tzpath):
  85. continue
  86. return _get_tzinfo_from_file(tzpath)
  87. raise LookupError('Can not find any timezone configuration')