views:

141

answers:

3

I iterate a RSS feed like so where _file is the feed

d = feedparser.parse(_file)
for element in d.entries: 
    print repr(element.date)

The date output comes out like so

u'Thu, 16 Jul 2009 15:18:22 EDT'

I cant seem to understand how to actually quantify the above date output so I can use it to limit feed elements. I So what I am asking is how can I get a actual time out of this, so I can say if greater then 7 days old, skip this element.

+1  A: 

one way

>>> import time
>>> t=time.strptime("Thu, 16 Jul 2009 15:18:22 EDT","%a, %d %b %Y %H:%M:%S %Z")
>>> sevendays=86400*7
>>> current=time.strftime ("%s",time.localtime())
>>> if int(current) - time.mktime(t) > sevendays:
        print "more than 7 days"

you can also see the datetime module and make use of timedelta() for date calculations.

ghostdog74
A: 

If you install the dateutil module:

import dateutil.parser as dp
import dateutil.tz as dtz
import datetime

date_string=u'Thu, 16 Jul 2009 15:18:22 EDT'
adatetime=dp.parse(date_string)
print(adatetime) 
# 2009-07-16 15:18:22-04:00

now=datetime.datetime.now(dtz.tzlocal())
print(now)
# 2010-02-04 23:35:52.428766-05:00

aweekago=now-datetime.timedelta(days=7)
print(aweekago)
# 2010-01-28 23:35:52.428766-05:00

if adatetime<aweekago:
    print('old news')

If you are using Ubuntu, dateutil is provided by the python-dateutil package.

unutbu
+1  A: 

feedparser is supposed to give you a struct_time object from Python's time module. I'm guessing it doesn't recognize that date format and so is giving you the raw string.

See here on how to add support for parsing malformed timestamps:

http://www.feedparser.org/docs/date-parsing.html

If you manage to get it to give you the struct_time, you can read more about that here:

http://docs.python.org/library/time.html#time.struct_time

struct_time objects have everything you need. They have these members:

time.struct_time(tm_year=2010, tm_mon=2, tm_mday=4, tm_hour=23, tm_min=44, tm_sec=19, tm_wday=3, tm_yday=35, tm_isdst=0)

I generally convert the structs to seconds, like this:

import time
import calendar

struct = time.localtime()
seconds = calendar.timegm(struct)

Then you can just do regular math to see how many seconds have elapsed, or use the datetime module to do timedeltas.

FogleBird