Rhodri James wrote: > On Fri, 09 Oct 2009 13:39:43 +0100, Tim Chase > <python.l...@tim.thechases.com> wrote: > >>> Month arithmetic is a bit of a mess, since it's not clear how >>> to map e.g. Jan 31 + one month. >> >> "Jan 31 + one month" usually means "add one to the month value and >> then keep backing off the day if you get an exception making the >> date", so you'd get Feb 31, exception, Feb 30, exception, Feb 29, >> possibly an exception, and possibly/finally Feb 28th. This makes >> pretty intuitive sense to most folks and is usually what's meant. >> >> I've found that issues and confusion stem more from the >> non-commutative reality that "Jan 31 + (1 month) + (-1 month) != Jan >> 31 + (-1 month) + (1 month)" or the non-associative "Jan 31 + (1 month >> + 1 month) != (Jan 31 + 1 month) + 1 month" :-/ > > I'd hazard a guess that what we're actually seeing is people mentally > rebasing their indices, i.e. counting from the end of the month rather > than the start, which makes "the last day of January" and "January 31" > not the same thing really. Unfortunately we're very fuzzy about when > we do things like this, which makes it hard on a poor programmer.
Ah, for that we have RelativeDateTime: >>> # "next month, last day of the month" >>> mx.DateTime.DateTime(2009, 1, 31) + mx.DateTime.RelativeDateTime(months=+1, >>> day=-1) <mx.DateTime.DateTime object for '2009-02-28 00:00:00.00' at 2ba43f95c088> -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Oct 12 2009) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list