Python: date, time formatting

后端 未结 2 771
Happy的楠姐
Happy的楠姐 2020-12-14 02:08

I need to generate a local timestamp in a form of YYYYMMDDHHmmSSOHH\'mm\'. That OHH\'mm\' is one of +, -, Z and then there are hourhs and minutes followed by \'.

Ple

2条回答
  •  不思量自难忘°
    2020-12-14 03:02

    import time
    
    localtime   = time.localtime()
    timeString  = time.strftime("%Y%m%d%H%M%S", localtime)
    
    # is DST in effect?
    timezone    = -(time.altzone if localtime.tm_isdst else time.timezone)
    timeString += "Z" if timezone == 0 else "+" if timezone > 0 else "-"
    timeString += time.strftime("%H'%M'", time.gmtime(abs(timezone)))
    

提交回复
热议问题