diff --git a/README.rst b/README.rst index ff85c37..aed95ed 100644 --- a/README.rst +++ b/README.rst @@ -79,7 +79,7 @@ It may be: This will be converted to a UTC datetime with the time 00:00:00. * A ``float`` or ``int`` specifying a `Unix timestamp `__ * A string, which will be parsed with `dateutil.parse `__ and converted to a timestamp. - Again, if the result is naive, it will be assumed to have the UTC time zone. + If the result is naive, it will be assumed to be local time. .. |zoneinfo-instance| replace:: ``zoneinfo.ZoneInfo`` instance .. _zoneinfo-instance: https://docs.python.org/3/library/zoneinfo.html#zoneinfo.ZoneInfo diff --git a/tests/test_time_machine.py b/tests/test_time_machine.py index 1b71115..94f5ce5 100644 --- a/tests/test_time_machine.py +++ b/tests/test_time_machine.py @@ -1,10 +1,12 @@ from __future__ import annotations import asyncio +import contextlib import datetime as dt import os import sys import time +import typing import uuid from importlib.util import module_from_spec from importlib.util import spec_from_file_location @@ -433,6 +435,33 @@ 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"], + [ + ("UTC", 0), + ("Europe/Amsterdam", -3600), + ("US/Eastern", 5 * 3600), + ], +) +@pytest.mark.parametrize("destination", ["1970-01-01 00:00", "1970-01-01"]) +def test_destination_string_naive(local_tz, expected_offset, destination): + with change_local_timezone(local_tz): + with time_machine.travel(destination): + assert time.time() == EPOCH + expected_offset + + @time_machine.travel(lambda: EPOCH + 140.0) def test_destination_callable_lambda_float(): assert time.time() == EPOCH + 140.0