[Python-ideas] Pythonic Dates, Times, and Deltas

Alexander Belopolsky alexander.belopolsky at gmail.com
Wed Oct 13 23:17:36 CEST 2010


On Wed, Oct 13, 2010 at 4:04 PM, Daniel G. Taylor
<dan at programmer-art.org> wrote:
> ... and have run into several annoyances with the
> built-in datetime, date, time, timedelta, etc classes, even when adding in
> relativedelta. They are awkward, non-intuitive and not at all Pythonic to
> me.

There seems to be no shortage of blogosphere rants about how awkward
python datetime module is, but once patches are posted on the tracker
to improve it, nobody seems to be interested in reviewing them.  I has
been suggested that C implementation presented a high barrier to entry
for people to get involved in datetime module development.  This was
one of the reasons I pushed for including a pure python equivalent in
3.2.   Unfortunately, getting datetime.py into SVN tree was not enough
to spark new interest in improving the module.  Maybe this will change
with datetime.py making it into a released version.

..
> My original post about it was here:
>
> http://programmer-art.org/articles/programming/pythonic-date
>

This post is severely lacking in detail, so I cannot tell how your
library solves your announced problems, but most of them seem to be
easy with datetime:

* Make it easy to make a Date from anything - a timestamp, date,
datetime, tuple, etc.

>>> from datetime import *
>>> datetime.utcfromtimestamp(0)
datetime.datetime(1970, 1, 1, 0, 0)
>>> datetime.utcfromtimestamp(0).date()
datetime.date(1970, 1, 1)

* Make it easy to turn a Date into anything

datetime.timetuple() will convert datetime to a tuple.  There is an
open ticket to simplify datetime to timestamp conversion

http://bugs.python.org/issue2736

but it is already easy enough:

>>> (datetime.now() - datetime(1970,1,1)).total_seconds()
1286989863.82536

* Make it easy and pythonic to add/subtract one or more days, weeks,
months, or years

monthdelta addition was discussed at http://bugs.python.org/issue5434,
but did not get enough interest.  The rest seems to be easy enough
with timedetla.

* Make it easy to get a tuple of the start and end of the month

Why would you want this?  Start of the month is easy: just date(year,
month, 1).  End of the month is often unnecessary because it is more
pythonic to work with semi-open ranges and use first of the next month
instead.



More information about the Python-ideas mailing list