MCPcopy Create free account
hub / github.com/dateutil/dateutil / TZWinContext

Class TZWinContext

tests/_common.py:136–162  ·  view source on GitHub ↗

Context manager for changing local time zone on Windows. Because the effect of this is system-wide and global, it may have unintended side effect. Set the ``DATEUTIL_MAY_CHANGE_TZ`` environment variable to a truthy value before using this context manager.

Source from the content-addressed store, hash-verified

134
135
136class TZWinContext(TZContextBase):
137 """
138 Context manager for changing local time zone on Windows.
139
140 Because the effect of this is system-wide and global, it may have
141 unintended side effect. Set the ``DATEUTIL_MAY_CHANGE_TZ`` environment
142 variable to a truthy value before using this context manager.
143 """
144 def get_current_tz(self):
145 p = subprocess.Popen(['tzutil', '/g'], stdout=subprocess.PIPE)
146
147 ctzname, err = p.communicate()
148 ctzname = ctzname.decode() # Popen returns
149
150 if p.returncode:
151 raise OSError('Failed to get current time zone: ' + err)
152
153 return ctzname
154
155 def set_current_tz(self, tzname):
156 p = subprocess.Popen('tzutil /s "' + tzname + '"')
157
158 out, err = p.communicate()
159
160 if p.returncode:
161 raise OSError('Failed to set current time zone: ' +
162 (err or 'Unknown error.'))
163
164
165###

Calls

no outgoing calls