python:datetime tzinfo时区名称文档

时间:2013-03-28 21:59:25

标签: python datetime timezone

我有一个我建立的日期:

from datetime import datetime
from datetime import tzinfo
test = '2013-03-27 23:05'
test2 = datetime.strptime(test,'%Y-%m-%d %H:%M')
>>> test2
datetime.datetime(2013, 3, 27, 23, 5)

>>> test2.replace(tzinfo=EST)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'EST' is not defined

>> test2.replace(tzinfo=UTC)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'UTC' is not defined

我无法在names来电中分配给tzinfo的时区replace.tzinfo=列表中找到文档。

我已阅读以下内容,但没有任何内容:

http://docs.python.org/2/library/datetime.html#tzinfo-objects

我也在谷歌搜索过。

编辑:我按照unutbu提供的解决方案,但我得到以下内容:

>>> test = '2013-03-27 00:05'
>>> test
'2013-03-27 00:05'

>>> test2 = dt.datetime.strp(test, '%Y-%m-%d %H:%M')
>>> test2
datetime.datetime(2013, 3, 27, 0, 5)

>>> est = pytz.timezone('US/Eastern')
>>> utc = pytz.utc
>>> print(est.localize(test2))
2013-03-27 00:05:00-04:00
>>> print(utc.localize(test2))
2013-03-27 00:05:00+00:00

>>> print(est.localize(test2,is_dst=False))
2013-03-27 00:05:00-04:00
>>> print(est.localize(test2,is_dst=True))
2013-03-27 00:05:00-04:00
>>>

正如你所看到的,即使我提供了is_dst =标志,偏移仍然是'-04:00',这是EDT而不是EST。我很感激帮助。谢谢。

文档显示以下内容:

如果您坚持使用当地时间,这个库提供了明确构建它们的工具: http://pytz.sourceforge.net/#problems-with-localtime

>>> loc_dt = datetime(2002, 10, 27, 1, 30, 00)
>>> est_dt = eastern.localize(loc_dt, is_dst=True)
>>> edt_dt = eastern.localize(loc_dt, is_dst=False)
>>> print(est_dt.strftime(fmt) + ' / ' + edt_dt.strftime(fmt))
2002-10-27 01:30:00 EDT-0400 / 2002-10-27 01:30:00 EST-0500

east在文档的早期定义为eastern = timezone('US/Eastern')

这似乎表明is_dst= flag应进一步指定是否指定日光节约。我很感激为什么这不适用于我的情况。

4 个答案:

答案 0 :(得分:29)

标准库没有定义任何时区 - 至少不是很好(the documentation中给出的玩具示例不能处理像mentioned here那样的细微问题。对于预定义的时区,请使用第三方pytz module

import pytz
import datetime as DT

eastern = pytz.timezone('US/Eastern')
utc = pytz.utc
test = '2013-03-27 23:05'

这是一个“天真”的约会时间:

test2 = DT.datetime.strptime(test, '%Y-%m-%d %H:%M')   
print(test2)
# 2013-03-27 23:05:00

通过将test2解释为在EST时区中,可以获得时区感知日期时间:

print(eastern.localize(test2))
# 2013-03-27 23:05:00-04:00

这可以通过将test2解释为在UTC时区中来表示时区感知日期时间:

print(utc.localize(test2))
# 2013-03-27 23:05:00+00:00

或者,您可以使用astimezone方法将一个时区感知日期时间转换为另一个时区:

test2_eastern = eastern.localize(test2)
print(test2_eastern.astimezone(utc))
# 2013-03-28 03:05:00+00:00

答案 1 :(得分:2)

正如@MrFuppes 所提到的,从 python 3.9 开始,您不需要安装 3rd 方 pytz,而是使用本机 zoneinfo

from datetime import datetime
from zoneinfo import ZoneInfo

test = '2013-03-27 23:05'
test2 = datetime.strptime(test,'%Y-%m-%d %H:%M')
date_string = test2.replace(tzinfo=ZoneInfo('US/Eastern'))
print(datestring)
2013-03-27 23:05:00-04:00

答案 2 :(得分:1)

 import pytz
 timezones=pytz.all_timezones

这给出了所有时区

答案 3 :(得分:1)

自Python 3.9发行以来,标准库确实定义了时区,您可以通过以下方式获取它们:

import zoneinfo
print(zoneinfo.available_timezones())

# {'America/Belem', 'Asia/Tel_Aviv', 'Australia/North', 'Asia/Omsk', 
#  'Europe/Isle_of_Man', 'America/New_York', 'Europe/Nicosia', 
#  'Pacific/Funafuti', 'America/Ensenada', 'Europe/Mariehamn', 
#  'America/Maceio', 'America/Guatemala', 'America/Guadeloupe', ...

docs