Skip to content

Commit

Permalink
Reduced nesting in the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
adambirds committed Jul 27, 2024
1 parent cccdeb1 commit ea35ab5
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions twitchio/ext/routines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,15 @@ def start_time(self) -> Optional[datetime.datetime]:
@property
def next_run(self) -> Optional[datetime.datetime]:
"""Calculate and return the next run time of the routine."""
now = datetime.datetime.now(datetime.timezone.utc)

if self._time:
# If _time is set, calculate the next occurrence
next_run = self._time + datetime.timedelta(days=self._completed_loops)
if next_run < now:
next_run += datetime.timedelta(days=1)
elif self._delta:
# If _delta is set, calculate the next run based on the last start time and delta
next_run = self._start_time + datetime.timedelta(seconds=self._delta * (self._completed_loops + 1))
else:
# If neither _time nor _delta is set, return None
return None

# Ensure that the next run time is not in the past
now = datetime.datetime.now(datetime.timezone.utc)
if next_run < now:
if self._time:
next_run = next_run + datetime.timedelta(days=1)
else:
if next_run < now:
next_run = now + datetime.timedelta(seconds=self._delta)

return next_run
Expand Down

0 comments on commit ea35ab5

Please sign in to comment.