python datetime.strptime() 为 %Z 接受哪些可能的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1302701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:53:34  来源:igfitidea点击:

What possible values does datetime.strptime() accept for %Z?

pythondatetime

提问by mike

Python's datetime.strptime() is documented as supporting a timezone in the %Z field. So, for example:

Python 的 datetime.strptime() 被记录为支持 %Z 字段中的时区。因此,例如:

In [1]: datetime.strptime('2009-08-19 14:20:36 UTC', "%Y-%m-%d %H:%M:%S %Z")
Out[1]: datetime.datetime(2009, 8, 19, 14, 20, 36)

However, "UTC" seems to be the only timezone I can get it to support:

但是,“UTC”似乎是我可以让它支持的唯一时区:

In [2]: datetime.strptime('2009-08-19 14:20:36 EDT', "%Y-%m-%d %H:%M:%S %Z")
ValueError: time data '2009-08-19 14:20:36 EDT' does not match format '%Y-%m-%d %H:%M:%S %Z'

In [3]: datetime.strptime('2009-08-19 14:20:36 America/Phoenix', "%Y-%m-%d %H:%M:%S %Z")
ValueError: time data '2009-08-19 14:20:36 America/Phoenix' does not match format '%Y-%m-%d %H:%M:%S %Z'

In [4]: datetime.strptime('2009-08-19 14:20:36 -0700', "%Y-%m-%d %H:%M:%S %Z")
ValueError: time data '2009-08-19 14:20:36 -0700' does not match format '%Y-%m-%d %H:%M:%S %Z'

What format is it expecting for %Z? Or, how do I represent a timezone other than UTC?

%Z 期望什么格式?或者,我如何表示 UTC 以外的时区?

采纳答案by hughdbrown

I gather they are GMT, UTC, and whatever is listed in time.tzname.

我认为它们是 GMT、UTC 以及 time.tzname 中列出的任何内容。

>>> for t in time.tzname:
...     print t
...
Eastern Standard Time
Eastern Daylight Time
>>> datetime.strptime('2009-08-19 14:20:36 Eastern Standard Time', "%Y-%m-%d %H:%M:%S %Z")
datetime.datetime(2009, 8, 19, 14, 20, 36)
>>> datetime.strptime('2009-08-19 14:20:36 UTC', "%Y-%m-%d %H:%M:%S %Z")
datetime.datetime(2009, 8, 19, 14, 20, 36)
>>> datetime.strptime('2009-08-19 14:20:36 GMT', "%Y-%m-%d %H:%M:%S %Z")
datetime.datetime(2009, 8, 19, 14, 20, 36)

These settings are machine-specific, of course, and yours will be different in all likelihood.

当然,这些设置是特定于机器的,您的设置很可能会有所不同。

回答by brianz

This is from the timemodule, but I'm almost certain it applies to datetime:

这是来自time模块,但我几乎可以肯定它适用于datetime

Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

对 %Z 指令的支持基于 tzname 中包含的值以及 daylight 是否为真。因此,它是特定于平台的,除了识别始终已知的 UTC 和 GMT(并且被认为是非夏令时时区)。

https://docs.python.org/library/time.html

https://docs.python.org/library/time.html

On my system:

在我的系统上:

>>> import time
>>> time.tzname
('PST', 'PDT')

Using anything but these in datetime.strptime results in an exception. So, look to see what you have available on your machine.

在 datetime.strptime 中使用除这些之外的任何内容都会导致异常。所以,看看你的机器上有什么。