python 在python中找到2次之间差异的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51010/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:27:20  来源:igfitidea点击:

What is the simplest way to find the difference between 2 times in python?

pythondatetimetime

提问by airportyh

I have 2 time values which have the type datetime.time. I want to find their difference. The obvious thing to do is t1 - t2, but this doesn't work. It works for objects of type datetime.datetimebut not for datetime.time. So what is the best way to do this?

我有 2 个类型为 的时间值datetime.time。我想找出它们的不同之处。显而易见的事情是 t1 - t2,但这不起作用。它适用于类型的对象,datetime.datetime但不适用于datetime.time. 那么最好的方法是什么?

采纳答案by Jason Etheridge

Firstly, note that a datetime.time is a time of day, independent of a given day, and so the different between any two datetime.time values is going to be less than 24 hours.

首先,请注意 datetime.time 是一天中的时间,与给定的一天无关,因此任何两个 datetime.time 值之间的差异将小于 24 小时。

One approach is to convert both datetime.time values into comparable values (such as milliseconds), and find the difference.

一种方法是将两个 datetime.time 值转换为可比较的值(例如毫秒),然后找出差异。

t1, t2 = datetime.time(...), datetime.time(...)

t1_ms = (t1.hour*60*60 + t1.minute*60 + t1.second)*1000 + t1.microsecond
t2_ms = (t2.hour*60*60 + t2.minute*60 + t2.second)*1000 + t2.microsecond

delta_ms = max([t1_ms, t2_ms]) - min([t1_ms, t2_ms])

It's a little lame, but it works.

这有点蹩脚,但它有效。

回答by Blair Conrad

Also a little silly, but you could try picking an arbitrary day and embedding each time in it, using datetime.datetime.combine, then subtracting:

也有点傻,但您可以尝试选择任意一天并每次嵌入其中,使用datetime.datetime.combine,然后减去:

>>> import datetime
>>> t1 = datetime.time(2,3,4)
>>> t2 = datetime.time(18,20,59)
>>> dummydate = datetime.date(2000,1,1)
>>> datetime.datetime.combine(dummydate,t2) - datetime.datetime.combine(dummydate,t1)
datetime.timedelta(0, 58675)

回答by chryss

You could transform both into timedelta objectsand subtract these from each other, which will take care to of the carry-overs. For example:

您可以将两者都转换为timedelta 对象并将它们从彼此中减去,这将处理结转。例如:

>>> import datetime as dt
>>> t1 = dt.time(23, 5, 5, 5)
>>> t2 = dt.time(10, 5, 5, 5)
>>> dt1 = dt.timedelta(hours=t1.hour, minutes=t1.minute, seconds=t1.second, microseconds=t1.microsecond)
>>> dt2 = dt.timedelta(hours=t2.hour, minutes=t2.minute, seconds=t2.second, microseconds=t2.microsecond)
>>>  print(dt1-dt2)
13:00:00
>>> print(dt2-dt1)
-1 day, 11:00:00
>>> print(abs(dt2-dt1))
13:00:00

Negative timedelta objects in Python get a negative day field, with the other fields positive. You could check beforehand: comparison works on both time objects and timedelta objects:

Python 中的负 timedelta 对象获得负的 day 字段,其他字段为正。您可以事先检查:比较适用于时间对象和时间增量对象:

>>> dt2 < dt1
True
>>> t2 < t1
True

回答by Girish

Python has pytz (http://pytz.sourceforge.net) module which can be used for arithmetic of 'time' objects. It takes care of DST offsets as well. The above page has a number of examples that illustrate the usage of pytz.

Python 有 pytz ( http://pytz.sourceforge.net) 模块,可用于“时间”对象的算术。它也负责 DST 偏移。上页有很多例子说明了pytz的用法。

回答by pkaeding

It seems that this isn't supported, since there wouldn't be a good way to deal with overflows in datetime.time. I know this isn't an answer directly, but maybe someone with more python experience than me can take this a little further. For more info, see this: http://bugs.python.org/issue3250

这似乎不受支持,因为在 datetime.time 中没有处理溢出的好方法。我知道这不是直接的答案,但也许比我有更多 Python 经验的人可以更进一步。有关更多信息,请参阅:http: //bugs.python.org/issue3250

回答by Joshua

Retrieve the times in milliseconds and then do the subtraction.

以毫秒为单位检索时间,然后进行减法运算。

回答by Joshua

Environment.TickCount seems to work well if you need something quick.

Environment.TickCount 如果你需要一些快速的东西,它似乎工作得很好。

int start = Environment.TickCount

int start = Environment.TickCount

...DoSomething()

...做一点事()

int elapsedtime = Environment.TickCount - start

int elapsedtime = Environment.TickCount - 开始

Jon

乔恩