def format_http_datetime(stamp):
""" Formats datetime to a string following rfc1123 pattern.
>>> now = datetime(2011, 9, 19, 10, 45, 30, 0, UTC)
>>> format_http_datetime(now)
'Mon, 19 Sep 2011 10:45:30 GMT'
if timezone is not set in datetime instance the ``stamp``
is assumed to be in UTC (``datetime.utcnow``).
>>> now = datetime(2011, 9, 19, 10, 45, 30, 0)
>>> format_http_datetime(now)
'Mon, 19 Sep 2011 10:45:30 GMT'
>>> now = datetime.utcnow()
>>> assert format_http_datetime(now)
if ``stamp`` is a string just return it
>>> format_http_datetime('x')
'x'
>>> format_http_datetime(100) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: ...
"""
if isinstance(stamp, datetime):
if stamp.tzinfo:
stamp = stamp.astimezone(UTC).timetuple()
else:
stamp = localtime(mktime(stamp.timetuple()))
elif isinstance(stamp, str):
return stamp
else:
raise TypeError('Expecting type ``datetime.datetime``.')
year, month, day, hh, mm, ss, wd, y, z = stamp
return "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
WEEKDAYS[wd], day, MONTHS[month], year, hh, mm, ss
)
评论列表
文章目录