我正在使用以下代码,它返回:
now = time.strftime("%c")
Out[196]:
'Mon Jan 9 18:34:48 2017'
我们有一些解析器读取时间戳,它们期望以下约定:
Mon Jan 9 17:02:40 PST 2017
主要区别在于我的代码缺少时区的秒数和年份。我已经使用了 time.strftime
和 datetime
库,但是我仍然无法获得我正在寻找的时间戳的对齐方式。
我还认为我可以将时间戳插入或加入到缺少时区的现有字符串中,但即使这样对我来说也不起作用。
最佳答案
您可以使用datetime
而不是time
获取时区:
import datetime as dt
from tzlocal import get_localzone # $ pip install tzlocal
local_tz = get_localzone()
now = dt.datetime.now(local_tz)
print now.strftime('%a %b %d %H:%M:%S %Z %Y')
请务必将时区分配给日期时间
,如 naive时间戳不会显示时区。
上面的结果是:
Mon Jan 09 19:40:28 PST 2017
关于python - 将时区添加到Python的当前时间戳中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41560350/