Python - time: how to check if seconds == 00

2.2k views Asked by At

In my script I want to run a function when the seconds are 00. For example if I start the script at 8:12:32, I want the function to start at 8:13:00. I have tried to use the datetime.datetime.now() but haven't figured out how to sort out the seconds from the time. In the Python 3 documentation I have also found time.second and datetime.second but i haven't had any success using them.

2

There are 2 answers

1
TigerhawkT3 On
import datetime, time
delta = 60 - datetime.datetime.now().second
time.sleep(delta)

The next statement will be executed on the minute (hh:mm:00).

0
Daniel On

Just use time.time() and time.sleep() to sleep till the start of the next minute with sub-second precision:

time.sleep(60 - time.time() % 60)