[Python-Dev] Issue5434: datetime.monthdelta (original) (raw)

Paul Moore p.f.moore at gmail.com
Fri Apr 17 00:17:07 CEST 2009


2009/4/16 Robert Kern <robert.kern at gmail.com>:

On 2009-04-16 13:42, Paul Moore wrote:

2009/4/16 Ned Deily<nad at acm.org>:

In article <b8ad139e0904152318p5473cbe5yb5f55a19894cc834 at mail.gmail.com>,  Jess Austin<jess.austin at gmail.com>  wrote:

I'm new to python core development, and I've been advised to write to python-dev concerning a feature/patch I've placed at http://bugs.python.org/issue5434, with Rietveld at http://codereview.appspot.com/25079. Without having looked at the code, I wonder whether you've looked at python-dateutil.   I believe its relativedelta type does what you propose, plus much more, and it has the advantage of being widely used and tested. The key thing missing (I believe) from dateutil is any equivalent of monthmod. Hmm, it might be possible via relativedelta(d1,d2), but it's not clear to me from the documentation precisely what attributes/methods of a relativedelta object are valid for getting data out of it. I thought the examples were quite clear. relativedelta() has an alternate constructor precisely suited to these calculations but is general and handles more than just months. from dateutil.relativedelta import * dt = relativedelta(months=1) dt relativedelta(months=+1) from datetime import datetime datetime(2009, 1, 15) + dt datetime.datetime(2009, 2, 15, 0, 0) datetime(2009, 1, 31) + dt datetime.datetime(2009, 2, 28, 0, 0) dt.months 1 datetime(2009, 1, 31) + relativedelta(years=-1) datetime.datetime(2008, 1, 31, 0, 0)

Yes, but given

r = relativedelta(d1, d2)

how do I determine the number of months between d1 and d2, and the "remainder" - what monthmod gives me. From the code, r.months looks like it works, but it's not documented, and I'm not 100% sure if it's always computed.

The use case I'm thinking of is converting the difference between 2 dates into "3 years, 2 months, 5 days" or whatever. I've got an application which needs to get this right for one of the dates being 29th Feb, so I really get to exercise the corner cases :-)

Paul



More information about the Python-Dev mailing list