diff options
| author | Josh B <[email protected]> | 2020-01-21 15:53:53 +1000 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2020-01-21 03:18:25 -0500 |
| commit | 40369a493d0fc9b948dda7679e08a06fc85511ad (patch) | |
| tree | 887b0a0583a0e53b08da7d170f271bfcb070039b | |
| parent | [tasks] Fix issue with next_iteration when task overruns time allotted (diff) | |
| download | discord.py-40369a493d0fc9b948dda7679e08a06fc85511ad.tar.xz discord.py-40369a493d0fc9b948dda7679e08a06fc85511ad.zip | |
Add discord.utils.sleep_until helper function
| -rw-r--r-- | discord/utils.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/discord/utils.py b/discord/utils.py index 83fdda2a..53eccf82 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -338,6 +338,24 @@ async def sane_wait_for(futures, *, timeout): return done +async def sleep_until(when): + """Sleep until a specified time. + + If the time supplied is in the past this function will yield instantly. + + Parameters + ----------- + when: :class:`datetime.datetime` + The timestamp in which to sleep until. + + .. versionadded:: 1.3.0 + """ + if when.tzinfo is None: + when = when.replace(tzinfo=datetime.timezone.utc) + now = datetime.datetime.now(datetime.timezone.utc) + delta = (when - now).total_seconds() + await asyncio.sleep(max(delta, 0)) + def valid_icon_size(size): """Icons must be power of 2 within [16, 4096].""" return not size & (size - 1) and size in range(16, 4097) |