python:日期时间 tzinfo 时区名称文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15692906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:42:29  来源:igfitidea点击:

python: datetime tzinfo time zone names documentation

pythondatetimetimezone

提问by codingknob

I have a date that I build:

我有一个我建立的日期:

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

I can't find documentation on the list of time zone namesthat I can assign to tzinfo in the replace.tzinfo=call.

namesreplace.tzinfo=通话中找不到可以分配给 tzinfo的时区列表中的文档。

I have read through the following and there is nothing:

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

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

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

I have also searched in google.

我也在谷歌搜索过。

Edit: I followed the solution provided by unutbu but I get the following:

编辑:我遵循了 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
>>>

As you can see even when I provide the is_dst= flag the offset is still '-04:00', which is EDT and not EST. I appreciate the help. Thank you.

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

The documentation shows the following:

文档显示以下内容:

If you insist on working with local times, this library provides a facility for constructing them unambiguously: http://pytz.sourceforge.net/#problems-with-localtime

如果你坚持使用当地时间,这个库提供了一个明确构建它们的工具: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 was defined earlier in the documentation as eastern = timezone('US/Eastern')

东部在文档中早些时候被定义为 eastern = timezone('US/Eastern')

This seems to indicate that the is_dst= flagshould further specify whether day light savings is specified or not. I would appreciate help on why this isn't working in my case.

这似乎表明is_dst= flag应该进一步指定是否指定夏令时。我很感激有关为什么这在我的情况下不起作用的帮助。

采纳答案by unutbu

The standard library does not define any timezones -- at least not well (the toy example given in the documentationdoes not handle subtle problems like the ones mentioned here). For predefined timezones, use the third-party pytz module.

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

import pytz
import datetime as DT

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

This is a "naive" datetime:

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

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

This makes a timezone-aware datetime by interpreting test2as if it were in the EST timezone:

这通过将test2其解释为 EST 时区来生成时区感知日期时间:

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

This makes a timezone-aware datetime by interpreting test2as if it were in the UTC timezone:

这通过将日期时间解释test2为 UTC 时区来生成时区感知日期时间:

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

Alternatively, you can convert one timezone-aware datetime to another timezone using the astimezonemethod:

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

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

回答by Three-pth

 import pytz
 timezones=pytz.all_timezones

This gives all timezones

这给出了所有时区