• <small id='bkG2e'></small><noframes id='bkG2e'>

    1. <tfoot id='bkG2e'></tfoot>
      <legend id='bkG2e'><style id='bkG2e'><dir id='bkG2e'><q id='bkG2e'></q></dir></style></legend>

        <bdo id='bkG2e'></bdo><ul id='bkG2e'></ul>
        <i id='bkG2e'><tr id='bkG2e'><dt id='bkG2e'><q id='bkG2e'><span id='bkG2e'><b id='bkG2e'><form id='bkG2e'><ins id='bkG2e'></ins><ul id='bkG2e'></ul><sub id='bkG2e'></sub></form><legend id='bkG2e'></legend><bdo id='bkG2e'><pre id='bkG2e'><center id='bkG2e'></center></pre></bdo></b><th id='bkG2e'></th></span></q></dt></tr></i><div id='bkG2e'><tfoot id='bkG2e'></tfoot><dl id='bkG2e'><fieldset id='bkG2e'></fieldset></dl></div>
      1. `datetime.now(pytz_timezone)` 什么时候失败?

        when does `datetime.now(pytz_timezone)` fail?(`datetime.now(pytz_timezone)` 什么时候失败?)
      2. <i id='BR0VK'><tr id='BR0VK'><dt id='BR0VK'><q id='BR0VK'><span id='BR0VK'><b id='BR0VK'><form id='BR0VK'><ins id='BR0VK'></ins><ul id='BR0VK'></ul><sub id='BR0VK'></sub></form><legend id='BR0VK'></legend><bdo id='BR0VK'><pre id='BR0VK'><center id='BR0VK'></center></pre></bdo></b><th id='BR0VK'></th></span></q></dt></tr></i><div id='BR0VK'><tfoot id='BR0VK'></tfoot><dl id='BR0VK'><fieldset id='BR0VK'></fieldset></dl></div>
          <tbody id='BR0VK'></tbody>
        <legend id='BR0VK'><style id='BR0VK'><dir id='BR0VK'><q id='BR0VK'></q></dir></style></legend>
      3. <tfoot id='BR0VK'></tfoot>
          <bdo id='BR0VK'></bdo><ul id='BR0VK'></ul>

              • <small id='BR0VK'></small><noframes id='BR0VK'>

                  本文介绍了`datetime.now(pytz_timezone)` 什么时候失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  delorean docs show this way to get the current time in a given timezone using datetime:

                  from datetime import datetime
                  from pytz import timezone
                  
                  EST = "US/Eastern"
                  UTC = "UTC"
                  
                  d = datetime.utcnow()
                  utc = timezone(UTC)
                  est = timezone(EST)
                  d = utc.localize(d)
                  d = est.normalize(EST)
                  

                  and compare it with the delorian-based code:

                  from delorean import Delorean
                  
                  EST = "US/Eastern"
                  
                  d = Delorean(timezone=EST)
                  

                  I believe the datetime example should be written as:

                  from datetime import datetime
                  import pytz
                  
                  eastern_timezone = pytz.timezone("US/Eastern")
                  d = datetime.now(eastern_timezone)
                  

                  that is more concise.

                  Are there any cases when the last code example fails while the first one continues to work?


                  Update: the current example:

                  from datetime import datetime
                  import pytz
                  
                  d = datetime.utcnow()
                  d = pytz.utc.localize(d)
                  
                  est = pytz.timezone('US/Eastern')
                  d = est.normalize(d)
                  return d
                  

                  that is still too verbose.

                  The question stills stands: do you need the explicit round-trip via utc and tz.normalize() or can you use datetime.now(tz) instead?

                  解决方案

                  When does datetime.now(pytz_timezone) fail?

                  As far as I can tell, there are no scenarios where it could fail. datetime.now invokes the fromutc function on the tzinfo instance passed in the parameter. All conversions from UTC to local time are unambiguous, so there are no opportunities for failure.

                  Also, the original code does not even work.

                  d = est.normalize(EST)
                  

                  This would appear to pass a string as the only parameter to normalize, which is intended to take a datetime. This gives:

                  AttributeError: 'str' object has no attribute 'tzinfo'
                  

                  I believe they meant to write:

                  d = est.normalize(d.astimezone(est))
                  

                  That said, I don't think the verbosity of their code adds much value. As you noted, it's just as easy to do this in a single step:

                  d = datetime.now(est)
                  

                  Looking at the cpython source code for datetime.now, I can see that when a tzinfo object is provided, it calls the fromutc method on that object.

                  if (self != NULL && tz != Py_None) {
                      /* Convert UTC to tzinfo's zone. */
                      PyObject *temp = self;
                  
                      self = _PyObject_CallMethodId(tz, &PyId_fromutc, "O", self);
                      Py_DECREF(temp);
                  }
                  

                  Then, in the pytz source, I see that the fromutc method is implemented differently depending on whether the zone is pytz.UTC, or an instance of StaticTzInfo, or DstTzInfo. In all three cases, the transformation from the input UTC value to the target time zone is unambiguous. Here is the DstTzInfo implementation, which is the more complex of the three:

                  def fromutc(self, dt):
                      '''See datetime.tzinfo.fromutc'''
                      if (dt.tzinfo is not None
                          and getattr(dt.tzinfo, '_tzinfos', None) is not self._tzinfos):
                          raise ValueError('fromutc: dt.tzinfo is not self')
                      dt = dt.replace(tzinfo=None)
                      idx = max(0, bisect_right(self._utc_transition_times, dt) - 1)
                      inf = self._transition_info[idx]
                      return (dt + inf[0]).replace(tzinfo=self._tzinfos[inf])
                  

                  This would appear to find the transition from _utc_transition_times of the time zone, then apply it to the returned datetime. There are no ambiguities in this direction, so the results will be equivalent.

                  Also worth noting, in the datetime docs it says that datetime.now is equivalent to calling:

                  tz.fromutc(datetime.utcnow().replace(tzinfo=tz))
                  

                  Given the source of fromutc in pytz I showed earlier, I'm not sure that this is any different than just:

                  tz.fromutc(datetime.utcnow())
                  

                  But in either case, I don't think localize and normalize are necessary.

                  这篇关于`datetime.now(pytz_timezone)` 什么时候失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                1. <i id='U0CNn'><tr id='U0CNn'><dt id='U0CNn'><q id='U0CNn'><span id='U0CNn'><b id='U0CNn'><form id='U0CNn'><ins id='U0CNn'></ins><ul id='U0CNn'></ul><sub id='U0CNn'></sub></form><legend id='U0CNn'></legend><bdo id='U0CNn'><pre id='U0CNn'><center id='U0CNn'></center></pre></bdo></b><th id='U0CNn'></th></span></q></dt></tr></i><div id='U0CNn'><tfoot id='U0CNn'></tfoot><dl id='U0CNn'><fieldset id='U0CNn'></fieldset></dl></div>

                    <tbody id='U0CNn'></tbody>

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

                    • <legend id='U0CNn'><style id='U0CNn'><dir id='U0CNn'><q id='U0CNn'></q></dir></style></legend>
                        <bdo id='U0CNn'></bdo><ul id='U0CNn'></ul>

                        <tfoot id='U0CNn'></tfoot>