[Baypiggies] HTTP date format

Ian Zimmerman itz at buug.org
Wed Feb 17 16:28:58 EST 2016


On 2016-02-17 20:58 +0000, Alex Young wrote:

> datetime(1994, 11, 6, 8, 49, 37, 0, GMT()).strftime('%a, %d %b %Y %H:%M:%S
> %Z')

Same trouble as always: I ask a question after thinking hard about a
problem and seeing the difficulties, and I assume others see the
difficulties as well :-(

Here, the main difficulty (the one which made me stop hacking and ask)
is that strftime is locale dependent, while the RFC demands fixed
English spelling.

In the meantime I came up with this pearl:

def rfc7231_of_timestamp(timestamp):
    real_loc = locale.getlocale(locale.LC_ALL)
    locale.setlocale(locale.LC_ALL, 'C')
    try:
        gmtuple = time.gmtime(timestamp)
        return time.strftime('%a, %d %b %Y %H:%M:%S', gmtuple) + ' GMT' 
    finally:
        locale.setlocale(locale.LC_ALL, real_loc)

Do you think it has a chance in hell of working?  Note: portability to
Windows is not important, portability to MacOS only mildly interesting.

-- 
Please *no* private copies of mailing list or newsgroup messages.
Rule 420: All persons more than eight miles high to leave the court.


More information about the Baypiggies mailing list