Python - 以毫秒为单位的时差对我不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18426882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - time difference in milliseconds not working for me
提问by Greg
I've read a few posts about this and thought I had some code that worked. If the difference between the 2 values is less than a 1sec then the millisecs displayed is correct.
我已经阅读了一些关于此的帖子,并认为我有一些有效的代码。如果 2 个值之间的差异小于 1 秒,则显示的毫秒是正确的。
If the difference is more than a sec, its still only showing me the difference of the millisecs.
如果差异超过一秒,它仍然只向我显示毫秒的差异。
As below.
如下。
Correct:
正确的:
now_wind 2013-08-25 08:43:04.776209
first_time_wind 2013-08-25 08:43:04.506301
time_diff 0:00:00.269908
diff 269
Wrong - this should be 2000 + 76?:
错误 - 这应该是 2000 + 76?:
now_wind 2013-08-25 08:43:25.660427
first_time_wind 2013-08-25 08:43:23.583902
time_diff 0:00:02.076525
diff 76
#!/usr/bin/env python
import datetime
import time
from time import sleep
first_time_wind = datetime.datetime.now()
sleep (2)
now_wind = datetime.datetime.now()
print "now_wind", now_wind
print "first_time_wind", first_time_wind
time_diff_wind = (now_wind - first_time_wind)
print "time_diff", time_diff_wind
print "diff", time_diff_wind.microseconds / 1000
采纳答案by blakev
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
>>> a
datetime.datetime(2013, 8, 25, 2, 5, 1, 879000)
>>> b
datetime.datetime(2013, 8, 25, 2, 5, 8, 984000)
>>> a - b
datetime.timedelta(-1, 86392, 895000)
>>> b - a
datetime.timedelta(0, 7, 105000)
>>> (b - a).microseconds
105000
>>> (b - a).seconds
7
>>> (b - a).microseconds / 1000
105
your microseconds don't include the seconds that have passed
你的微秒不包括已经过去的秒数
回答by Hyperboreus
From the documentation:
从文档:
Instance attributes (read-only):
Attribute Value
days Between -999999999 and 999999999 inclusive
seconds Between 0 and 86399 inclusive
microseconds Between 0 and 999999 inclusive
实例属性(只读):
属性值
-999999999 和 999999999 之间的天数
秒 0 到 86399 之间(含)
0 到 999999 之间的微秒
Microseconds never exceed 999,999. Hence your milliseconds never exceed 999.
微秒永远不会超过 999,999。因此,您的毫秒数永远不会超过 999。
回答by moliware
Try using total_seconds method:
尝试使用 total_seconds 方法:
print time_diff_wind.total_seconds() * 1000
That method is equivalent to: (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
该方法等效于: (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
Note: It's available since version 2.7
注意:它从 2.7 版开始可用
回答by Caumons
I faced this issue as well, but in my case I need real milliseconds precision, so using total_seconds() * 1000
isn't an option for me, so what I did is:
我也遇到了这个问题,但在我的情况下,我需要真正的毫秒精度,所以使用total_seconds() * 1000
不是我的选择,所以我所做的是:
def millis_interval(start, end):
"""start and end are datetime instances"""
diff = end - start
millis = diff.days * 24 * 60 * 60 * 1000
millis += diff.seconds * 1000
millis += diff.microseconds / 1000
return millis
I hope this helps someone else! :)
我希望这对其他人有帮助!:)
回答by shadow-light
The correct answer (in 2020) is:
正确答案(2020 年)是:
>>> from datetime import timedelta
>>> timedelta(days=1, milliseconds=50) / timedelta(milliseconds=1)
86400050.0
The other answers lose precision and/or are more verbose.
其他答案失去准确性和/或更冗长。