I’m parsing an atom feed. There is an updated tag in the feed. It’s value is 2012-05-20T02:24:56Z. I insert this information in a table in this manner.
cursor.execute('Insert into Timing (LastUpdatedTime) Values(?)',
(testFeed.feed.updated,))
After that I obtain the value from table and convert it into datetime in this way
cursor.execute("Select LastUpdatedTime from Timing")
lastUpdatedTime=datetime.strptime(list(cursor.fetchone())[0],
'%Y-%m-%dT%H:%M:%SZ').isoformat()
The value of lastUpdatedTime is 2012-05-20T02:24:56
Now when I compare the value of testFeed.feed.updated with lastUpdatedTime to check if the feed had any new entries my comparison is giving output which I fail to comprehend.
print testFeed.feed.updated == lastUpdatedTime
This returns false
print testFeed.feed.updated > lastUpdatedTime
This returns true.
Values of variables when comparing:
testFeed.feed.updated=2012-05-20T02:24:56Z
lastUpdatedTime=2012-05-20T02:24:56
The column in database is of type text.
type of testFeed.feed.updated is unicode
type of lastUpdatedTime is string
Using python 2.7.2, working on Ubuntu 11.10
It works after converting the unicode object to datetime
currentFeedTime=datetime.strptime(str(testfeed.feed.updated),'%Y-%m-%dT%H:%M:%SZ').isoformat() and then to do the comparison with previous time.
The behavior you’re seeing makes sense to me, since
testFeed.feed.updatedhas an extra ‘Z’ at the end.Having said that, if you’re doing date/time comparison, I think you’re better off converting both dates to
datetime.datetime, so something along the lines of:For
lastUpdatedTimeAnd instead of
testFeed.feed.updated:And then to compare the two: