You can use parser.parse
method of dateutil
package.
For example for statckoverflow:
import feedparser
from dateutil import parser, tz
url = 'http://stackoverflow.com/feeds/tag/python'
feed = feedparser.parse(url)
published = feed.entries[0].published
dt = parser.parse(published)
print(published)
print(dt) # that is timezone aware
print(dt.utcoffset()) # time zone of time
print(dt.astimezone(tz.tzutc())) # that is timezone aware as UTC
2012-11-28T19:07:32Z
2012-11-28 19:07:32+00:00
0:00:00
2012-11-28 19:07:32+00:00
You can see that published
ends with Z
, it means timezone is in UTC:
Looks at History of Date Formats for it in feedparser:
Atom 1.0 states that all date elements “MUST conform to the date-time
production in RFC 3339.
In addition, an uppercase T character MUST be used to separate date and time,
and an uppercase Z character MUST be present in the absence of
a numeric time zone offset.”
And for another example:
import feedparser
from dateutil import parser, tz
url = 'http://omidraha.com/rss/'
feed = feedparser.parse(url)
published = feed.entries[0].published
dt = parser.parse(published)
print(published)
print(dt) # that is timezone aware
print(dt.utcoffset()) # time zone of time
print(dt.astimezone(tz.tzutc())) # that is timezone aware as UTC
Thu, 26 Dec 2013 14:24:04 +0330
2013-12-26 14:24:04+03:30
3:30:00
2013-12-26 10:54:04+00:00
But also it depends on the format of time data that received and type of feed.