[英]python time.strftime %z is always zero instead of timezone offset
>>> 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' 偏移量保持不变+0000,但我希望是'2015/08 / 30-13:50:42 +0200'
The timezone is correct, as the command is interpreting capital %Z as it should 时区正确,因为命令正在解释大写字母%Z
>>> 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 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
As documented : 如记录所示 :
Most of the functions defined in this module call platform C library functions with the same name.
该模块中定义的大多数函数都使用相同的名称调用平台C库函数。 It may sometimes be helpful to consult the platform documentation, because the semantics of these functions varies among platforms.
有时查阅平台文档可能会有所帮助,因为这些功能的语义在平台之间会有所不同。
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.
某些平台上可能支持其他指令,但只有此处列出的指令具有ANSI C标准化的含义。要查看平台上支持的全套格式代码,请查阅strftime(3)文档。
... ...
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.
现在已不建议使用%Z,但并非所有ANSI C库都支持%z转义扩展到首选的小时/分钟偏移量。
time.strftime()
uses C strftime()
and therefore the behavior is platform-dependent. time.strftime()
使用C strftime()
,因此行为与平台有关。 %z
should work on POSIX but %z
may return the same result as %Z
on Windows . %z
应该可以在POSIX上使用,但是%z
可能返回与Windows上的%Z
相同的结果 。 %z
is not documented on Python 2 and therefore time
module should return whatever C strftime()
returns on the given platform without any changes. %z
在Python 2上没有记录,因此time
模块应该返回给定平台上返回的C strftime()
,而无需进行任何更改。
The same code works in Python 3 on my machine: 相同的代码可在我的机器上的Python 3中运行:
>>> 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: 您的问题似乎是特定于Python 2的:
>>> 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. 注意:
time.strftime('%Z%z')
在Python 2和Python 3上均返回'CEST+0200'
。这种差异可能是由于Python <3.3中缺少tm_zone
和tm_gmtoff
属性tm_gmtoff
。 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.gmtime()
和time.localtime()
都不提供Python 2上的时区信息(除了tm_isdst
之外,这就是time.gmtime()
导致CET
)。 time.strftime('%Z%z')
uses C localtime()
and therefore it may provide tm_zone
, tm_gmtoff
even on Python 2. time.strftime('%Z%z')
使用C localtime()
,因此即使在Python 2上也可以提供tm_zone
, tm_gmtoff
。
If you need portable behavior and to support timezones that might have different tzname, utc offset in the past; 如果您需要可移植的行为并支持可能具有不同tzname的时区,请使用utc过去的偏移量; you could use
pytz
tzinfo objects (eg, via tzlocal
module) that provide access to the historical timezone database: 您可以使用
pytz
tzinfo对象(例如,通过tzlocal
模块)来提供对历史时区数据库的访问:
>>> from datetime import datetime
>>> import tzlocal # $ pip install tzlocal
>>> datetime.fromtimestamp(1440935442, tzlocal.get_localzone()).strftime('%Z%z')
'CEST+0200'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.