Skip to content

Commit

Permalink
Tidy tests to use change_local_timezone()
Browse files Browse the repository at this point in the history
Added in #306.
  • Loading branch information
adamchainz committed Sep 19, 2023
1 parent 3c70dd5 commit c69bd39
Showing 1 changed file with 19 additions and 23 deletions.
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

0 comments on commit c69bd39

Please sign in to comment.