YADTR (Yet Another DateTime Rant)

Roy Smith roy at panix.com
Wed Mar 26 16:04:53 CET 2014

On Wednesday, March 26, 2014 9:37:06 AM UTC-4, Skip Montanaro wrote:

> The problem gets more challenging once you get into magnitudes > one
> day:
> >>> e = datetime.timedelta(days=-4, seconds=3605)
> >>> e
> datetime.timedelta(-4, 3605)
> >>> print e
> -4 days, 1:00:05
> Hmmm... It's printing just what we said, negative four days, positive
> one hour, five minutes. Let's try the trick from above:

No, what you said was "negative four days, positive 3605 seconds".  Why does it make sense to normalize 3605 seconds to "1 hour, 5 seconds", but not extend that normalization to the days portion?

> Complicating things are that timedelta objects are normalized internally so that
> the seconds field is always non-negative

The whole idea of datatypes is to hide the internal representation.  If it's convenient on your platform, you can store timedeltas internally as fempto-years.  That should not affect how it prints.

More information about the Python-list mailing list