How to get the sum of timedelta in Python?

前端 未结 6 1937
时光说笑
时光说笑 2020-12-06 09:47

Python: How to get the sum of timedelta?

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

相关标签:
6条回答
  • 2020-12-06 09:52

    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(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

    • http://www.doughellmann.com/PyMOTW/datetime/
    0 讨论(0)
  • 2020-12-06 09:52

    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)
    
    0 讨论(0)
  • 2020-12-06 09:56

    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 dir function to reflect on an object, returning a list of its methods and attributes.

    >>> 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
    
    0 讨论(0)
  • 2020-12-06 09:56

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

    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.

    0 讨论(0)
  • 2020-12-06 09:56

    If you have a list of timedelta objects, you could try:

    datetime.timedelta(seconds=sum(td.total_seconds() for td in list_of_deltas))

    0 讨论(0)
  • 2020-12-06 10:00

    To add timedeltas you can use the builtin operator +:

    result = timedelta1 + timedelta2
    

    To add a lot of timedeltas you can use sum:

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

    Or reduce:

    import operator
    result = reduce(operator.add, timedeltas)
    
    0 讨论(0)
提交回复
热议问题