From 40369a493d0fc9b948dda7679e08a06fc85511ad Mon Sep 17 00:00:00 2001 From: Josh B Date: Tue, 21 Jan 2020 15:53:53 +1000 Subject: [PATCH] Add discord.utils.sleep_until helper function --- discord/utils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/discord/utils.py b/discord/utils.py index 83fdda2aa..53eccf82f 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)