Skip to content

Commit

Permalink
Merge pull request #539 from vicfergar/fix/add-tick-to-ticking-date-t…
Browse files Browse the repository at this point in the history
…ime-factory

Add tick() method to TickingDateTimeFactory
  • Loading branch information
bblommers authored Apr 14, 2024
2 parents c65f4db + c1b8653 commit 10f5124
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Patches and Suggestions
- `staticdev <staticdev-support@proton.me>`_
- `Marcin Sulikowski <https://github.com/marcinsulikowski>`_
- `Ashish Patil <https://github.com/ashishnitinpatil>`_
- `Victor Ferrer <https://github.com/vicfergar>`_
7 changes: 7 additions & 0 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,13 @@ def __init__(self, time_to_freeze, start):
def __call__(self):
return self.time_to_freeze + (real_datetime.now() - self.start)

def tick(self, delta=datetime.timedelta(seconds=1)):
if isinstance(delta, numbers.Real):
# noinspection PyTypeChecker
self.move_to(self.time_to_freeze + datetime.timedelta(seconds=delta))
else:
self.move_to(self.time_to_freeze + delta)

def move_to(self, target_datetime):
"""Moves frozen date to the given ``target_datetime``"""
self.start = real_datetime.now()
Expand Down
16 changes: 16 additions & 0 deletions tests/test_ticking.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ def test_ticking_time():
assert time.time() > 1326585599.0


@utils.cpython_only
def test_ticking_tick():
with freeze_time("Jan 14th, 2012, 23:59:59", tick=True) as ft:
ft.tick(61)
time.sleep(0.001) # Deal with potential clock resolution problems
assert datetime.datetime.now().replace(
second=0, microsecond=0
) == datetime.datetime(2012, 1, 15, 0, 1, 0)

ft.tick(delta=datetime.timedelta(minutes=2))
time.sleep(0.001) # Deal with potential clock resolution problems
assert datetime.datetime.now().replace(
second=0, microsecond=0
) == datetime.datetime(2012, 1, 15, 0, 3, 0)


@utils.cpython_only
def test_ticking_move_to():
with freeze_time("Jan 14th, 2012, 23:59:59", tick=True) as ft:
Expand Down

0 comments on commit 10f5124

Please sign in to comment.