datetime.strptime()接受%Z可能的值 [英] What possible values does datetime.strptime() accept for %Z?

查看:920
本文介绍了datetime.strptime()接受%Z可能的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 在[1]中:datetime.strptime('2009-08-19 14:20:36 UTC ',%Y-%m-%d%H:%M:%S%Z)
输出[1]:datetime.datetime(2009,8,19,4,20,36)

然而,UTC似乎是唯一可以让它支持的时区:

 在[2]中:datetime.strptime('2009-08-19 14:20:36 EDT',%Y-%m-% d%H:%M:%S%Z)
ValueError:时间数据'2009-08-19 14:20:36 EDT'与格式不匹配%Y-%m-%d%H: %M:%S%Z'

在[3]中:datetime.strptime('2009-08-19 14:20:36 America / Phoenix',%Y-%m-%d %H:%M:%S%Z)
ValueError:时间数据'2009-08-19 14:20:36美国/凤凰'与格式不符%Y-%m-%d%H :%M:%S%Z'

在[4]中:datetime.strptime('2009-08-19 14:20:36 -0700',%Y-%m-%d %H:%M:%S%Z)
ValueError:时间数据'2009-08-19 14:20:36 -0700'不匹配格式'%Y-%m-%d%H: %M:%S%Z'

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

解决方案

我收集他们是GMT,UTC, time.tzname。

 >>>对于t in time.tzname:
... print t
...
东部标准时间
东部夏令时间
>>> 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)

这些设置是机器特定的,当然和你的将会有所不同。


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

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:

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?

解决方案

I gather they are GMT, UTC, and whatever is listed in 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.

这篇关于datetime.strptime()接受%Z可能的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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