本文介绍了python:datetime tzinfo时区名称文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个日期,我建立:

 从datetime import datetime 
从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)
追溯(最近的最后一次呼叫):
文件< stdin>,第1行在< module>
NameError:未定义名称'EST'

>> test2.replace(tzinfo = UTC)
追溯(最近的最后一次调用):
文件< stdin>,第1行,< module>
NameError:name'UTC'未定义

我找不到文档可以在 replace.tzinfo = 调用中分配给tzinfo的时区列表名称 $ / b
$ b

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




$ b $我也在谷歌搜索。



编辑:我遵循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。我很感激帮助谢谢。



文档显示以下内容:



如果您坚持使用本地时间,此库提供了一个明确构建它们的设施:

 >>> loc_dt = datetime(2002,10,27,1,30,00)
>>> est_dt = east.localize(loc_dt,is_dst = True)
>>> edt_dt = east.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-东部早在文档中定义为 east = timezone('US'),在$ 00





这似乎表明 is_dst = flag 应该进一步指定是否指定日间节约光。我很乐意帮助我为什么这不工作在我的情况。

解决方案

标准库没有定义任何时区 - 至少不太好(不会处理像)。对于预定义的时区,请使用第三方。

  import pytz 
import datetime as DT

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

这是一个天真的datetime: / p>

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

通过将 test2 解释为EST时区中的时区知道datetime:

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

通过将 test2 解释为UTC时区中的时区来实现日期时间:

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

或者,您可以转换一个t imezone感知datetime到另一个时区使用 astimezone 方法:

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


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 names that I can assign to tzinfo in the replace.tzinfo= call.

I have read through the following and there is nothing:

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:

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

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

>>> 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')

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

解决方案

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

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 test2 as if it were in the EST timezone:

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

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

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 astimezone method:

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

这篇关于python:datetime tzinfo时区名称文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 12:25