Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always assume UTC if no timezone in Retry-After
- Loading branch information
Showing
5 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from contextlib import contextmanager | ||
import time | ||
import datetime | ||
import os | ||
import pytest | ||
from dateutil import tz | ||
|
||
|
||
@contextmanager | ||
def stub_timezone_ctx(tzname): | ||
""" | ||
Switch to a locally-known timezone specified by `tzname`. | ||
On exit, restore the previous timezone. | ||
If `tzname` is `None`, do nothing. | ||
""" | ||
if tzname is None: | ||
yield | ||
return | ||
|
||
# Only supported on Unix | ||
if not hasattr(time, "tzset"): | ||
pytest.skip("Timezone patching is not supported") | ||
|
||
# Make sure the new timezone exists, at least in dateutil | ||
new_tz = tz.gettz(tzname) | ||
if new_tz is None: | ||
raise ValueError("Invalid timezone specified: %r" % (tzname,)) | ||
|
||
# Get the current timezone | ||
local_tz = tz.tzlocal() | ||
if local_tz is None: | ||
raise EnvironmentError("Cannot determine current timezone") | ||
old_tzname = datetime.datetime.now(local_tz).tzname() | ||
|
||
os.environ["TZ"] = tzname | ||
time.tzset() | ||
yield | ||
os.environ["TZ"] = old_tzname | ||
time.tzset() |