local_timezone.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. from __future__ import annotations
  2. import contextlib
  3. import os
  4. import re
  5. import sys
  6. import warnings
  7. from contextlib import contextmanager
  8. from typing import Iterator
  9. from typing import cast
  10. from pendulum.tz.exceptions import InvalidTimezone
  11. from pendulum.tz.timezone import UTC
  12. from pendulum.tz.timezone import FixedTimezone
  13. from pendulum.tz.timezone import Timezone
  14. if sys.platform == "win32":
  15. import winreg
  16. _mock_local_timezone = None
  17. _local_timezone = None
  18. def get_local_timezone() -> Timezone | FixedTimezone:
  19. global _local_timezone
  20. if _mock_local_timezone is not None:
  21. return _mock_local_timezone
  22. if _local_timezone is None:
  23. tz = _get_system_timezone()
  24. _local_timezone = tz
  25. return _local_timezone
  26. def set_local_timezone(mock: str | Timezone | None = None) -> None:
  27. global _mock_local_timezone
  28. _mock_local_timezone = mock
  29. @contextmanager
  30. def test_local_timezone(mock: Timezone) -> Iterator[None]:
  31. set_local_timezone(mock)
  32. yield
  33. set_local_timezone()
  34. def _get_system_timezone() -> Timezone:
  35. if sys.platform == "win32":
  36. return _get_windows_timezone()
  37. elif "darwin" in sys.platform:
  38. return _get_darwin_timezone()
  39. return _get_unix_timezone()
  40. if sys.platform == "win32":
  41. def _get_windows_timezone() -> Timezone:
  42. from pendulum.tz.data.windows import windows_timezones
  43. # Windows is special. It has unique time zone names (in several
  44. # meanings of the word) available, but unfortunately, they can be
  45. # translated to the language of the operating system, so we need to
  46. # do a backwards lookup, by going through all time zones and see which
  47. # one matches.
  48. handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
  49. tz_local_key_name = r"SYSTEM\CurrentControlSet\Control\TimeZoneInformation"
  50. localtz = winreg.OpenKey(handle, tz_local_key_name)
  51. timezone_info = {}
  52. size = winreg.QueryInfoKey(localtz)[1]
  53. for i in range(size):
  54. data = winreg.EnumValue(localtz, i)
  55. timezone_info[data[0]] = data[1]
  56. localtz.Close()
  57. if "TimeZoneKeyName" in timezone_info:
  58. # Windows 7 (and Vista?)
  59. # For some reason this returns a string with loads of NUL bytes at
  60. # least on some systems. I don't know if this is a bug somewhere, I
  61. # just work around it.
  62. tzkeyname = timezone_info["TimeZoneKeyName"].split("\x00", 1)[0]
  63. else:
  64. # Windows 2000 or XP
  65. # This is the localized name:
  66. tzwin = timezone_info["StandardName"]
  67. # Open the list of timezones to look up the real name:
  68. tz_key_name = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"
  69. tzkey = winreg.OpenKey(handle, tz_key_name)
  70. # Now, match this value to Time Zone information
  71. tzkeyname = None
  72. for i in range(winreg.QueryInfoKey(tzkey)[0]):
  73. subkey = winreg.EnumKey(tzkey, i)
  74. sub = winreg.OpenKey(tzkey, subkey)
  75. info = {}
  76. size = winreg.QueryInfoKey(sub)[1]
  77. for i in range(size):
  78. data = winreg.EnumValue(sub, i)
  79. info[data[0]] = data[1]
  80. sub.Close()
  81. with contextlib.suppress(KeyError):
  82. # This timezone didn't have proper configuration.
  83. # Ignore it.
  84. if info["Std"] == tzwin:
  85. tzkeyname = subkey
  86. break
  87. tzkey.Close()
  88. handle.Close()
  89. if tzkeyname is None:
  90. raise LookupError("Can not find Windows timezone configuration")
  91. timezone = windows_timezones.get(tzkeyname)
  92. if timezone is None:
  93. # Nope, that didn't work. Try adding "Standard Time",
  94. # it seems to work a lot of times:
  95. timezone = windows_timezones.get(tzkeyname + " Standard Time")
  96. # Return what we have.
  97. if timezone is None:
  98. raise LookupError("Unable to find timezone " + tzkeyname)
  99. return Timezone(timezone)
  100. else:
  101. def _get_windows_timezone() -> Timezone:
  102. raise NotImplementedError
  103. def _get_darwin_timezone() -> Timezone:
  104. # link will be something like /usr/share/zoneinfo/America/Los_Angeles.
  105. link = os.readlink("/etc/localtime")
  106. tzname = link[link.rfind("zoneinfo/") + 9 :]
  107. return Timezone(tzname)
  108. def _get_unix_timezone(_root: str = "/") -> Timezone:
  109. tzenv = os.environ.get("TZ")
  110. if tzenv:
  111. with contextlib.suppress(ValueError):
  112. return _tz_from_env(tzenv)
  113. # Now look for distribution specific configuration files
  114. # that contain the timezone name.
  115. tzpath = os.path.join(_root, "etc/timezone")
  116. if os.path.isfile(tzpath):
  117. with open(tzpath, "rb") as tzfile:
  118. tzfile_data = tzfile.read()
  119. # Issue #3 was that /etc/timezone was a zoneinfo file.
  120. # That's a misconfiguration, but we need to handle it gracefully:
  121. if tzfile_data[:5] != b"TZif2":
  122. etctz = tzfile_data.strip().decode()
  123. # Get rid of host definitions and comments:
  124. if " " in etctz:
  125. etctz, dummy = etctz.split(" ", 1)
  126. if "#" in etctz:
  127. etctz, dummy = etctz.split("#", 1)
  128. return Timezone(etctz.replace(" ", "_"))
  129. # CentOS has a ZONE setting in /etc/sysconfig/clock,
  130. # OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
  131. # Gentoo has a TIMEZONE setting in /etc/conf.d/clock
  132. # We look through these files for a timezone:
  133. zone_re = re.compile(r'\s*ZONE\s*=\s*"')
  134. timezone_re = re.compile(r'\s*TIMEZONE\s*=\s*"')
  135. end_re = re.compile('"')
  136. for filename in ("etc/sysconfig/clock", "etc/conf.d/clock"):
  137. tzpath = os.path.join(_root, filename)
  138. if not os.path.isfile(tzpath):
  139. continue
  140. with open(tzpath) as tzfile:
  141. data = tzfile.readlines()
  142. for line in data:
  143. # Look for the ZONE= setting.
  144. match = zone_re.match(line)
  145. if match is None:
  146. # No ZONE= setting. Look for the TIMEZONE= setting.
  147. match = timezone_re.match(line)
  148. if match is not None:
  149. # Some setting existed
  150. line = line[match.end() :]
  151. etctz = line[
  152. : cast(
  153. re.Match, end_re.search(line) # type: ignore[type-arg]
  154. ).start()
  155. ]
  156. parts = list(reversed(etctz.replace(" ", "_").split(os.path.sep)))
  157. tzpath_parts: list[str] = []
  158. while parts:
  159. tzpath_parts.insert(0, parts.pop(0))
  160. with contextlib.suppress(InvalidTimezone):
  161. return Timezone(os.path.join(*tzpath_parts))
  162. # systemd distributions use symlinks that include the zone name,
  163. # see manpage of localtime(5) and timedatectl(1)
  164. tzpath = os.path.join(_root, "etc", "localtime")
  165. if os.path.isfile(tzpath) and os.path.islink(tzpath):
  166. parts = list(
  167. reversed(os.path.realpath(tzpath).replace(" ", "_").split(os.path.sep))
  168. )
  169. tzpath_parts: list[str] = [] # type: ignore[no-redef]
  170. while parts:
  171. tzpath_parts.insert(0, parts.pop(0))
  172. with contextlib.suppress(InvalidTimezone):
  173. return Timezone(os.path.join(*tzpath_parts))
  174. # No explicit setting existed. Use localtime
  175. for filename in ("etc/localtime", "usr/local/etc/localtime"):
  176. tzpath = os.path.join(_root, filename)
  177. if not os.path.isfile(tzpath):
  178. continue
  179. with open(tzpath, "rb") as f:
  180. return Timezone.from_file(f)
  181. warnings.warn(
  182. "Unable not find any timezone configuration, defaulting to UTC.", stacklevel=1
  183. )
  184. return UTC
  185. def _tz_from_env(tzenv: str) -> Timezone:
  186. if tzenv[0] == ":":
  187. tzenv = tzenv[1:]
  188. # TZ specifies a file
  189. if os.path.isfile(tzenv):
  190. with open(tzenv, "rb") as f:
  191. return Timezone.from_file(f)
  192. # TZ specifies a zoneinfo zone.
  193. try:
  194. return Timezone(tzenv)
  195. except ValueError:
  196. raise