datetime module: iso date *input* needed

Tim Peters tim.one at comcast.net
Thu Jan 30 23:30:29 EST 2003


[george young]
> I'm using the 2.3a datetime module.  The datetime.isoformat()
> works nicely:
>   >>> import datetime
>   >>> d=datetime.datetime.now()
>   >>> d
>   datetime.datetime(2003, 1, 30, 17, 48, 7, 848769)
>   >> d.isoformat()
>   '2003-01-30T17:48:07.848769'
>
> but now I need a way to make a datetime object from a date in iso format.
> I can't find a function in datetime to do this, e.g. something like:
>   d=datetime.datetime.parse_iso('2003-01-30T17:48:07.848769')

You can stop looking:  datetime doesn't support any kind of conversion from
string.  The number of bottomless pits in any datetime module is unbounded,
and Guido declared this particular pit out-of-bounds at the start so that
there was a fighting chance to get *anything* done for 2.3.

> I thought about using the time module strptime function, but it doesn't
> do fractions of a second.

As above.  You'll have to parse it yourself for now, but part of the point
of the ISO format is that it's dead easy to parse.  The only irregularities
are that the fractional-seconds and UTC offset portions are optional.  A
simple regexp can handle all variations easily -- and I never put "simple"
next to "regexp" to be cruel <wink>.






More information about the Python-list mailing list