sdispater / pendulum

Python datetimes made easy
https://pendulum.eustace.io
MIT License
6.25k stars 387 forks source link

Interval: store months separately #23

Closed flokli closed 5 years ago

flokli commented 8 years ago

Currently, intervals are broken down into microseconds, seconds and days in Pendulum.

However, this might be not precise enough if you calculate with months. Adding months to a Pendulum instance is already supported by add(months=n) (with precise calculations), so we should be able to store months in Interval instances too (and calculate with them).

This would also allow a loss-free representation of the ISO8601 duration format in a Duration instance, and conversions from and to it ;-)

Of course, there might be some corner cases that need to be considered - like what happens when 1 month is compared with 30 days.

I'd suggest to peek at the PostgreSQL interval implementation (with added microseconds):

Internally interval values are stored as months, days, and seconds. This is done because the number of days in a month varies, and a day can have 23 or 25 hours if a daylight savings time adjustment is involved. 
sdispater commented 8 years ago

It would be useful, no doubt. However, the current implementation (inheritance from the timedelta class) prevent it from being trivial. And, furthermore, if you have an interval with 1 month, how many seconds is it (total_seconds() method)? The fact is you can't know for certain.

So, for now, I won't integrate it in a near future but I will keep this in mind :-)

flokli commented 8 years ago

Sure, I totally agree its not trivial. But great to hear you'll keep this in mind :+1:

sdispater commented 5 years ago

Closing this since it's now supported.