Skip to content

Commit

Permalink
Issue #257: fix documentation about using local time for naive date(t…
Browse files Browse the repository at this point in the history
…ime) strings
  • Loading branch information
soxofaan committed Dec 2, 2022
1 parent 333ab69 commit c5b83c0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://en.m.wikipedia.org/wiki/Unix_time>`__
* A string, which will be parsed with `dateutil.parse <https://dateutil.readthedocs.io/en/stable/parser.html>`__ 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
Expand Down
29 changes: 29 additions & 0 deletions tests/test_time_machine.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c5b83c0

Please sign in to comment.