<bdo id='E2mdo'></bdo><ul id='E2mdo'></ul>

    1. <tfoot id='E2mdo'></tfoot>
      <i id='E2mdo'><tr id='E2mdo'><dt id='E2mdo'><q id='E2mdo'><span id='E2mdo'><b id='E2mdo'><form id='E2mdo'><ins id='E2mdo'></ins><ul id='E2mdo'></ul><sub id='E2mdo'></sub></form><legend id='E2mdo'></legend><bdo id='E2mdo'><pre id='E2mdo'><center id='E2mdo'></center></pre></bdo></b><th id='E2mdo'></th></span></q></dt></tr></i><div id='E2mdo'><tfoot id='E2mdo'></tfoot><dl id='E2mdo'><fieldset id='E2mdo'></fieldset></dl></div>

      <legend id='E2mdo'><style id='E2mdo'><dir id='E2mdo'><q id='E2mdo'></q></dir></style></legend>
      1. <small id='E2mdo'></small><noframes id='E2mdo'>

        Python:datetime tzinfo 时区名称文档

        时间:2023-07-03
        • <bdo id='o9Tzj'></bdo><ul id='o9Tzj'></ul>

                <tbody id='o9Tzj'></tbody>

              <legend id='o9Tzj'><style id='o9Tzj'><dir id='o9Tzj'><q id='o9Tzj'></q></dir></style></legend>

              <small id='o9Tzj'></small><noframes id='o9Tzj'>

                1. <i id='o9Tzj'><tr id='o9Tzj'><dt id='o9Tzj'><q id='o9Tzj'><span id='o9Tzj'><b id='o9Tzj'><form id='o9Tzj'><ins id='o9Tzj'></ins><ul id='o9Tzj'></ul><sub id='o9Tzj'></sub></form><legend id='o9Tzj'></legend><bdo id='o9Tzj'><pre id='o9Tzj'><center id='o9Tzj'></center></pre></bdo></b><th id='o9Tzj'></th></span></q></dt></tr></i><div id='o9Tzj'><tfoot id='o9Tzj'></tfoot><dl id='o9Tzj'><fieldset id='o9Tzj'></fieldset></dl></div>
                  <tfoot id='o9Tzj'></tfoot>

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

                  问题描述

                  我有一个日期:

                  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
                  

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

                  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

                  我也在谷歌上搜索过.

                  编辑:我遵循了 unutbu 提供的解决方案,但得到以下信息:

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

                  正如您所见,即使我提供了 is_dst= 标志,偏移量仍然是-04:00",即 EDT 而不是 EST.我很感激帮助.谢谢.

                  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.

                  文档显示如下:

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

                  eastern was defined earlier in the documentation as eastern = timezone('US/Eastern')

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

                  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.

                  推荐答案

                  标准库没有定义任何时区——至少没有定义好(文档不处理像 此处提及).对于预定义的时区,请使用第三方 pytz 模块.

                  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
                  

                  这通过将 test2 解释为就好像它在 EST 时区中来生成可感知时区的日期时间:

                  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
                  

                  这通过将 test2 解释为就好像它在 UTC 时区中来生成可感知时区的日期时间:

                  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
                  

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

                  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 时区名称文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何强制 pytz 使用当前的标准时区? 下一篇:在 Python 中使用 pytz 本地化纪元时间

                  相关文章

                  <i id='kDxu0'><tr id='kDxu0'><dt id='kDxu0'><q id='kDxu0'><span id='kDxu0'><b id='kDxu0'><form id='kDxu0'><ins id='kDxu0'></ins><ul id='kDxu0'></ul><sub id='kDxu0'></sub></form><legend id='kDxu0'></legend><bdo id='kDxu0'><pre id='kDxu0'><center id='kDxu0'></center></pre></bdo></b><th id='kDxu0'></th></span></q></dt></tr></i><div id='kDxu0'><tfoot id='kDxu0'></tfoot><dl id='kDxu0'><fieldset id='kDxu0'></fieldset></dl></div>

                3. <legend id='kDxu0'><style id='kDxu0'><dir id='kDxu0'><q id='kDxu0'></q></dir></style></legend>
                  <tfoot id='kDxu0'></tfoot>

                    <bdo id='kDxu0'></bdo><ul id='kDxu0'></ul>
                4. <small id='kDxu0'></small><noframes id='kDxu0'>