开发者

Rfc 1123 in python

Just want to use 'datetime' or 'time' modules. I based the following code on this question:

import time

def rfc1123 (gmt):
    weekday = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][gmt.tm_wday]

    month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][gmt.tm_mon]

    return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (weekday, gmt.tm_mday, month, gmt.tm_year, gmt.tm_hour, gmt.tm_min, gmt.tm_sec)

print (rfc1123 (time.gmtime ()))

Is something wrong ? I'm really worried abo开发者_如何学Cut the DST.

Thanks.


An easier way to do it :

from time import strftime, gmtime
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())

See exemple in python python documentation


If you have a bit of flexibility around using datetime or time, an even easier way to do it is to use email.utils.formatdate(). See this Stack Overflow answer for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜