python:datetime tzinfo时区名称文档

cod*_*nob 19 python timezone datetime

我有一个约会:

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
Run Code Online (Sandbox Code Playgroud)

我找不到names可以在replace.tzinfo=调用中分配给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
>>>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,即使我提供了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
Run Code Online (Sandbox Code Playgroud)

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

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

unu*_*tbu 34

标准库没有定义任何时区 - 至少不是很好(文档中给出的玩具示例不处理像这里提到的那些细微问题).对于预定义的时区,请使用第三方pytz模块.

import pytz
import datetime as DT

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

这是一个"天真"的日期时间:

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

这使得时区感知日期时间通过解释test2就像它在EST时区中一样:

print(eastern.localize(test2))
# 2013-03-27 23:05:00-04:00
Run Code Online (Sandbox Code Playgroud)

这使得时区感知日期时间通过解释test2就像它在UTC时区中一样:

print(utc.localize(test2))
# 2013-03-27 23:05:00+00:00
Run Code Online (Sandbox Code Playgroud)

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

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


小智 11

正如 @FObersteiner 提到的,从 python 3.9开始,您不需要安装第 3 方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
Run Code Online (Sandbox Code Playgroud)


MrF*_*pes 9

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', ...
Run Code Online (Sandbox Code Playgroud)