Python的分辨率datetime.now() [英] Python resolution of datetime.now()

查看:131
本文介绍了Python的分辨率datetime.now()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $




(curr_time)
time.sleep(0.0001)

我正在测试 datetime.now()。因为它假设以微秒输出,我预计每个打印将是不同的。



但是,我总是得到这样的东西。

  ... 
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38 :47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015- 07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07- 10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22 :38:47.213074
...

为什么发生这种情况?有什么办法可以得到一个精确的时间戳到微秒吗?实际上我不需要微秒,但是得到0.1ms的分辨率是很好的。



===更新====



我将它与使用time.perf_counter()进行比较,并从datetime import datetime,timedelta
添加到起始 datetime
进口时间

  datetime0 = datetime.now()
t0 = time.perf_counter()
为i在范围(1000):
print('datetime.now():',datetime.now())
print('time.perf_counter():',datetime0 + timedelta(0,time。 perf_counter() - t0))
print('\\\
')

time.sleep(0.000001)

我不知道它的确是如何准确,但分辨率至少要高一些....这似乎并不重要,因为我的电脑甚至无法打印速度那么高。为了我的目的,我只需要不同的时间戳来区分不同的条目,这对我来说就够了。

  ... 
datetime.now():2015-07-10 23:24:18.010377
time.perf_counter():2015-07-10 23:24:18.010352


datetime.now():2015-07-10 23:24:18.010377
time.perf_counter():2015-07-10 23:24:18.010545


日期时间.now():2015-07-10 23:24:18.010377
time.perf_counter():2015-07-10 23:24:18.010745


datetime.now ():2015-07-10 23:24:18.011377
time.perf_counter():2015-07-10 23:24:18.010961


datetime.now() :2015-07-10 23:24:18.011377
time.perf_counter():2015-07-10 23:24:18.011155


datetime.now():2015 -07-10 23:24:18.011377
time.perf_counter():2015-07-10 23:24:18.011369


datetime.now():2015-07 -10 23:24:18.011377
time.perf_counter():2015-07-10 23:24:18.011596


datetime.now():2015-07-10 23:24:18.012 379
time.perf_counter():2015-07-10 23:24:18.011829


datetime.now():2015-07-10 23:24:18.012379
time.perf_counter():2015-07-10 23:24:18.012026


datetime.now():2015-07-10 23:24:18.012379
time.perf_counter():2015-07-10 23:24:18.012232


datetime.now():2015-07-10 23:24:18.012379
时间.perf_counter():2015-07-10 23:24:18.012424


datetime.now():2015-07-10 23:24:18.012379
time.perf_counter ():2015-07-10 23:24:18.012619


datetime.now():2015-07-10 23:24:18.013380
time.perf_counter() :2015-07-10 23:24:18.012844


datetime.now():2015-07-10 23:24:18.013380
time.perf_counter():2015 -07-10 23:24:18.013044


datetime.now():2015-07-10 23:24:18.013380
time.perf_counter():2015-07 -10 23:24:18.013242


datetime.now():2015-07-10 23:24:18.013380
time.perf_counter():2015-07-10 23:24:18.013437


datet ime.now():2015-07-10 23:24:18.013380
time.perf_counter():2015-07-10 23:24:18.013638


datetime。 now():2015-07-10 23:24:18.014379
time.perf_counter():2015-07-10 23:24:18.013903


datetime.now( ):2015-07-10 23:24:18.014379
time.perf_counter():2015-07-10 23:24:18.014125


datetime.now(): 2015-07-10 23:24:18.014379
time.perf_counter():2015-07-10 23:24:18.014328


datetime.now():2015- 07-10 23:24:18.014379
time.perf_counter():2015-07-10 23:24:18.014526


datetime.now():2015-07- 10 23:24:18.014379
time.perf_counter():2015-07-10 23:24:18.014721


datetime.now():2015-07-10 23 :24:18.015381
time.perf_counter():2015-07-10 23:24:18.014919

...


解决方案

这可能是系统上 time.sleep 的限制,而不是 datetime.now() ...或者可能两者。



您的系统可能无法提供中提到的亚秒精度 code> time.sleep docs:

  sleep(...)
睡眠(秒)

延迟执行给定的秒数。参数可以是
一个浮点数,用于亚秒精度。

在带有CPython 2.7的amd64上的Linux 3.x上,我得到了一些东西非常接近您打算的0.0001时间步骤:

  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


from datetime import datetime
import time
for i in range(1000):
    curr_time  = datetime.now()
    print(curr_time)
    time.sleep(0.0001)

I was testing the resolution of datetime.now(). Since it supposes to output in microsecond, I expected that each print will be different.

However, I always get something like that.

...
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
...

Why does that happen? Is there any way that I can get an accurate timestamp down to the microsecond? Actually I don't need microseconds, but it would be nice to get 0.1ms resolution.

=== UPDATE ====

I compared it with using time.perf_counter() and adding to the starting datetime from datetime import datetime, timedelta import time

datetime0 = datetime.now()
t0 = time.perf_counter()
for i in range(1000):
    print('datetime.now(): ', datetime.now())
    print('time.perf_counter(): ', datetime0 + timedelta(0, time.perf_counter()-t0))
    print('\n')

    time.sleep(0.000001)

I am not sure how 'accurate' it really is, but the resolution is at least higher.... it doesn't seems to matter as my computer cannot even print at a speed that high. For my purpose, which I simply need different timestamps to distinguish different entries, this is good enough for me.

...
datetime.now():  2015-07-10 23:24:18.010377
time.perf_counter():  2015-07-10 23:24:18.010352


datetime.now():  2015-07-10 23:24:18.010377
time.perf_counter():  2015-07-10 23:24:18.010545


datetime.now():  2015-07-10 23:24:18.010377
time.perf_counter():  2015-07-10 23:24:18.010745


datetime.now():  2015-07-10 23:24:18.011377
time.perf_counter():  2015-07-10 23:24:18.010961


datetime.now():  2015-07-10 23:24:18.011377
time.perf_counter():  2015-07-10 23:24:18.011155


datetime.now():  2015-07-10 23:24:18.011377
time.perf_counter():  2015-07-10 23:24:18.011369


datetime.now():  2015-07-10 23:24:18.011377
time.perf_counter():  2015-07-10 23:24:18.011596


datetime.now():  2015-07-10 23:24:18.012379
time.perf_counter():  2015-07-10 23:24:18.011829


datetime.now():  2015-07-10 23:24:18.012379
time.perf_counter():  2015-07-10 23:24:18.012026


datetime.now():  2015-07-10 23:24:18.012379
time.perf_counter():  2015-07-10 23:24:18.012232


datetime.now():  2015-07-10 23:24:18.012379
time.perf_counter():  2015-07-10 23:24:18.012424


datetime.now():  2015-07-10 23:24:18.012379
time.perf_counter():  2015-07-10 23:24:18.012619


datetime.now():  2015-07-10 23:24:18.013380
time.perf_counter():  2015-07-10 23:24:18.012844


datetime.now():  2015-07-10 23:24:18.013380
time.perf_counter():  2015-07-10 23:24:18.013044


datetime.now():  2015-07-10 23:24:18.013380
time.perf_counter():  2015-07-10 23:24:18.013242


datetime.now():  2015-07-10 23:24:18.013380
time.perf_counter():  2015-07-10 23:24:18.013437


datetime.now():  2015-07-10 23:24:18.013380
time.perf_counter():  2015-07-10 23:24:18.013638


datetime.now():  2015-07-10 23:24:18.014379
time.perf_counter():  2015-07-10 23:24:18.013903


datetime.now():  2015-07-10 23:24:18.014379
time.perf_counter():  2015-07-10 23:24:18.014125


datetime.now():  2015-07-10 23:24:18.014379
time.perf_counter():  2015-07-10 23:24:18.014328


datetime.now():  2015-07-10 23:24:18.014379
time.perf_counter():  2015-07-10 23:24:18.014526


datetime.now():  2015-07-10 23:24:18.014379
time.perf_counter():  2015-07-10 23:24:18.014721


datetime.now():  2015-07-10 23:24:18.015381
time.perf_counter():  2015-07-10 23:24:18.014919

...

解决方案

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

这篇关于Python的分辨率datetime.now()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆