Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Parse ISO 8601 date and time from a string (using the standard modules)

I want to parse the date for entries given by SVN:

svn list --xml https://subversion:8765/svn/Foo/tags/

If I am not mistaken it is given using the ISO 8601 standard. An example is:

dateString = "2012-02-14T11:22:34.593750Z"

I am using Python 2.7 and am looking for the best way to process is using the standard modules only. I think I´ll parse it this way using a regular expression: ^--- is a match group and A indicates that I will assume that they are always there (which might be a weakness)

dateString = "2012-02-14T11:22:34.593750Z"
              ^--- ^- ^-A^- ^- ^--------A
                        |               |
                        |  I think it always gives the date
                        |  as UTC, which Z means in ISO 8601.
                        |
                 Just a separator

After parsing it I´ll simply create a datetime from it.

Is there a better way using Python 2.7 with only the standard modules?

like image 329
Deleted Avatar asked Feb 22 '23 08:02

Deleted


1 Answers

No need for a regexp, use datetime.datetime.strptime() instead.

like image 81
Kimvais Avatar answered May 09 '23 04:05

Kimvais