title: How to wait for a certain time, like midnight, before proceeding
body: <p>We are trying to get a test to wait until exactly midnight before proceeding. </p>
  
  <p>Currently it checks every second for midnight, which fills the console with a bunch of "waiting for midnight" messages. </p>
  
  <p>Is there a way to have it just check the time then wait until midnight?</p>
  
  <pre><code>def wait_if_almost_midnight max_minutes_needed, utc: false
      seconds_waited = 0
      now = utc ? Time.now.utc : DateTime.now
      now_hour = now.strftime('%k').to_i
      now_minute = now.strftime('%M').to_i
      while now_hour == 23 &amp;&amp; max_minutes_needed &gt; (59 - now_minute)
        sleep 1
        seconds_waited += 1
         p "Waiting for midnight, seconds: #{seconds_waited}"
         now_hour = now.strftime('%k').to_i
        now_minute = now.strftime('%M').to_i
      end
    end
  </code></pre>