简体   繁体   English

你如何在Python中获得两个时间对象之间的差异

[英]How do you get the difference between two time objects in Python

I have two datetime.time objects in Python, eg, 我在Python中有两个datetime.time对象,例如,

>>> x = datetime.time(9,30,30,0)
>>> y = datetime.time(9,30,31,100000)

However, when I do (yx) as what I would do for datetime.datetime object, I got the following error: 但是,当我像对datetime.datetime对象那样做(yx)时,我收到以下错误:

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    y-x
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

I need to get the yx value in microseconds, ie, in this case it should be 1100000. Any ideas? 我需要以微秒为单位得到yx值,即在这种情况下它应该是1100000.任何想法?

The class datetime.time does not support object subtraction for the same reason it doesn't support object comparison, ie because its objects might not define their tzinfo attribute: datetime.time不支持对象减法,原因与它不支持对象比较的原因相同,即因为它的对象可能没有定义它们的tzinfo属性:

comparison of time to time, where a is considered less than b when a precedes b in time. 比较时间,其中a在b之前的时间被认为小于b。 If one comparand is naive and the other is aware, TypeError is raised. 如果一个comparand是天真的而另一个是知道的,则会引发TypeError。 If both comparands are aware, and have the same tzinfo attribute, the common tzinfo attribute is ignored and the base times are compared. 如果两个比较都知道并且具有相同的tzinfo属性,则忽略公共tzinfo属性并比较基本时间。 If both comparands are aware and have different tzinfo attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from self.utcoffset()). 如果两个比较都知道并且具有不同的tzinfo属性,则首先通过减去它们的UTC偏移(从self.utcoffset()获得)来调整比较。 In order to stop mixed-type comparisons from falling back to the default comparison by object address, when a time object is compared to an object of a different type, TypeError is raised unless the comparison is == or !=. 为了阻止混合类型比较从对象地址回退到默认比较,当将时间对象与不同类型的对象进行比较时,除非比较为==或!=,否则引发TypeError。 The latter cases return False or True, respectively. 后一种情况分别返回False或True。

You should use datetime.datetime which include both the date and the time. 您应该使用包含日期和时间的datetime.datetime

If the two times refers to a single day, you can tell python that the date is today, with date.today() and combine the date with the time using datetime.combine . 如果两次引用一天,您可以使用date.today()告诉python日期是今天,并使用datetime.combine将日期与时间结合起来。

Now that you have datetimes you can perform subtraction, this will return a datetime.timedelta instance, which the method total_seconds() that will return the number of seconds (it's a float that includes the microseconds information). 既然你有日期时间你可以执行减法,这将返回一个datetime.timedelta实例,该方法total_seconds()将返回秒数(它是一个包含微秒信息的float )。 So multiply by 10 6 and you get the microseconds. 因此乘以10 6即可得到微秒。

from datetime import datetime, date, time

x = time(9, 30, 30, 0)
y = time(9, 30, 31, 100000)

diff = datetime.combine(date.today(), y) - datetime.combine(date.today(), x)
print diff.total_seconds() * (10 ** 6)        # 1100000.0

You can also just use timedeltas: 你也可以使用timedeltas:

from datetime import timedelta
x = timedelta(hours=9, minutes=30, seconds=30)
y = timedelta(hours=9, minutes=30, seconds=31, microseconds=100000)
print (y - x).total_seconds() * (10 ** 6)     # 1100000.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中找到两个日期时间对象之间的时差? - How do I find the time difference between two datetime objects in python? 如何在Python中获取两个字符串与时间和工作日的差异 - How to get Difference between two strings with time and weekday in Python 获取两个日期时间对象之间的时差,以小时,分钟和秒为单位 - Get time difference between two datetime objects as hour, min and sec python中时间对象之间的区别? - Difference between the time objects in python? 在python中以分钟为单位获取两个日期时间对象之间的差异 - Get the difference between two datetime objects in minutes in python 如何获取Python中两个字典的区别? - How to get the difference between two dictionaries in Python? 你如何比较python中的两个图像以返回数值差异和经过的时间? - How do you compare two images in python to return a numerical difference and time elapsed? 如何在 Python 中找到两个日期时间对象之间的毫秒差异? - How do I find the difference in milliseconds between two datetime objects in Python? 如何计算python中两个时区之间的时差? - How to compute the time difference between two time zones in python? 如何从不同的时区获取python daytime.time之间的正确差异? - How do I get the correct difference between python daytime.time from different time zones?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM