Skip to content

Commit

Permalink
Add support for dt.timedelta to travel class
Browse files Browse the repository at this point in the history
  • Loading branch information
AgDude authored and adamchainz committed Sep 19, 2023
1 parent ac337ab commit 6531024
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* Add support for ``datetime.timedelta`` to ``time_machine.travel()``.

Thanks to Nate Dudenhoeffer in `PR #298 <https://github.com/adamchainz/time-machine/pull/298>`__.

* Add ``shift()`` method to the ``time_machine`` pytest fixture.

Thanks to Stefaan Lippens in `PR #312 <https://github.com/adamchainz/time-machine/pull/312>`__.
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ It may be:
* 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.
* A ``datetime.timedelta`` relative to the current real time.

.. |zoneinfo-instance| replace:: ``zoneinfo.ZoneInfo`` instance
.. _zoneinfo-instance: https://docs.python.org/3/library/zoneinfo.html#zoneinfo.ZoneInfo
Expand Down
7 changes: 7 additions & 0 deletions src/time_machine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
int,
float,
dt.datetime,
dt.timedelta,
dt.date,
str,
]
Expand Down Expand Up @@ -124,6 +125,12 @@ def extract_timestamp_tzname(
if dest.tzinfo is None:
dest = dest.replace(tzinfo=dt.timezone.utc)
timestamp = dest.timestamp()
elif isinstance(dest, dt.timedelta):
if coordinates_stack:
raise TypeError(
"timedelta destination is not supported when already time travelling."
)
timestamp = time() + dest.total_seconds()
elif isinstance(dest, dt.date):
timestamp = dt.datetime.combine(
dest, dt.time(0, 0), tzinfo=dt.timezone.utc
Expand Down
22 changes: 22 additions & 0 deletions tests/test_time_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,28 @@ def test_destination_generator():
assert time.time() == EPOCH + 13.0


def test_destination_delta():
now = time.time()
with time_machine.travel(dt.timedelta(seconds=3600)):
assert now + 3600 < time.time() < now + 3601


def test_destination_negative_delta():
now = time.time()
with time_machine.travel(dt.timedelta(seconds=-3600)):
assert now - 3600 < time.time() < now - 3599


@time_machine.travel(0)
def test_destination_delta_raises():
with pytest.raises(TypeError) as excinfo:
time_machine.travel(dt.timedelta(seconds=3600))

assert excinfo.value.args == (
"Timedelta destination is not supported when already time travelling.",
)


def test_traveller_object():
traveller = time_machine.travel(EPOCH + 10.0)
assert time.time() >= LIBRARY_EPOCH
Expand Down

0 comments on commit 6531024

Please sign in to comment.