[Python-ideas] Date/time literals

Chris Rebert pyideas at rebertia.com
Sun May 30 03:49:02 CEST 2010


On Sat, May 29, 2010 at 6:05 PM, Marcos Bonci <marcos.bonci at gmail.com> wrote:
> Hi!
> I've been working a lot with date/time variables lately, and maybe it's just
> me, and
> maybe I just don't get it yet, but it sure doesn't feel like there's
> one-and-only-one
> elegant, obvious way to work with them. Feels like working with strings in
> C.

Reading the rest of your post, it sounds like you're unaware of the
fairly nice API of the `datetime` std lib module:
http://docs.python.org/library/datetime.html

IMO, it's pretty nice to work with; although it is annoying that some
operations can only be done using the more primitive `time` or
`calendar` modules. Patches are probably welcome; perhaps I should
consider trying to write one.

> So I was thinking, have date and time literals ever been seriously
> discussed anywhere
> in these discussion lists? (If so, I apologize in advance for reviving this
> subject.)
> Is there any chance we could ever see date/time literals in Python?

Well, the datetime module does appear to already be written in C, so
that'd be one less hurdle.
I wasn't able to find a prior post on the exact subject; perhaps
someone else will.

> Perhaps as a top-level
> abstraction itself, or as a subclass of numbers.Number, or even as common
> float or int
> (by adding attributes regarding specific properties of a number's date/time
> interpretation,
> e.g. "<2010-12-31>.incr_month(2) == <2011-02-28>" -- just an example though,
> i don't really
> think this exact method would be practical).

The `datetime` module already provides nice and more typesafe
abstractions; purposefully conflating date/times with plain numbers is
not a good idea, imo.

<snip>
> Very often the "time elapsed" value
> is what we really
> want (or at least the absolute difference between two values, which is of
> course easier to find
> in this form), and besides being more compact and flexible, no functionality
> is lost.

datetime.timedelta:
http://docs.python.org/library/datetime.html#timedelta-objects

> I guess the most controversial point here would be the meaning of arithmetic
> operations
> between dates (or the very validity of such operations).

Already defined and accepted as valid:
datetime.date, "Supported operations"
http://docs.python.org/library/datetime.html#date-objects

<snip>
> So, what do you guys think? I'd love to hear others' opinions, even if it's
> just for me
> to understand what I got wrong and see the obvious way that's already there

I think the classes in the `datetime` module are the nice "missing"
API that you're looking for, and that there's not enough of a gain to
justify complicating the language by adding literals for them and
making them built-in types.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-ideas mailing list