views:

705

answers:

5

Is there an easy way to convert an RFC 3339 time into a regular Python timestamp?

I've got a script which is reading an ATOM feed and I'd like to be able to compare the timestamp of an item in the ATOM feed to the modification time of a file.

I notice from the ATOM spec, that ATOM dates include a time zone offset (Z<a number>) but, in my case, there's nothing after the Z so I guess we can assume GMT.

I suppose parse the time with a regex of some sort but I was hoping Python had a built-in way of doing it that I just haven't been able to find.

+2  A: 

http://bugs.python.org/issue5207

Looks like there isn't a built-in as of yet.

Amber
+7  A: 

No builtin, afaik.

feed.date.rfc3339 This is a Python library module with functions for converting timestamp strings in RFC 3339 format to Python time float values, and vice versa. RFC 3339 is the timestamp format used by the Atom feed syndication format.

It is BSD-licensed.

http://home.blarg.net/~steveha/pyfeed.html

(Edited so it's clear I didn't write it. :-)

Alex Brasetvik
PyFeed does exactly what I need, courtesy of the tf_from_timestamp() function in feed.date.rfc3339
Mark Biek
Also, I wrote the PyFeed (and Xe) libraries, and I hang out here on StackOverflow, so if you have any questions about it, I would be happy to answer them.
steveha
Note that PyFeed can be used to parse an Atom feed. It uses xml.dom.minidom to do the actual parsing, and then unpacks the XML tree structure into nice convenient classes. Hmm, I ought to put Xe and PyFeed up on PyPI.
steveha
@steveha Excellent, thanks for the offer. The libraries seem pretty easy to use so far but I'll remember you're here if I run into anything weird.
Mark Biek
@Alex Brasetvik, it was already clear that you were not claiming to have written it. If you were claiming credit you wouldn't have included the direct link to my web page! P.S. I was happy to see you recommending my library; thank you.
steveha
+1  A: 

http://pypi.python.org/pypi/iso8601/ seems to be able to parse iso 8601, which RFC 3339 is a subset of, maybe this could be useful, but again, not built-in.

zpon
+6  A: 

You don't include an example, but if you don't have a Z-offset or timezone, and assuming you don't want durations but just the basic time, then maybe this will suit you:

import datetime as dt
>>> dt.datetime.strptime('1985-04-12T23:20:50.52', '%Y-%m-%dT%H:%M:%S.%f')
datetime.datetime(1985, 4, 12, 23, 20, 50, 520000)

The strptime() function was added to the datetime module in Python 2.5 so some people don't yet know it's there.

Edit: The time.strptime() function has existed for a while though, and works about the same to give you a struct_time value:

>>> ts = time.strptime('1985-04-12T23:20:50.52', '%Y-%m-%dT%H:%M:%S.%f')
>>> ts
time.struct_time(tm_year=1985, tm_mon=4, tm_mday=12, tm_hour=23, tm_min=20, tm_sec=50, tm_wday=4, tm_yday=102, tm_isdst=-1)
>>> time.mktime(ts)
482210450.0
Peter Hansen
+1 for a solution using the standard library!
jathanism
`time.strptime()` existed before Python 2.5
J.F. Sebastian
+2  A: 

feedparser.py provides robust/extensible way to parse various date formats that may be encountered in real-world atom/rss feeds:

>>> from feedparser import _parse_date as parse_date
>>> parse_date('1985-04-12T23:20:50.52Z')
time.struct_time(tm_year=1985, tm_mon=4, tm_mday=12, tm_hour=23, tm_min=20,
                 tm_sec=50, tm_wday=4, tm_yday=102, tm_isdst=1)
J.F. Sebastian