[Python-Dev] Issue5434: datetime.monthdelta

Jess Austin jess.austin at gmail.com
Fri Apr 17 01:02:11 CEST 2009


Antoine Pitrou <solipsis at pitrou.net> wrote:
> Jess Austin <jess.austin <at> gmail.com> writes:
>>
>> What other behavior options besides "last-valid-day-of-the-month"
>> would you like to see?
>
> IMHO, the question is rather what the use case is for the behaviour you are
> proposing. In which kind of situation is it acceptable to turn 31/2 silently
> into 29/2?

I have worked in utility/telecom billing, and needed to examine large
numbers of invoice dates, fulfillment dates, disconnection dates,
payment dates, collection event dates, etc.  There would often be
particular rules for the relationships among these dates, and since
many companies generate invoices every day of the month, you couldn't
rely on rules like "this always happens on the 5th".  Here is an
example (modified) from the doc page.  We want to find missing
invoices:

>>> invoices = {123: [date(2008, 1, 31),
...                   date(2008, 2, 29),
...                   date(2008, 3, 31),
...                   date(2008, 4, 30),
...                   date(2008, 5, 31),
...                   date(2008, 6, 30),
...                   date(2008, 7, 31),
...                   date(2008, 12, 31)],
...             456: [date(2008, 1, 1),
...                   date(2008, 5, 1),
...                   date(2008, 6, 1),
...                   date(2008, 7, 1),
...                   date(2008, 8, 1),
...                   date(2008, 11, 1),
...                   date(2008, 12, 1)]}
>>> for account, dates in invoices.items():
...     a = dates[0]
...     for b in dates[1:]:
...         if b - monthdelta(1) > a:
...             print('account', account, 'missing between', a, 'and', b)
...         a = b
...
account 456 missing between 2008-01-01 and 2008-05-01
account 456 missing between 2008-08-01 and 2008-11-01
account 123 missing between 2008-07-31 and 2008-12-31


In general, sometimes we care more about the number of months that
separate dates than we do about the exact dates themselves.  This is
perhaps not the most common situation for date calculations, but it
does come up for some of us.  I tired of writing one-off solutions
that would fail in unexpected corner cases, so I created this patch.
Paul Moore has also described his favorite use-case for this
functionality.

cheers,
Jess


More information about the Python-Dev mailing list