views:

279

answers:

2
  1. I am reading a Facebook updates feed using the python library 'feedparser'.
  2. I loop through the collection of entries in my Django templates, and display the results.
  3. The updated field is returned in a big long string, of some format I am unfamiliar with.

    Tue, 01 Dec 2009 23:55:52 +0000

How can I...

A) Use a Django filter to clean the date time in the for loop on the template.

...or...

B) Parse the date and format the updated date in the view, esentially cleaning the date in the collection of entries before it is iterated over in the view.

NOTE: I have tried both approaches. Django's date filter does't recognize it, and the iso8601 library I tried to parse the string didn't either.

Anybody have any experience with this? Thanks for your help!

UPDATE:

Using the updated_parsed value from feedparser in a Django template didn't work so well. But a Django snippet of a filter for this very thing already exists!**

Django Snippet: http://www.djangosnippets.org/snippets/1595/

+3  A: 

Use entries[i].updated_parsed instead of entries[i].updated, and feedparser will return a parsed 9-tuple for you. (Documentation)

Then build a datetime object and pass it to Django or format to a string by yourself.

There is a similar question here.

Iamamac
I like your solution but passing the tuple into Django's filter doesn't seem to work. I just get blank when I use the date filter. Maybe there's a better way to format it.
Ryan Montgomery
I updated the post with the link to the Django filter for feedparser dates.
Ryan Montgomery
A: 

This worked but wasn't what my final solution ended up becoming.

This solution iterates over the feed entries collection I get back from Facebook. I then parse the datetime and set the updated property to that new datetime. (Also, ignoring the +0000)

for entry in feed.entries:
        entry.updated = datetime.strptime(entry.updated, "%a, %d %b %Y %H:%M:%S +0000")

The entries collection is returned to the template which I can now use the Django 'date' filter to format the date.

Ryan Montgomery
This worked fine, but the accepted solution leverages the feedparser framework which is +1 better.
Ryan Montgomery