2

I have been thinking to do a sleep function where it sleeps until a certain date is called. My idea was based of date such as : 2019-01-20 12:00:00.

I haven't really figured out how to start to solve this problem. My idea was something similar such as

if there is a date given:
   time.sleep(until the date and time)

So the question is how can I possibly be able to sleep until a certain time given in a value of 2019-01-20 12:00:00?

martineau
  • 119,623
  • 25
  • 170
  • 301
Hellosiroverthere
  • 285
  • 10
  • 19
  • *Hint:* `time.sleep()` expects seconds. Look at `time.strptime()` to convert a string to seconds since 1970-01-01 00:00. – BoarGules Jan 13 '19 at 21:02
  • If you want to do this, because some code should run at that time, look at the Python standard library, it comes with a scheduler! https://docs.python.org/3/library/sched.html – 576i Jan 13 '19 at 21:33
  • Does this answer your question? [In Python, how can I put a thread to sleep until a specific time?](https://stackoverflow.com/questions/2031111/in-python-how-can-i-put-a-thread-to-sleep-until-a-specific-time) – wisbucky Dec 31 '21 at 06:47

4 Answers4

5

Easy, calculate how long it is, and sleep the time.

You can calculate how long it takes until your wakeup time is reached and sleep for the delta time.

Python can calculate with time intervals. If you subtract one timestamp from another, then you get a datetime.timedelta:

import datetime
import time

target = datetime.datetime(2019,1,20,12,0,0)

now = datetime.datetime.now()
delta = target - now
if delta > datetime.timedelta(0):
    print('will sleep: %s' % delta)
    time.sleep(delta.total_seconds())
    print('just woke up')

of course, you can put that in a function:

import datetime
import time

target = datetime.datetime(2019,1,20,12,0,0)


def sleep_until(target):
    now = datetime.datetime.now()
    delta = target - now

    if delta > datetime.timedelta(0):
        time.sleep(delta.total_seconds())
        return True


sleep_until(target)

You can check the return value: only if it slept, it returns True.

BTW: it's OK, to use a date in the past as target. This will generate a negative number of seconds. Sleeping a negative value will just not sleep.

if your time is a string, use this:

target = datetime.datetime.strptime('20.1.2019 20:00:00', '%d.%m.%Y %H:%M:%s')

or

target = datetime.datetime.strptime('2019-1-20 20:00:00', '%Y-%m-%d %H:%M:%s')
Community
  • 1
  • 1
Jörg Beyer
  • 3,631
  • 21
  • 35
  • Do you mean by calculate how many seconds it is from now until the date that is given? I could do that but I thought maybe there is a better way where you just give a function a date and hours which automatically will sleep until then :) ? – Hellosiroverthere Jan 13 '19 at 21:01
  • Oh right! It work as I wish! The only little certain I have is that I would like to enter `2019-01-20 12:00:00` inside the `datetime.datetime` which I believe wont work since its a string and not a datetime object. Do you maybe think there is a way without entering manually the date as you did with `2019,1,20,12,0,0` I would understand if you don't have the time for it of course but either way! This was the answer that actually worked. It would be lovely if I could just paste in `2019-01-20 12:00:00` inside the datetime – Hellosiroverthere Jan 13 '19 at 21:20
  • 1
    There it is! the `target = datetime.datetime.strptime('2019-1-20 20:00:00', '%Y-%m-%d %H:%M:%s')` was the solution til what I wanted! I really appreciate it and I thank you!! :) I have now listed this as answer! – Hellosiroverthere Jan 13 '19 at 21:24
1

I did your problem in an efficient way:

import time, datetime
# given_date --> Your target time and date
dur = time.mktime(datetime.datetime.strptime(given_date, "%d-%b-%y %H:%M:%S").timetuple()) - time.time()
time.sleep(dur)
MrVahid
  • 11
  • 1
0

you mean something like this:

from time import sleep
from datetime import datetime

x = datetime.datetime.strptime('2019-01-20 12:00:00', '%Y-%m-%d %H:%M:%S')
y = datetime.now()

sleep((max(x,y) - min(x,y)).seconds)
sam46
  • 1,273
  • 9
  • 12
  • I guess something like that yes. I assume it should be x-y since you will get negative if you do time now - some date? But yeah. and where x would be = 2019-01-20 12:00:00 – Hellosiroverthere Jan 13 '19 at 21:05
  • Oh, Thats strange. I tried this using `x = 2019-01-20 12:00:00` and I am getting ` print(((max(x,y) - min(x,y)).seconds)) ----- TypeError: '>' not supported between instances of 'datetime.datetime' and 'str'` – Hellosiroverthere Jan 13 '19 at 21:12
  • x must be a datetime object not a string – sam46 Jan 13 '19 at 21:15
-1

You can use a while loop.

import datetime
_theday="01/01/2019"
while datetime.today() != _theday:
    some stuff
Fatih1923
  • 2,665
  • 3
  • 21
  • 28
  • 2
    This is a form of [busy waiting](https://en.wikipedia.org/wiki/Busy_waiting) and is terribly inefficient. I don't recommend this approach. Something that involves making the thread sleep is far better, especially if the time to wait is long. – John Szakmeister Jan 13 '19 at 21:07