Parsing ISO date/time strings - where did the parser go?

Dave Angel d at davea.name
Thu Sep 6 15:54:15 EDT 2012


On 09/06/2012 03:27 PM, John Nagle wrote:
> In Python 2.7:
>
>    I want to parse standard ISO date/time strings such as
>
> 	2012-09-09T18:00:00-07:00
>
> into Python "datetime" objects.  The "datetime" object offers
> an output method , datetimeobj.isoformat(), but not an input
> parser.  There ought to be
>
> 	classmethod datetime.fromisoformat(s)
>
> but there isn't.  I'd like to avoid adding a dependency on
> a third party module like "dateutil".
>
> The "Working with time" section of the Python wiki is so
> ancient it predates "datetime", and says so.
>
> There's an iso8601 module on PyPi, but it's abandoned; it hasn't been
> updated since 2007 and has many outstanding issues.
>
> There are mentions of "xml.utils.iso8601.parse" in
> various places, but the "xml" module that comes
> with Python 2.7 doesn't have xml.utils.
>
> http://www.seehuhn.de/pages/pdate
> says:
>
> "Unfortunately there is no easy way to parse full ISO 8601 dates using
> the Python standard library."
>
> It looks like this was taken out of "xml" at some point,
> but not moved into "datetime".
>

For working with datetime, see
    http://docs.python.org/library/datetime.html#datetime.datetime

and look up  datetime.strptime()

Likewise for generalized output, check out  datetime.strftime().





-- 

DaveA




More information about the Python-list mailing list