유경필
유경필

Reputation: 11

Questions about Python time_to_sleep?

When writing the code, the following code will be activated on time, saying it is stopped until the hour. However, to operate the code on time, it takes more than 59 minutes to run the code must run the code. Is there a way to make it work at 12pm by adjusting the minutes and hours instead of seconds in that code?


enter code here`current_time = time.time()
time_to_sleep = (60 - (current_time % 60))
time.sleep(time_to_sleep)

Upvotes: 1

Views: 26

Answers (2)

Dmitriy Neledva
Dmitriy Neledva

Reputation: 864

or you can do it like this:

import datetime
import time


while datetime.datetime(2022,10,25,10,28,0) > datetime.datetime.now():
   time.sleep(30)
    pass
print("it's time to act")

but @Tanner version seems to be more preferable

Upvotes: 1

Tanner
Tanner

Reputation: 181

Instead of sleep, you can pause until a defined time:

import pause
from datetime import datetime

pause.until(datetime(2022, 10, 25, 12))

Upvotes: 1

Related Questions