Python resolution of datetime.now()

后端 未结 1 640
无人及你
无人及你 2020-12-21 04:27
from datetime import datetime
import time
for i in range(1000):
    curr_time  = datetime.now()
    print(curr_time)
    time.sleep(0.0001)

I was t

相关标签:
1条回答
  • 2020-12-21 04:59

    This may be a limitation of time.sleep on your system, rather than datetime.now()... or possibly both. What OS and what version and distribution of Python are you running on?

    Your system may not offer the "subsecond precision" mentioned in the time.sleep docs:

    sleep(...)
        sleep(seconds)
    
        Delay execution for a given number of seconds.  The argument may be
        a floating point number for subsecond precision.
    

    On Linux 3.x on amd64 with CPython 2.7, I get something pretty close to the 0.0001 time steps that you intended:

    2015-07-10 19:58:24.353711
    2015-07-10 19:58:24.353879
    2015-07-10 19:58:24.354052
    2015-07-10 19:58:24.354227
    2015-07-10 19:58:24.354401
    2015-07-10 19:58:24.354577
    2015-07-10 19:58:24.354757
    2015-07-10 19:58:24.354938
    
    0 讨论(0)
提交回复
热议问题