Welcome, guest | Sign In | My Account | Store | Cart

Python 2.x doesn't support dividing a timedelta by a float (only works with int). This function adds support for that.

Python, 30 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import datetime

def divide_timedelta(td, divisor):
    """Python 2.x timedelta doesn't support division by float, this function does.

    >>> td = datetime.timedelta(10, 100, 1000)
    >>> divide_timedelta(td, 2) == td / 2
    True
    >>> divide_timedelta(td, 100) == td / 100
    True
    >>> divide_timedelta(td, 0.5)
    datetime.timedelta(20, 200, 2000)
    >>> divide_timedelta(td, 0.3)
    datetime.timedelta(33, 29133, 336667)
    >>> divide_timedelta(td, 2.5)
    datetime.timedelta(4, 40, 400)
    >>> td / 0.5
    Traceback (most recent call last):
      ...
    TypeError: unsupported operand type(s) for /: 'datetime.timedelta' and 'float'

    """
    # timedelta.total_seconds() is new in Python version 2.7, so don't use it
    total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6) / 1e6
    divided_seconds = total_seconds / float(divisor)
    return datetime.timedelta(seconds=divided_seconds)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

1 comment

Gabriel Genellina 11 years, 11 months ago  # | flag

One should note that using float as an intermediate representation may yield incorrect results due to precision loss. This is relevant because all other operations on timedelta objects are exact.

py> td = datetime.timedelta(397685, 0, 5)
py> divide_timedelta(td, 5)
datetime.timedelta(79537, 0, 2) # wrong
py> td /5
datetime.timedelta(79537, 0, 1) # good