1

I'm trying to write a code to execute a print function at a specific local time in microsecond resolution. I'm using python 3.6. My simple code is:

from datetime import datetime
while True:
    now=datetime.now()
    now=(now.hour,now.minute,now.second,now.microsecond)
    if now>(0,42,40,0):
        print(now)
        break

these are some results: for time (0,42,40,0) result is:(0, 42, 40, 255) for time (0,49,0,0) result is:(0, 49, 0, 841) for time (0,51,0,0) result is (0, 51, 30, 86). the problem is every time i run this code this code starts at different times with a random distribution in microseconds while I need the delay to be constant in 1 microsecond accuracy. my OS is Windows 10. I'll be appreciate if anybody helps me to solve this problem. thanks.

  • Depending on the implementation and OS, the accuracy of time can be of different accuracies. For example on my (mac) computer it is accurate to about 10 microseconds. – Eric Jin May 13 '20 at 22:27
  • thank you Eric, actually I ran this coed on Ubuntu OS too and it didn't change too much. – Hamidreza Gharib May 14 '20 at 11:34
  • may be of interest although not python-specific: https://stackoverflow.com/q/2607263/10197418 - and also https://stackoverflow.com/questions/1938048/high-precision-clock-in-python – FObersteiner May 14 '20 at 17:05

0 Answers0