[issue14262] Allow using decimals as arguments to `timedelta`

STINNER Victor report at bugs.python.org
Tue Mar 20 01:14:54 CET 2012


STINNER Victor <victor.stinner at gmail.com> added the comment:

My patch looses precision for big numbers. It's better to convert Decimal to a number of microseconds.

Wrong:

>>> value=86400*365.25*999999+1e-6; print(datetime.timedelta(seconds=value))
365249634 days, 18:00:00
>>> value=decimal.Decimal(86400*365.25*999999)+decimal.Decimal('1e-6'); print(datetime.timedelta(seconds=float(value)))
365249634 days, 18:00:00

Correct:

>>> value=decimal.Decimal(86400*365.25*999999)+decimal.Decimal('1e-6'); print(datetime.timedelta(microseconds=int(value*decimal.Decimal(10**6))))
365249634 days, 18:00:00.000001

I'm not completly conviced by the need of supporting Decimal in timedelta constructor. Why do you use Decimal if the result should be a timedelta? Why not using timedelta directly?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14262>
_______________________________________


More information about the Python-bugs-list mailing list