关于windows:datetime.now()的Python解析

Python resolution of datetime.now()

1
2
3
4
5
6
from datetime import datetime
import time
for i in range(1000):
    curr_time  = datetime.now()
    print(curr_time)
    time.sleep(0.0001)

我正在测试datetime.now()的分辨率。 由于它假设以微秒为单位输出,我预计每次打印都会有所不同。

但是,我总是得到类似的东西。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
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进行了比较
从datetime import datetime,timedelta
进口时间

1
2
3
4
5
6
7
8
9
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('
'
)

    time.sleep(0.000001)

我不确定它到底有多"准确",但分辨率至少要高......这似乎并不重要,因为我的电脑甚至无法以高速打印。 出于我的目的,我只需要不同的时间戳来区分不同的条目,这对我来说已经足够了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
...
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

...


这可能是系统上time.sleep的限制,而不是datetime.now() ...或可能两者都有。 您运行的是什么操作系统以及Python的哪个版本和发行版?

您的系统可能不提供time.sleep文档中提到的"亚秒级精度":

1
2
3
4
5
sleep(...)
    sleep(seconds)

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

在使用CPython 2.7的amd64上的Linux 3.x上,我得到的东西非常接近你想要的0.0001时间步长:

1
2
3
4
5
6
7
8
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