| Store | Cart

[Python-ideas] Parsing ISO 8601 with the standard library

From: Tin Tvrtković <tinc...@gmail.com>
Tue, 11 Mar 2014 19:46:07 +0100

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.

Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Recent Messages in this Thread
Tin Tvrtković Mar 11, 2014 06:46 pm
Guido van Rossum Mar 11, 2014 06:57 pm
Alexander Belopolsky Mar 11, 2014 07:08 pm
Guido van Rossum Mar 11, 2014 07:10 pm
Tin Tvrtković Mar 11, 2014 07:39 pm
Mark Lawrence Mar 11, 2014 07:51 pm
rand...@fastmail.us Mar 11, 2014 07:14 pm
Chris Rebert Mar 11, 2014 07:28 pm
Messages in this thread