Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tick() method to TickingDateTimeFactory #539

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>`_
- `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
Loading