Format date for RSS/Atom

Posted: [Source]
Tags:  date python rfc3339 timezone

While importing posts from my old Blog to Nikola, I considered generating a big atom feed, and import it using Nikola importers. I finally generated all post files by hand, but along the way I found how to correctly format DateTime object to match rfc3339 which is required for RSS or Atom feeds.

Obviously there's an rfc3339 libary which will help you get desired format, and correct date printed.

>>> from rfc3339 import rfc3339
>>> from datetime import datetime
>>>
>>> now = datetime.strptime('2013/04/17 22:22:14', '%Y/%m/%d %H:%M:%S')
>>> now
datetime.datetime(2013, 4, 17, 22, 22, 14)
>>> rfc3339(now)
'2013-04-17T22:22:14+02:00'

It might happen however, that your datetime does not have timezone info on this, and you're working on system, which has different timezone set. In this case, you'll also need pytz library and create required timezone yourself, and localize the date:

>>> import pytz
>>> warsaw = pytz.timezone('Europe/Warsaw')
>>> warsaw.localize(now)
datetime.datetime(2013, 4, 17, 22, 22, 14, tzinfo=<DstTzInfo 'Europe/Warsaw' CEST+2:00:00 DST>)
>>> rfc3339(warsaw.localize(now))
'2013-04-17T22:22:14+01:00'

Of course, there's also the issue to find out the timezone the date was originally stored with if we retrieve it from database.

Comments powered by Disqus