python time.strftime %z 始终为零而不是时区偏移 [英] python time.strftime %z is always zero instead of timezone offset

查看:59
本文介绍了python time.strftime %z 始终为零而不是时区偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预><代码>>>>导入时间>>>t=1440935442>>>time.strftime("%Y/%m/%d-%H:%M:%S %z",time.gmtime(t))'2015/08/30-11:50:42 +0000'>>>time.strftime("%Y/%m/%d-%H:%M:%S %z",time.localtime(t))'2015/08/30-13:50:42 +0000'

偏移量保持不变 +0000,但我预计 '2015/08/30-13:50:42 +0200'

时区是正确的,因为该命令正在解释大写的 %Z

<预><代码>>>>time.strftime("%Y/%m/%d-%H:%M:%S %Z",time.localtime(t))'2015/08/30-13:50:42 CEST'

Unix 日期就像我想要的一样

$ date -u --date @1440935442 +"%Y/%m/%d-%H:%M:%S %z"2015/08/30-11:50:42 +0000$ date --date @1440935442 +"%Y/%m/%d-%H:%M:%S %z"2015/08/30-13:50:42 +0200

解决方案

如文档所述:

<块引用>

本模块中定义的大部分函数调用平台C库具有相同名称的函数.有时咨询可能会有所帮助平台文档,因为这些函数的语义因平台而异.

:

<块引用>

某些平台可能支持其他指令,但仅限于此处列出的具有 ANSI C 标准化的含义.要查看您的平台支持的全套格式代码,请参阅strftime(3) 文档.

...

<块引用>

现在不推荐使用 %Z,但是 %z 转义扩展为并非所有 ANSI C 库都支持首选小时/分钟偏移量.

time.strftime() 使用 C strftime(),因此行为是平台相关的.%z 应该在 POSIX 上工作,但 %z 可能返回相同的结果为 %Z 在 Windows 上.%z 未在 Python 2 上记录,因此 time 模块应该返回 C strftime() 在给定平台上返回的任何内容,而无需任何更改.

相同的代码在我的机器上的 Python 3 中工作:

<预><代码>>>>导入时间>>>t = 1440935442>>>time.strftime("%Z%z", time.gmtime(t))'格林威治标准时间+0000'>>>time.strftime("%Z%z", time.localtime(t))'CEST+0200'

您的问题似乎是 Python 2 特有的:

<预><代码>>>>导入时间>>>t = 1440935442>>>time.strftime("%Z%z", time.gmtime(t))'CET+0000'>>>time.strftime("%Z%z", time.localtime(t))'CEST+0000'

注意:time.strftime('%Z%z') 在 Python 2 和 3 上返回 'CEST+0200'.差异可能是由Python <3.3 中缺少 tm_zonetm_gmtoff 属性.time.gmtime()time.localtime() 都没有提供 Python 2 的时区信息(除了 tm_isdst 这就是为什么 time.gmtime() 导致 CET).time.strftime('%Z%z') 使用 C localtime() 因此它可以提供 tm_zone, tm_gmtoff甚至在 Python 2 上.

如果您需要可移植的行为并支持过去可能具有不同 tzname、utc 偏移量的时区;您可以使用 pytz tzinfo 对象(例如,通过 tzlocal 模块)提供对历史时区数据库的访问:

<预><代码>>>>从日期时间导入日期时间>>>import tzlocal # $ pip install tzlocal>>>datetime.fromtimestamp(1440935442, tzlocal.get_localzone()).strftime('%Z%z')'CEST+0200'

>>> import time
>>> t=1440935442
>>> time.strftime("%Y/%m/%d-%H:%M:%S %z",time.gmtime(t))
'2015/08/30-11:50:42 +0000'
>>> time.strftime("%Y/%m/%d-%H:%M:%S %z",time.localtime(t))
'2015/08/30-13:50:42 +0000'

The offset stays the same +0000, but I expect '2015/08/30-13:50:42 +0200'

The timezone is correct, as the command is interpreting capital %Z as it should

>>> time.strftime("%Y/%m/%d-%H:%M:%S %Z",time.localtime(t))
'2015/08/30-13:50:42 CEST'

Unix date works like I want

$ date -u --date @1440935442 +"%Y/%m/%d-%H:%M:%S %z"
2015/08/30-11:50:42 +0000
$ date --date @1440935442 +"%Y/%m/%d-%H:%M:%S %z"
2015/08/30-13:50:42 +0200

解决方案

As documented:

Most of the functions defined in this module call platform C library functions with the same name. It may sometimes be helpful to consult the platform documentation, because the semantics of these functions varies among platforms.

and:

Additional directives may be supported on certain platforms, but only the ones listed here have a meaning standardized by ANSI C. To see the full set of format codes supported on your platform, consult the strftime(3) documentation.

...

The use of %Z is now deprecated, but the %z escape that expands to the preferred hour/minute offset is not supported by all ANSI C libraries.

time.strftime() uses C strftime() and therefore the behavior is platform-dependent. %z should work on POSIX but %z may return the same result as %Z on Windows. %z is not documented on Python 2 and therefore time module should return whatever C strftime() returns on the given platform without any changes.

The same code works in Python 3 on my machine:

>>> import time
>>> t = 1440935442
>>> time.strftime("%Z%z", time.gmtime(t))
'GMT+0000'
>>> time.strftime("%Z%z", time.localtime(t)) 
'CEST+0200'

Your issue seems to be Python 2 specific:

>>> import time
>>> t = 1440935442
>>> time.strftime("%Z%z", time.gmtime(t))
'CET+0000'
>>> time.strftime("%Z%z", time.localtime(t))
'CEST+0000'

Note: time.strftime('%Z%z') returns 'CEST+0200' on both Python 2 and 3. The difference might be explained by the absence of tm_zone, tm_gmtoff attributes in Python <3.3. Neither time.gmtime() nor time.localtime() provide timezone info on Python 2 (apart from tm_isdst that is why time.gmtime() leads to CET). time.strftime('%Z%z') uses C localtime() and therefore it may provide tm_zone, tm_gmtoff even on Python 2.

If you need portable behavior and to support timezones that might have different tzname, utc offset in the past; you could use pytz tzinfo objects (e.g., via tzlocal module) that provide access to the historical timezone database:

>>> from datetime import datetime
>>> import tzlocal # $ pip install tzlocal
>>> datetime.fromtimestamp(1440935442, tzlocal.get_localzone()).strftime('%Z%z')
'CEST+0200'

这篇关于python time.strftime %z 始终为零而不是时区偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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