Created
December 30, 2020 18:20
-
-
Save rfinnie/53df5db7ecd8b2be4bd3ecbdeaa84e39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def http_date(dt=None): | |
"""Return an RFC 7231 HTTP date formatted string | |
Input may be an epoch timestamp, or datetime object. | |
If a naive datetime object, it must be in GMT already. | |
If not specified, now is assumed. | |
""" | |
if dt is None: | |
dt = datetime.datetime.utcnow() | |
if isinstance(dt, (int, float)): | |
dt = datetime.datetime.utcfromtimestamp(dt) | |
if dt.tzinfo is not None: | |
dt = dt.astimezone(datetime.timezone.utc).replace(tzinfo=None) | |
weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()] | |
month = [ | |
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", | |
"Nov", "Dec" | |
][dt.month - 1] | |
return "{}, {:02d} {} {:04d} {:02d}:{:02d}:{:02d} GMT".format( | |
weekday, dt.day, month, dt.year, dt.hour, dt.minute, dt.second | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment