_unix.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- coding: utf-8 -*-
  2. from __future__ import with_statement
  3. import os
  4. import re
  5. import sys
  6. import pytz
  7. import subprocess
  8. _systemconfig_tz = re.compile(r'^Time Zone: (.*)$', re.MULTILINE)
  9. def _tz_from_env(tzenv):
  10. if tzenv[0] == ':':
  11. tzenv = tzenv[1:]
  12. # TZ specifies a file
  13. if os.path.exists(tzenv):
  14. with open(tzenv, 'rb') as tzfile:
  15. return pytz.tzfile.build_tzinfo('local', tzfile)
  16. # TZ specifies a zoneinfo zone.
  17. try:
  18. tz = pytz.timezone(tzenv)
  19. # That worked, so we return this:
  20. return tz
  21. except pytz.UnknownTimeZoneError:
  22. raise pytz.UnknownTimeZoneError(
  23. "tzlocal() does not support non-zoneinfo timezones like %s. \n"
  24. "Please use a timezone in the form of Continent/City")
  25. def _get_localzone(_root='/'):
  26. """Tries to find the local timezone configuration.
  27. This method prefers finding the timezone name and passing that to pytz,
  28. over passing in the localtime file, as in the later case the zoneinfo
  29. name is unknown.
  30. The parameter _root makes the function look for files like /etc/localtime
  31. beneath the _root directory. This is primarily used by the tests.
  32. In normal usage you call the function without parameters.
  33. """
  34. tzenv = os.environ.get('TZ')
  35. if tzenv:
  36. return _tz_from_env(tzenv)
  37. # This is actually a pretty reliable way to test for the local time
  38. # zone on operating systems like OS X. On OS X especially this is the
  39. # only one that actually works.
  40. try:
  41. link_dst = os.readlink('/etc/localtime')
  42. except OSError:
  43. pass
  44. else:
  45. pos = link_dst.find('/zoneinfo/')
  46. if pos >= 0:
  47. zone_name = link_dst[pos + 10:]
  48. try:
  49. return pytz.timezone(zone_name)
  50. except pytz.UnknownTimeZoneError:
  51. pass
  52. # If we are on OS X now we are pretty sure that the rest of the
  53. # code will fail and just fall through until it hits the reading
  54. # of /etc/localtime and using it without name. At this point we
  55. # can invoke systemconfig which internally invokes ICU. ICU itself
  56. # does the same thing we do (readlink + compare file contents) but
  57. # since it knows where the zone files are that should be a bit
  58. # better than reimplementing the logic here.
  59. if sys.platform == 'darwin':
  60. c = subprocess.Popen(['systemsetup', '-gettimezone'],
  61. stdout=subprocess.PIPE)
  62. sys_result = c.communicate()[0]
  63. c.wait()
  64. tz_match = _systemconfig_tz.search(sys_result)
  65. if tz_match is not None:
  66. zone_name = tz_match.group(1)
  67. try:
  68. return pytz.timezone(zone_name)
  69. except pytz.UnknownTimeZoneError:
  70. pass
  71. # Now look for distribution specific configuration files
  72. # that contain the timezone name.
  73. tzpath = os.path.join(_root, 'etc/timezone')
  74. if os.path.exists(tzpath):
  75. with open(tzpath, 'rb') as tzfile:
  76. data = tzfile.read()
  77. # Issue #3 in tzlocal was that /etc/timezone was a zoneinfo file.
  78. # That's a misconfiguration, but we need to handle it gracefully:
  79. if data[:5] != b'TZif2':
  80. etctz = data.strip().decode()
  81. # Get rid of host definitions and comments:
  82. if ' ' in etctz:
  83. etctz, dummy = etctz.split(' ', 1)
  84. if '#' in etctz:
  85. etctz, dummy = etctz.split('#', 1)
  86. return pytz.timezone(etctz.replace(' ', '_'))
  87. # CentOS has a ZONE setting in /etc/sysconfig/clock,
  88. # OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
  89. # Gentoo has a TIMEZONE setting in /etc/conf.d/clock
  90. # We look through these files for a timezone:
  91. timezone_re = re.compile(r'\s*(TIME)?ZONE\s*=\s*"(?P<etctz>.+)"')
  92. for filename in ('etc/sysconfig/clock', 'etc/conf.d/clock'):
  93. tzpath = os.path.join(_root, filename)
  94. if not os.path.exists(tzpath):
  95. continue
  96. with open(tzpath, 'rt') as tzfile:
  97. for line in tzfile:
  98. match = timezone_re.match(line)
  99. if match is not None:
  100. # We found a timezone
  101. etctz = match.group("etctz")
  102. return pytz.timezone(etctz.replace(' ', '_'))
  103. # No explicit setting existed. Use localtime
  104. for filename in ('etc/localtime', 'usr/local/etc/localtime'):
  105. tzpath = os.path.join(_root, filename)
  106. if not os.path.exists(tzpath):
  107. continue
  108. with open(tzpath, 'rb') as tzfile:
  109. return pytz.tzfile.build_tzinfo('local', tzfile)
  110. raise pytz.UnknownTimeZoneError('Can not find any timezone configuration')