Skip to content

Commit

Permalink
Merge pull request #533 from ashishnitinpatil/fix_350
Browse files Browse the repository at this point in the history
Add move_to to TickingDateTimeFactory
  • Loading branch information
bblommers authored Feb 16, 2024
2 parents 78b4437 + 6ed91d8 commit 10924ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
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 <[email protected]>`_
- `staticdev <[email protected]>`_
- `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

0 comments on commit 10924ba

Please sign in to comment.