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

Class TZWinContext

tests/_common.py:256–282  ·  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

254
255
256class TZWinContext(TZContextBase):
257 """
258 Context manager for changing local time zone on Windows.
259
260 Because the effect of this is system-wide and global, it may have
261 unintended side effect. Set the ``DATEUTIL_MAY_CHANGE_TZ`` environment
262 variable to a truthy value before using this context manager.
263 """
264 def get_current_tz(self):
265 p = subprocess.Popen(['tzutil', '/g'], stdout=subprocess.PIPE)
266
267 ctzname, err = p.communicate()
268 ctzname = ctzname.decode() # Popen returns
269
270 if p.returncode:
271 raise OSError('Failed to get current time zone: ' + err)
272
273 return ctzname
274
275 def set_current_tz(self, tzname):
276 p = subprocess.Popen('tzutil /s "' + tzname + '"')
277
278 out, err = p.communicate()
279
280 if p.returncode:
281 raise OSError('Failed to set current time zone: ' +
282 (err or 'Unknown error.'))
283
284
285###

Calls

no outgoing calls