[Python-Dev] Issue5434: datetime.monthdelta
jess.austin at gmail.com
Fri Apr 17 03:01:22 CEST 2009
On Thu, Apr 16, 2009 at 7:18 PM, <skip at pobox.com> wrote:
> >> I have this funny feeling that arithmetic using monthdelta wouldn't
> >> always be intuitive.
> Jess> I think that's true, especially since these calculations are not
> Jess> necessarily invertible:
> >>> date(2008, 1, 30) + monthdelta(1)
> datetime.date(2008, 2, 29)
> >>> date(2008, 2, 29) - monthdelta(1)
> datetime.date(2008, 1, 29)
> Jess> It could be that non-intuitivity is inherent in the problem of
> Jess> dealing with dates and months.
> To which I would respond:
> >>> import this
> The Zen of Python, by Tim Peters
> In the face of ambiguity, refuse the temptation to guess.
> There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.
> From the discussion I've seen so far, it's not clear that there is one
> obvious way to do it, and the ambiguity of the problem forces people to
> My recommendations after letting it roll around in the back of my brain for
> the day:
> * I think it would be best to leave the definition of monthdelta up to
> individual users. That is, add nothing to the datetime module and let
> them write a function which does what they want it to do.
> * The idea/implementation probably needs to bake on the python-ideas
> list and perhaps comp.lang.python for a bit to see if some concensus
> can be reached on reasonable functionality.
So far, all the other solutions to the problem that have been
mentioned are easily supported in current python.
Raise an exception when a calculation results in an invalid date:
>>> dt = date(2008, 1, 31)
>>> dt.replace(month=dt.month + 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: day is out of range for month
Add exactly 30 days to a date:
>>> dt + timedelta(30)
datetime.date(2008, 3, 1)
These operations are useful in particular contexts. What I've
submitted is also useful, and currently isn't easy in core,
batteries-included python. While I would consider the foregoing
interpretation of the Zen to be backwards (this doesn't add another
way to do something that's already possible, it makes possible
something that currently encourages one to pull her hair out), I
suppose it doesn't matter. If adding a class and a function to a
module will require extended advocacy on -ideas and c.l.p, I'm
probably not the person for the job.
If, on the other hand, one of the committers wants to toss this in at
some point, whether now or 3 versions down the road, the patch is up
at bugs.python.org (and I'm happy to make any suggested
modifications). I'm glad to have written this; I learned a bit about
CPython internals and scraped a layer of rust off my C skills. I will
go ahead and backport the python-coded version to 2.3. I'll continue
this conversation with whomever for however long, but I suspect this
topic will soon have worn out its welcome on python-dev.
More information about the Python-Dev