Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy tests to use change_local_timezone() #385

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions tests/test_time_machine.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

import asyncio
import contextlib
import datetime as dt
import os
import sys
import time
import typing
import uuid
from contextlib import contextmanager
from importlib.util import module_from_spec
from importlib.util import spec_from_file_location
from unittest import mock
Expand Down Expand Up @@ -38,6 +38,21 @@
)


@contextmanager
def change_local_timezone(local_tz: str | None) -> typing.Iterator[None]:
orig_tz = os.environ["TZ"]
if local_tz:
os.environ["TZ"] = local_tz
else:
del os.environ["TZ"]
time.tzset()
try:
yield
finally:
os.environ["TZ"] = orig_tz
time.tzset()


@pytest.mark.skipif(
not hasattr(time, "CLOCK_REALTIME"), reason="No time.CLOCK_REALTIME"
)
Expand Down Expand Up @@ -403,22 +418,15 @@ def test_destination_datetime_tzinfo_zoneinfo_nested():


def test_destination_datetime_tzinfo_zoneinfo_no_orig_tz():
orig_tz = os.environ["TZ"]
del os.environ["TZ"]
time.tzset()
orig_tzname = time.tzname

try:
with change_local_timezone(None):
orig_tzname = time.tzname
dest = LIBRARY_EPOCH_DATETIME.replace(tzinfo=ZoneInfo("Africa/Addis_Ababa"))

with time_machine.travel(dest):
assert time.tzname == ("EAT", "EAT")

assert time.tzname == orig_tzname

finally:
os.environ["TZ"] = orig_tz
time.tzset()


def test_destination_datetime_tzinfo_zoneinfo_windows():
orig_timezone = time.timezone
Expand Down Expand Up @@ -469,18 +477,6 @@ def test_destination_string():
assert time.time() == EPOCH + 60.0


@contextlib.contextmanager
def change_local_timezone(local_tz: str) -> typing.Iterator[None]:
orig_tz = os.environ["TZ"]
os.environ["TZ"] = local_tz
time.tzset()
try:
yield
finally:
os.environ["TZ"] = orig_tz
time.tzset()


@pytest.mark.parametrize(
["local_tz", "expected_offset"],
[
Expand Down