<i id='93lgk'><tr id='93lgk'><dt id='93lgk'><q id='93lgk'><span id='93lgk'><b id='93lgk'><form id='93lgk'><ins id='93lgk'></ins><ul id='93lgk'></ul><sub id='93lgk'></sub></form><legend id='93lgk'></legend><bdo id='93lgk'><pre id='93lgk'><center id='93lgk'></center></pre></bdo></b><th id='93lgk'></th></span></q></dt></tr></i><div id='93lgk'><tfoot id='93lgk'></tfoot><dl id='93lgk'><fieldset id='93lgk'></fieldset></dl></div>
  • <legend id='93lgk'><style id='93lgk'><dir id='93lgk'><q id='93lgk'></q></dir></style></legend>
      • <bdo id='93lgk'></bdo><ul id='93lgk'></ul>
    1. <small id='93lgk'></small><noframes id='93lgk'>

      <tfoot id='93lgk'></tfoot>

        如何检查当前时间是否在python的范围内?

        How to check if the current time is in range in python?(如何检查当前时间是否在python的范围内?)
          <tbody id='4L76C'></tbody>
        <tfoot id='4L76C'></tfoot>

          <bdo id='4L76C'></bdo><ul id='4L76C'></ul>
        • <small id='4L76C'></small><noframes id='4L76C'>

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

                1. 本文介绍了如何检查当前时间是否在python的范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要检查当前时间是否在时间范围内.最简单的情况time_end > time_start:

                  I need to check if the current time is in timerange. The most simple case time_end > time_start:

                  if time(6,0) <= now.time() <= time(12,00): print '1'
                  

                  但是当用户进入结束时间小于开始时间的时间范围时,麻烦就开始了,例如23:00 - 06:00".像00:00"这样的时间将在此范围内.大约 5 年前,我编写了这个 PHP 函数:

                  But troubles begin when user enters a time range when the end time is smaller than the start time, e.g. "23:00 - 06:00". A time like '00:00' will be in this range. About 5 years ago I wrote this PHP function:

                  function checkInterval($start, $end)
                    {    
                      $dt = date("H:i:s");    
                  
                      $tstart = explode(":", $start);
                      $tend =   explode(":", $end);
                      $tnow =   explode(":", $dt);
                  
                      if (!$tstart[2])
                        $tstart[2] = 0;
                  
                      if (!$tend[2])
                        $tend[2] = 0;  
                  
                      $tstart = $tstart[0]*60*60 + $tstart[1]*60 + $tstart[2];
                      $tend   = $tend[0]*60*60   + $tend[1]*60   + $tend[2];
                      $tnow   = $tnow[0]*60*60   + $tnow[1]*60   + $tnow[2];
                  
                      if ($tend < $tstart)
                        {
                          if ($tend - $tnow > 0 && $tnow > $tstart)
                            return true;
                          else if ($tnow - $tstart > 0 && $tnow > $tend)
                            return true;
                          else if ($tend > $tnow && $tend < $tstart && $tstart > $tnow)
                            return true;
                          else return false;
                        } else
                        {
                          if ($tstart < $tnow && $tend > $tnow)
                            return true;
                          else
                            return false;
                        }
                  

                  现在我需要做同样的事情,但我想让它看起来更好看.那么,我应该使用什么算法来确定当前时间 '00:00' 是否在 reversed 范围内,例如['23:00', '01:00']?

                  Now I need to do the same thing, but I want to make it good looking. So, what algorithm should I use to determine if the current time '00:00' is in reversed range e.g. ['23:00', '01:00']?

                  推荐答案

                  Python 解决方案将会非常非常短.

                  The Python solution is going to be much, much shorter.

                  def time_in_range(start, end, x):
                      """Return true if x is in the range [start, end]"""
                      if start <= end:
                          return start <= x <= end
                      else:
                          return start <= x or x <= end
                  

                  startendx 使用 datetime.time 类.

                  Use the datetime.time class for start, end, and x.

                  >>> import datetime
                  >>> start = datetime.time(23, 0, 0)
                  >>> end = datetime.time(1, 0, 0)
                  >>> time_in_range(start, end, datetime.time(23, 30, 0))
                  True
                  >>> time_in_range(start, end, datetime.time(12, 30, 0))
                  False
                  

                  这篇关于如何检查当前时间是否在python的范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                2. <i id='5z1nX'><tr id='5z1nX'><dt id='5z1nX'><q id='5z1nX'><span id='5z1nX'><b id='5z1nX'><form id='5z1nX'><ins id='5z1nX'></ins><ul id='5z1nX'></ul><sub id='5z1nX'></sub></form><legend id='5z1nX'></legend><bdo id='5z1nX'><pre id='5z1nX'><center id='5z1nX'></center></pre></bdo></b><th id='5z1nX'></th></span></q></dt></tr></i><div id='5z1nX'><tfoot id='5z1nX'></tfoot><dl id='5z1nX'><fieldset id='5z1nX'></fieldset></dl></div>
                  1. <tfoot id='5z1nX'></tfoot>

                  2. <small id='5z1nX'></small><noframes id='5z1nX'>

                        <legend id='5z1nX'><style id='5z1nX'><dir id='5z1nX'><q id='5z1nX'></q></dir></style></legend>

                          <tbody id='5z1nX'></tbody>
                          <bdo id='5z1nX'></bdo><ul id='5z1nX'></ul>