title: While Loop is not working well with the time
body: <p>I have a code with this ↓ part in it, which should do something when the time and day come. It has <code>outer</code> and <code>inner</code> time and day checks (<code>while loops</code>).
  <code>Outer</code> for the situation in which code starts processing in time (or day) which is not in the <code>timerange</code> for code processing - it <code>should</code> wait until that time (or day). And it works, but only in situation when I set it like 10 minutes before the <code>timerange</code> (code processing time). When I set it like a night before, it won't works and it stops in <code>print("timerange wait")</code> It shows False even it should be True. Is there a problem because it is not in the same day? Or something else? Please give me some advices.</p>
  
  <p>Here is the simple version of code (without unnecessary parts )</p>
  
  <pre class="lang-py prettyprint-override"><code>
  import os
  import datetime
  from time import sleep
  from datetime import date
  
  
  
  #setting used variables
  now = datetime.datetime.now()
  morning = now.replace(hour=8, minute=0, second=0, microsecond=0)
  evening = now.replace(hour=18, minute=9, second=0, microsecond=0)
  
  
  # function for check creating "timerange -start time, end time"
  def time_in_range(morning, evening, x):
      if morning &lt;= evening:
          return morning &lt;= x &lt;= evening
      else:
          return morning &lt;= x or x &lt;= evening
  
  
  # main program for doing something
  def main():
      while True:
  # Time period check - Check if time and weekdays are not out of range
  # if not in range will wait till the time
          if date.today().weekday():
              dayz = True
          else:
              dayz = False
          now = datetime.datetime.now()
          timerange = time_in_range(morning, evening, now)
          if timerange != True:
              print("waiting for function-time")
              sleep(10)
              continue
          if dayz != True:
              print("waiting for function-days")
              sleep(3600)
              continue
  
          while True:
              print("do something")
  #check if during while loop there wasn't date, time change (out of timerange etc)
              if date.today().weekday():
                  dayz = True
              else:
                  dayz = False
              now = datetime.datetime.now()
              timerange = time_in_range(morning, evening, now)
              if timerange != True:
                  print("break inner loop-timerange")
                  break
              if dayz != True:
                  print("break inner loop-dayz")
                  break
              print("do something -main work")
  
  
  
  #program starts here        
  timerange = time_in_range(morning, evening, now)
  
  
  # check if today is weekday (outer loop)
  if date.today().weekday():
      dayz = True
  else:
      dayz = False
  
  # if today is not a weekday wait till weekday (outer loop) 
  #for situation where the code started not in weekday
  while dayz != True:
      print("days wait")
      sleep(3600)
      dayz = date.today().weekday()
      if dayz == True:
          break
  # If it is not processing time, wait until it becomes
  # for the situation where code started not in "timerange"
  while timerange != True:
      print("timerange wait") # ---------HERE IT STOPS---------
  # But it works fine, when I set it like one hour before 'timerange' or so...
      sleep(10)
      now = datetime.datetime.now()
      timerange = time_in_range(morning, evening, now)
      print(timerange)
      if timerange == True:
          break
  
  # Do the main function
  main()
  
  # Let know when code ends
  print("Over of Code")
  
  
  </code></pre>