Import a datetime
The import time
While True:
Print (' to 'datetime. Datetime. Now ())
# take next minute
Next_20=datetime. Datetime. Now () + datetime. Timedelta (minutes=1)
# next minute 20 seconds
Y - next_20_str=next_20. Strftime (' % % m - H: % d % % m: 20. % f ') # next minute 20 seconds
# is converted into the format
Next_20_time=datetime. Datetime. Strptime (next_20_str, '% % Y - m - H: % d % % m: % s. % f')
Now=datetime. Datetime. Now ()
# seconds apart
Interval_time=(next_20_time - now). Total_seconds ()
Time. Sleep (interval_time)
CodePudding user response:
Timing function can learn about the schedule libraryOn the timing function code is relatively simple
CodePudding user response:
Oh, the ready-made wheelsCodePudding user response:
Use the replace more directlyImport a datetime
The import time
While True:
Print (' to 'datetime. Datetime. Now ())
# take next minute
Next_time=datetime. Datetime. Now () + datetime. Timedelta (minutes=1)
# next minute 20 seconds
Next_20=next_time. replace (second=20)
Now=datetime. Datetime. Now ()
# seconds apart
Interval_time=(next_20 - now). Total_seconds ()
Time. Sleep (interval_time)