Python:日期,时间格式 [英] Python: date, time formatting

查看:172
本文介绍了Python:日期,时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以YYYYMMDDHHmmSSOHH'mm'的形式生成本地时间戳。那个OHH'mm'是+, - ,Z之一,然后是小时和分钟,后跟'。

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 '.

请问如何获得这样的时间戳,表示当地时区和可能的夏令时?

Please, how do I get such a timestamp, denoting both local time zone and possible daylight saving?

推荐答案

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)))

这篇关于Python:日期,时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆