diff options
| author | 2021-04-12 23:14:08 +0800 | |
|---|---|---|
| committer | 2021-04-12 23:14:08 +0800 | |
| commit | 48a7cca5c90841679a71eb8d0e902ad6dc943cc6 (patch) | |
| tree | 5511c136833aa07efc89c3eac98a9eb013cb53ca /bot/utils/scheduling.py | |
| parent | update comment (diff) | |
| parent | Merge pull request #1516 from ToxicKidz/sorted-available-channels (diff) | |
Merge branch 'main' into doc-imp
Diffstat (limited to '')
| -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..6843bae88 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 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) |