Python datetime strptime()和strftime():如何保留时区信息 [英] Python datetime strptime() and strftime(): how to preserve the timezone information
问题描述
请参阅以下代码:
import datetime
import pytz
fmt = '%Y-%m-%d%H:%M:%S%Z'
d = datetime.datetime.now(pytz.timezone(America / New_York))
d_string = d.strftime(fmt)
d2 = datetime.datetime.strptime(d_string,fmt)
打印d_string
打印d2.strftime(fmt)
输出是
2013-02 -07 17:42:31 EST
2013-02-07 17:42:31
时区信息在翻译中简单丢失。
如果我将'%Z'切换到'%z',我得到
ValueError:'z'是格式'%Y-%m-%d%H:%M:%S%z'
我知道我可以使用 python-dateutil
,但是我刚刚发现它是bizzare,我无法实现这个简单特征在日期时间并且必须引入更多的依赖?
这里的一部分问题是通常用于表示时区的字符串实际上不是唯一的。 EST只是北美人的美国/纽约。这是C时间API中的一个限制,Python解决方案是...如果有人愿意写PEP,在今后的版本中添加全部tz功能。
您可以将时区解码为偏移量,但会导致夏令时/夏令时信息丢失(例如,您无法将America / Phoenix与America / Los_Angeles区分开夏天)。您可以将时区格式化为3个字母的缩写,但是您无法从中解析出来。
如果您想要一些模糊和模糊的东西,但通常是什么想要的是,您需要一个第三方库,如 dateutil
。
如果您想要的东西实际上是明确的,只需添加实际的tz名称到本地datetime字符串,并在另一端分开它:
d = datetime.datetime .now(pytz.timezone(America / New_York))
dtz_string = d.strftime(fmt)+''+America / New_York
d_string,tz_string = dtz_string.rsplit ('',1)
d2 = datetime.datetime.strptime(d_string,fmt)
tz2 = pytz.timezone(tz_string)
打印dtz_string
打印d2 .strftime(fmt)+''+ tz_string
或者在这两个之间的中间,你已经使用 pytz
库,可以解析(根据某些任意但明确定义的dis模糊规则)格式,如EST。所以,如果你真的想,你可以在格式化方面留下%Z
,然后把它关掉,并用 pytz.timezone ()
,然后将其余部分传递给 strptime
。
See the following code:
import datetime
import pytz
fmt = '%Y-%m-%d %H:%M:%S %Z'
d = datetime.datetime.now(pytz.timezone("America/New_York"))
d_string = d.strftime(fmt)
d2 = datetime.datetime.strptime(d_string, fmt)
print d_string
print d2.strftime(fmt)
the output is
2013-02-07 17:42:31 EST
2013-02-07 17:42:31
The timezone information simply got lost in the translation.
If I switch '%Z' to '%z', I get
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z'
I know I can use python-dateutil
, but I just found it bizzare that I can't achieve this simple feature in datetime and have to introduce more dependency?
Part of the problem here is that the strings usually used to represent timezones are not actually unique. "EST" only means "America/New_York" to people in North America. This is a limitation in the C time API, and the Python solution is… to add full tz features in some future version any day now, if anyone is willing to write the PEP.
You can format and parse a timezone as an offset, but that loses daylight savings/summer time information (e.g., you can't distinguish "America/Phoenix" from "America/Los_Angeles" in the summer). You can format a timezone as a 3-letter abbreviation, but you can't parse it back from that.
If you want something that's fuzzy and ambiguous but usually what you want, you need a third-party library like dateutil
.
If you want something that's actually unambiguous, just append the actual tz name to the local datetime string yourself, and split it back off on the other end:
d = datetime.datetime.now(pytz.timezone("America/New_York"))
dtz_string = d.strftime(fmt) + ' ' + "America/New_York"
d_string, tz_string = dtz_string.rsplit(' ', 1)
d2 = datetime.datetime.strptime(d_string, fmt)
tz2 = pytz.timezone(tz_string)
print dtz_string
print d2.strftime(fmt) + ' ' + tz_string
Or… halfway between those two, you're already using the pytz
library, which can parse (according to some arbitrary but well-defined disambiguation rules) formats like "EST". So, if you really want to, you can leave the %Z
in on the formatting side, then pull it off and parse it with pytz.timezone()
before passing the rest to strptime
.
这篇关于Python datetime strptime()和strftime():如何保留时区信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!