Skip to content

Commit

Permalink
Add support for dt.timedelta to travel class (adamchainz#298)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Johnson <[email protected]>
  • Loading branch information
AgDude and adamchainz authored Sep 19, 2023
1 parent ac337ab commit 483907d
Show file tree
Hide file tree
Showing 4 changed files with 28 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
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ It may be:
If it has ``tzinfo`` set to a |zoneinfo-instance|_, the current timezone will also be mocked.
* A ``datetime.date``.
This will be converted to a UTC datetime with the time 00:00:00.
* A ``datetime.timedelta``.
This will be interpreted relative to the current time.
If already within a ``travel()`` block, the ``shift()`` method is easier to use (documented below).
* 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.
Expand Down
3 changes: 3 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,8 @@ def extract_timestamp_tzname(
if dest.tzinfo is None:
dest = dest.replace(tzinfo=dt.timezone.utc)
timestamp = dest.timestamp()
elif isinstance(dest, dt.timedelta):
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
18 changes: 18 additions & 0 deletions tests/test_time_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,24 @@ def test_destination_date():
assert time.time() == EPOCH


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


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


def test_destination_timedelta_nested():
with time_machine.travel(EPOCH):
with time_machine.travel(dt.timedelta(seconds=10)):
assert time.time() == EPOCH + 10.0


@time_machine.travel("1970-01-01 00:01 +0000")
def test_destination_string():
assert time.time() == EPOCH + 60.0
Expand Down

0 comments on commit 483907d

Please sign in to comment.