The "beaty" of date arithmetic

Eddie Corns eddie at holyrood.ed.ac.uk
Fri Jan 24 06:41:23 EST 2003


hjwidmaier at web.de (Hans-Joachim Widmaier) writes:


>I've considered using mxDateTime, but from looking at the docs, I
>haven't found anything that seemed to make it much easier. The
>problems start as soon as you're going from fixed units like hour, day
>to somewhat fuzzier like month. What date is "T - 1 month - 1 day"?
>What date is "2000-02-29 + 1 year"? Date arithmetic *is* complex. Most
>of the ugliness IMHO stems from the tuple-list conversions and
>numerical indices. (Sure, I could have created names for them, making
>this little thing even longer.)

mxDateTime handles this and more.  Check the examples under the
RelativeDateTime section.

As for Feb 29 + 1 year:

>>> from mx.DateTime import *
>>> ld = strptime('2000-02-29','%Y-%m-%d')
>>> ld
<DateTime object for '2000-02-29 00:00:00.00' at 8129ec8>
>>> ld + RelativeDateTime (years=+1)
<DateTime object for '2001-03-01 00:00:00.00' at 815cf80>

>>> od =  strptime('2000-03-01','%Y-%m-%d')
>>> od
<DateTime object for '2000-03-01 00:00:00.00' at 8171180>
>>> od + RelativeDateTime (years=+1)
<DateTime object for '2001-03-01 00:00:00.00' at 8170d60>

Seems a reasonable interpretation to me.

I'm hard to please and I consider mxDateTime to be "good"!

Eddie




More information about the Python-list mailing list