【问题标题】:Python: datetime tzinfo time zone names documentationPython:datetime tzinfo 时区名称文档
【发布时间】:2013-03-19 13:03:48
【问题描述】:

我有一个日期:

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 时区列表上找不到文档,我可以在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
>>>

正如您所看到的,即使我提供了is_dst= 标志,偏移量仍然是“-04:00”,它是美国东部时间而不是美国东部时间。我很感激帮助。谢谢。

文档显示如下:

如果您坚持使用当地时间,这个库提供了一种明确构建它们的工具: 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

eastern 在文档前面定义为eastern = timezone('US/Eastern')

这似乎表明is_dst= 标志应该进一步指定是否指定了夏令时。我会很感激为什么这对我的情况不起作用的帮助。

【问题讨论】:

  • 我在尝试运行此程序时遇到错误:AttributeError: 'module' object has no attribute 'strptime'。你确定你复制的代码正确吗?
  • 哎呀。您是否导入了 datetime 和 tzinfo?我将编辑我的原始帖子以包含它。
  • 我的错,我导入了datetime,而不是datetime.datetime。谢谢!

标签: python datetime timezone


【解决方案1】:

标准库没有定义任何时区——至少不是很好(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

【讨论】:

  • @MartinAtkins:感谢您的链接。
  • @unutbu - 谢谢。问题:为什么时间偏移是'-04:00'?这不应该被视为 EST 而不是 EDT 吗?
  • @algotr8der:夏令时从 2013 年 3 月 10 日凌晨 2 点开始。所以它是“美国/东部”时区的 EDT。
  • 如果您真的想要 EST,无论是哪一天,请使用 pytz.timezone('EST')
  • @algotr8der:is_dst 参数仅影响localize 解释模糊日期的方式。 (在秋天,你“后退”,创造了两个用相同“名字”去的小时)。
【解决方案2】:
 import pytz
 timezones=pytz.all_timezones

这给出了所有时区

【讨论】:

  • 你应该提到这是一个单独的依赖项,并讨论为什么要使用它等等。现在 python 3.9 有 zoneinfo
【解决方案3】:

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', ...

【讨论】:

    【解决方案4】:

    正如@MrFuppes 所说,从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
    

    【讨论】:

      猜你喜欢
      • 2012-12-22
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2011-08-18
      • 1970-01-01
      相关资源
      最近更新 更多