diff options
| author | 2021-04-01 19:16:46 +0100 | |
|---|---|---|
| committer | 2021-04-01 19:16:46 +0100 | |
| commit | ac660e67eab0407b65b6d8cbe0a69181901f06bb (patch) | |
| tree | 1f9aa2c90cab312d9f1a0fddbebdabf09588b5b8 | |
| parent | Remove redundant traceback log in log.exception (diff) | |
Update schedule_at() to work with tz aware date times
| -rw-r--r-- | bot/utils/scheduling.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 4dd036e4f..b3f62257a 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -59,14 +59,18 @@ class Scheduler: def schedule_at(self, time: datetime, task_id: t.Hashable, coroutine: t.Coroutine) -> None: """ - Schedule `coroutine` to be executed at the given naïve UTC `time`. + Schedule `coroutine` to be executed at the given `time`. + + If `time` is timezone aware, then use that timezone to calculate now() when subtracting. + If `time` is naïve, then we use UTC. If `time` is in the past, schedule `coroutine` immediately. If a task with `task_id` already exists, close `coroutine` instead of scheduling it. This prevents unawaited coroutine warnings. Don't pass a coroutine that'll be re-used elsewhere. """ - delay = (time - datetime.utcnow()).total_seconds() + now_datetime = datetime.now(time.tzinfo) if time.tzinfo else datetime.utcnow() + delay = (time - now_datetime).total_seconds() if delay > 0: coroutine = self._await_later(delay, task_id, coroutine) |