[Python-ideas] Parsing ISO 8601 with the standard library
Tin Tvrtković
tinchester at gmail.com
Tue Mar 11 19:46:07 CET 2014
Hello,
it occured to me today that it's not currently possible to parse ISO
8601 dates using the datetime.datetime.strftime function (I was parsing
datetimes generated by Postgres). The problem is in the semicolon in the
time zone offset.
'1997-07-16T19:20:30.45+01:00' is a valid ISO 8601 date and time
representation, the likes of which can be generated by the datetime
module itself (the isoformat method). The %z strftime directive only
recognizes offsets without the semicolon. The Python docs also direct
users to inspect the platform strftime documentation; on my system the
man page clearly states %z is "... The +hhmm or -hhmm numeric timezone",
again, no semicolon support.
Googling around, most common suggestions are to use a third party
library (such as dateutil), so either this functionality really doesn't
exist in a simple form in the datetime module, or is really
undiscoverable. It seems to me ISO 8601 is significant enough (since
there are even special methods for it in the datetime module) for it to
be parsable, in a non-complex way, by the standard library.
I guess it's interesting to point out a new flag was added to Java's
SimpleDateFormat's date and time patterns in Java 7 - the X directive
stands for "ISO 8601 time zone" (-08; -0800; -08:00, Z). I've worked
with this so I happen to know it off the top of my head.
Thanks in advance for comments.
Tin
More information about the Python-ideas
mailing list