Does anyone know of a Python module that will convert an RFC 822 timestamp into a human readable format (like Twitter does) in Python?
I found parsedatetime, which seems to do the 开发者_运维百科reverse.
In python, you can use rfc822 module. This module provides the parsedate method.
Attempts to parse a date according to the rules in RFC 2822.
However, this module is deprecated.
Deprecated since version 2.3: The email package should be used in preference to the rfc822 module. This module is present only to maintain backward compatibility, and has been removed in 3.0.
According to this comment, it's better to use the parsedate method from the email.utils module.
email.utils.parsedate(date)
EDIT:
Example code :
import email.utils
from time import mktime
from datetime import datetime
example_date = "Sat, 02 Mar 2011 15:00:00"
date_parsed = email.utils.parsedate(example_date)
dt = datetime.fromtimestamp(mktime(date_parsed))
today = datetime.today()
diff_date = today - dt # timedelta object
print "%s days, %s hours ago" \
% (diff_date.days, diff_date.seconds / 3600)
Output (for now) :
31 days, 2 hours ago
datetime.strptime
will turn the times stamp into a datetime object which you can format with datetime.strftime
http://docs.python.org/library/datetime.html#strftime-strptime-behavior
See
http://docs.python.org/library/rfc822.html#rfc822.parsedate
精彩评论