Skip to content

Commit

Permalink
Add move_to to TickingDateTimeFactory; fix factories init.
Browse files Browse the repository at this point in the history
  • Loading branch information
priezz authored Dec 19, 2023
1 parent cdde1d5 commit b48ad3c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,18 +519,25 @@ def _parse_tz_offset(tz_offset):

class TickingDateTimeFactory:

def __init__(self, time_to_freeze, start):
def __init__(self, time_to_freeze, tz_offset):
self.time_to_freeze = time_to_freeze
self.start = start
self.start = real_datetime.now()
self.tz_offset = tz_offset

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.time_to_freeze = _parse_time_to_freeze(target_datetime, self.tz_offset)
self.start = real_datetime.now()


class FrozenDateTimeFactory:

def __init__(self, time_to_freeze):
def __init__(self, time_to_freeze, tz_offset):
self.time_to_freeze = time_to_freeze
self.tz_offset = tz_offset

def __call__(self):
return self.time_to_freeze
Expand All @@ -551,9 +558,10 @@ def move_to(self, target_datetime):

class StepTickTimeFactory:

def __init__(self, time_to_freeze, step_width):
def __init__(self, time_to_freeze, step_width, tz_offset):
self.time_to_freeze = time_to_freeze
self.step_width = step_width
self.tz_offset = tz_offset

def __call__(self):
return_time = self.time_to_freeze
Expand Down Expand Up @@ -671,11 +679,11 @@ def __exit__(self, *args):

def start(self):
if self.auto_tick_seconds:
freeze_factory = StepTickTimeFactory(self.time_to_freeze, self.auto_tick_seconds)
freeze_factory = StepTickTimeFactory(self.time_to_freeze, self.auto_tick_seconds, self.tz_offset)
elif self.tick:
freeze_factory = TickingDateTimeFactory(self.time_to_freeze, real_datetime.now())
freeze_factory = TickingDateTimeFactory(self.time_to_freeze, self.tz_offset)
else:
freeze_factory = FrozenDateTimeFactory(self.time_to_freeze)
freeze_factory = FrozenDateTimeFactory(self.time_to_freeze, self.tz_offset)

is_already_started = len(freeze_factories) > 0
freeze_factories.append(freeze_factory)
Expand Down

0 comments on commit b48ad3c

Please sign in to comment.