如何在Python中获得两个时间对象之间的差异 [英] How do you get the difference between two time objects in Python

查看:82
本文介绍了如何在Python中获得两个时间对象之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有两个datetime.time对象,例如

I have two datetime.time objects in Python, e.g.,

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

但是,当我这样做时(yx ),就像我对datetime.datetime对象所做的那样,出现了以下错误:

However, when I do (y-x) as what I would do for datetime.datetime object, I got the following error:

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'

我需要以微秒为单位获取yx值,即在这种情况下应该为1100000。有什么想法吗?

I need to get the y-x value in microseconds, i.e., in this case it should be 1100000. Any ideas?

推荐答案

datetime.time 不支持对象减法,其原因不支持对象比较,即因为其对象可能未定义其 tzinfo 属性:


进行时间比较,其中a在时间b之前被认为小于b。如果一个比较项是幼稚的而另一个比较项是知道的,则会引发TypeError。如果两个比较器都知道并且具有相同的tzinfo属性,则将忽略公共tzinfo属性,并比较基准时间。如果两个比较器都知道并且具有不同的tzinfo属性,则首先通过减去其UTC偏移量(从self.utcoffset()获得)来调整比较器。为了阻止混合类型比较回退到按对象地址进行的默认比较,将时间对象与其他类型的对象进行比较时,除非比较==或!=,否则将引发TypeError。后一种情况分别返回False或True。

comparison of time to time, where a is considered less than b when a precedes b in time. If one comparand is naive and the other is aware, TypeError is raised. If both comparands are aware, and have the same tzinfo attribute, the common tzinfo attribute is ignored and the base times are compared. If both comparands are aware and have different tzinfo attributes, the comparands are first adjusted by subtracting their UTC offsets (obtained from 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 !=. The latter cases return False or True, respectively.

您应使用 datetime.datetime ,其中包括日期和时间。

You should use datetime.datetime which include both the date and the time.

如果两次是同一天,则可以使用 date.today() 并使用< a href = https://docs.python.org/2/library/datetime.html#datetime.datetime.combine rel = noreferrer> datetime.combine

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.

现在您可以执行减法操作了,因此将返回 datetime.timedelta 实例,该实例使用 total_seconds() 将返回秒数(这是 float ,其中包含微秒信息)。因此,乘以10 6 即可得到微秒。

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). So multiply by 106 and you get the microseconds.

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

您也可以只使用timedelta:

You can also just use 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

这篇关于如何在Python中获得两个时间对象之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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