如何在Python中获得timedelta的总和?

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

How to get the sum of timedelta in Python?

pythondatetimetimedelta

提问by user469652

Python: How to get the sum of timedelta?

Python:如何获得timedelta的总和?

Eg. I just got a lot of timedelta object, and now I want the sum. That's it!

例如。我刚刚得到了很多 timedelta 对象,现在我想要总和。就是这样!

采纳答案by pyfunc

datetime combine method allows you to combine time with a delta

日期时间组合方法允许您将时间与增量组合

datetime.combine(date.today(), time()) + timedelta(hours=2)

timedelta can be combined using usual '+' operator

timedelta 可以使用通常的“+”运算符组合

>>> timedelta(hours=3) 
datetime.timedelta(0, 10800)
>>> timedelta(hours=2)
datetime.timedelta(0, 7200)
>>>
>>> timedelta(hours=3) + timedelta(hours=2)
datetime.timedelta(0, 18000)
>>> 

You can read the datetime module docs and a very good simple introduction at

您可以阅读 datetime 模块文档和一个非常好的简单介绍

回答by Mark Byers

To add timedeltas you can use the builtin operator +:

要添加 timedeltas,您可以使用内置运算符+

result = timedelta1 + timedelta2

To add a lot of timedeltas you can use sum:

要添加大量时间增量,您可以使用 sum:

result = sum(timedeltas, datetime.timedelta())

Or reduce:

或减少:

import operator
result = reduce(operator.add, timedeltas)

回答by af.

As this "just works", I assume this question lacks some detail...

由于这“有效”,我认为这个问题缺乏一些细节......

Just like this:

像这样:

>>> import datetime
>>> datetime.timedelta(seconds=10) + datetime.timedelta(hours=5)
datetime.timedelta(0, 18010)

回答by Doug F

I am pretty sure that by "sum" he means that he wants the value of the sum in a primitive type (eg integer) rather than a datetime object.

我很确定“总和”的意思是他想要原始类型(例如整数)而不是日期时间对象的总和值。

Note that you can always use the dirfunction to reflect on an object, returning a list of its methods and attributes.

请注意,您始终可以使用该dir函数来反映对象,返回其方法和属性的列表。

>>> import datetime
>>> time_sum=datetime.timedelta(seconds=10) + datetime.timedelta(hours=5)
>>> time_sum
datetime.timedelta(0, 18010)
>>> dir(time_sum)
['__abs__', '__add__', '__class__', '__delattr__', '__div__', '__doc__', '__eq__', '__floordiv__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__radd__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmul__', '__rsub__', '__setattr__', '__str__', '__sub__', 'days', 'max', 'microseconds', 'min', 'resolution', 'seconds']

So in this case, it looks like we probably want seconds.

所以在这种情况下,看起来我们可能需要几秒钟。

>>> time_sum.seconds
18010

Which looks right to me:

这对我来说是正确的:

>>> 5*60*60 + 10
18010

回答by RobM

(Edit:Assuming that by "sum timedelta" you mean "convert a timedelta to int seconds".)

编辑:假设“sum timedelta”是指“将 timedelta 转换为 int 秒”。)

This is very inefficient, but it is simple:

这是非常低效的,但很简单:

int((datetime.datetime.fromtimestamp(0) + myTimeDelta).strftime("%s"))

This converts a Unix timestamp (0) into a datetime object, adds your delta to it, and then converts back to a Unix time. Since Unix time counts seconds, this is the number of seconds your timedelta represented.

这会将 Unix 时间戳 ( 0) 转换为日期时间对象,将您的增量添加到其中,然后转换回 Unix 时间。由于 Unix 时间计算秒数,这是您的 timedelta 表示的秒数。