RFC 1123 Date Representation in Python?

前端 未结 5 1348
南笙
南笙 2020-12-07 12:28

Is there a fairly easy way to convert a datetime object into an RFC 1123 (HTTP/1.1) date/time string, i.e. a string with the format

Sun, 06 Nov 1994 08:49:37         


        
5条回答
  •  感动是毒
    2020-12-07 12:32

    Well, here is a manual function to format it:

    def httpdate(dt):
        """Return a string representation of a date according to RFC 1123
        (HTTP/1.1).
    
        The supplied date must be in UTC.
    
        """
        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 "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, dt.day, month,
            dt.year, dt.hour, dt.minute, dt.second)
    

提交回复
热议问题