From fed7b2519872f35963ea7f08df713dca862f0cd5 Mon Sep 17 00:00:00 2001 From: Adam Birds Date: Fri, 16 Aug 2024 03:37:16 +0100 Subject: [PATCH] Add property to calculate and expose next run time of routine. --- twitchio/ext/routines/__init__.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/twitchio/ext/routines/__init__.py b/twitchio/ext/routines/__init__.py index 89d4867c..c1770f3f 100644 --- a/twitchio/ext/routines/__init__.py +++ b/twitchio/ext/routines/__init__.py @@ -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()