The datetime
module is definitely easier to use, but, if you insist, you can do it with the time
module instead. I.e.:
>>> import time
>>> fmt = '%a %b %d %H:%M:%S +0000 %Y'
>>> time.strftime(fmt)
'Thu Oct 15 07:54:07 +0000 2009'
>>> createdat = 'Thu Oct 15 02:30:30 +0000 2009'
>>> createdtim = time.strptime(createdat, fmt)
>>> hoursago = (time.time() - time.mktime(createdtim)) / 3600
>>> print hoursago
5.42057559947
One reason the time
module is so pesky to use is that it uses two different ways to represent time -- one is a 9-tuple (and that's what you get from strptime
), and one is a float "seconds since the epoch" (and that's what you need to do differences); the mktime
function translates the former to the latter. Difference in hours is clearly 1/3600 of the difference in seconds -- you'll have to decide how to display a typically-fractionary "number of hours ago", of course (hour and fraction with some digits, or, round to closest integer number of hours, or what else).
The literal '+0000' in your format, for strptime
, means you expect and ignore those literal characters at that spot in your "createdat" string (normally you'd have a timezone offset specifier there). If that's indeed what you want, then you have the right format string!