MCPcopy Index your code
hub / github.com/python-pendulum/pendulum / _get_windows_timezone

Function _get_windows_timezone

src/pendulum/tz/local_timezone.py:70–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

68if sys.platform == "win32":
69
70 def _get_windows_timezone() -> Timezone:
71 from pendulum.tz.data.windows import windows_timezones
72
73 # Windows is special. It has unique time zone names (in several
74 # meanings of the word) available, but unfortunately, they can be
75 # translated to the language of the operating system, so we need to
76 # do a backwards lookup, by going through all time zones and see which
77 # one matches.
78 handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
79
80 tz_local_key_name = r"SYSTEM\CurrentControlSet\Control\TimeZoneInformation"
81 localtz = winreg.OpenKey(handle, tz_local_key_name)
82
83 timezone_info = {}
84 size = winreg.QueryInfoKey(localtz)[1]
85 for i in range(size):
86 data = winreg.EnumValue(localtz, i)
87 timezone_info[data[0]] = data[1]
88
89 localtz.Close()
90
91 if "TimeZoneKeyName" in timezone_info:
92 # Windows 7 (and Vista?)
93
94 # For some reason this returns a string with loads of NUL bytes at
95 # least on some systems. I don't know if this is a bug somewhere, I
96 # just work around it.
97 tzkeyname = timezone_info["TimeZoneKeyName"].split("\x00", 1)[0]
98 else:
99 # Windows 2000 or XP
100
101 # This is the localized name:
102 tzwin = timezone_info["StandardName"]
103
104 # Open the list of timezones to look up the real name:
105 tz_key_name = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones"
106 tzkey = winreg.OpenKey(handle, tz_key_name)
107
108 # Now, match this value to Time Zone information
109 tzkeyname = None
110 for i in range(winreg.QueryInfoKey(tzkey)[0]):
111 subkey = winreg.EnumKey(tzkey, i)
112 sub = winreg.OpenKey(tzkey, subkey)
113
114 info = {}
115 size = winreg.QueryInfoKey(sub)[1]
116 for i in range(size):
117 data = winreg.EnumValue(sub, i)
118 info[data[0]] = data[1]
119
120 sub.Close()
121 with contextlib.suppress(KeyError):
122 # This timezone didn't have proper configuration.
123 # Ignore it.
124 if info["Std"] == tzwin:
125 tzkeyname = subkey
126 break
127

Callers 2

test_windows_timezoneFunction · 0.90
_get_system_timezoneFunction · 0.85

Calls 2

TimezoneClass · 0.90
getMethod · 0.80

Tested by 1

test_windows_timezoneFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…