Skip to content

Commit

Permalink
Add property to calculate and expose next run time of routine.
Browse files Browse the repository at this point in the history
  • Loading branch information
adambirds committed Aug 16, 2024
1 parent 8e91cec commit fed7b25
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions twitchio/ext/routines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ def start_time(self) -> Optional[datetime.datetime]:
"""
return self._start_time

@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:
next_run = self._time + datetime.timedelta(days=self._completed_loops)
if next_run < now:
next_run += datetime.timedelta(days=1)
elif self._delta:
next_run = self._start_time + datetime.timedelta(seconds=self._delta * (self._completed_loops + 1))
if next_run < now:
next_run = now + datetime.timedelta(seconds=self._delta)

return next_run

def _can_be_cancelled(self) -> bool:
return self._task and not self._task.done()

Expand Down

0 comments on commit fed7b25

Please sign in to comment.