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 move_to to TickingDateTimeFactory #533

Merged
merged 1 commit into from
Feb 16, 2024
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 @@ -21,3 +21,4 @@ Patches and Suggestions
- `Hannes Ljungberg <hannes@5monkeys.se>`_
- `staticdev <staticdev-support@proton.me>`_
- `Marcin Sulikowski <https://github.com/marcinsulikowski>`_
- `Ashish Patil <https://github.com/ashishnitinpatil>`_
5 changes: 5 additions & 0 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ def __init__(self, time_to_freeze, start):
def __call__(self):
return self.time_to_freeze + (real_datetime.now() - self.start)

def move_to(self, target_datetime):
"""Moves frozen date to the given ``target_datetime``"""
self.start = real_datetime.now()
self.time_to_freeze = _parse_time_to_freeze(target_datetime)


class FrozenDateTimeFactory:

Expand Down
8 changes: 8 additions & 0 deletions tests/test_ticking.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def test_ticking_time():
assert time.time() > 1326585599.0


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


@utils.cpython_only_mark
@pytest.mark.parametrize("func_name",
("monotonic", "monotonic_ns", "perf_counter", "perf_counter_ns"),
Expand Down
Loading