Note that there are some explanatory texts on larger screens.

plurals
  1. POIs Rails time broken for midnight?
    primarykey
    data
    text
    <p>Let's look at the date:</p> <pre><code>1.9.2p320 :008 &gt; Date.today =&gt; Wed, 03 Oct 2012 1.9.2p320 :009 &gt; Time.now =&gt; 2012-10-03 22:32:55 -0400 </code></pre> <p>Now, given that when is midnight?</p> <pre><code>1.9.2p320 :005 &gt; Date.today.midnight =&gt; Wed, 03 Oct 2012 00:00:00 UTC +00:00 </code></pre> <p>Makes sense. But what about yesterday?</p> <pre><code>1.9.2p320 :006 &gt; Date.yesterday.midnight =&gt; Wed, 03 Oct 2012 00:00:00 UTC +00:00 </code></pre> <p>Uh, that doesn't quite make sense. Midnight today is the same as midnight yesterday? You can't be serious!</p> <pre><code>1.9.2p320 :026 &gt; Date.today.midnight == Date.yesterday.midnight =&gt; true 1.9.2p320 :033 &gt; 1.day.ago.midnight == Date.yesterday.midnight =&gt; true 1.9.2p320 :034 &gt; 1.day.ago.midnight == Date.today.midnight =&gt; true </code></pre> <p>Oh, you are serious. What about tomorrow?</p> <pre><code>1.9.2p320 :007 &gt; Date.tomorrow.midnight =&gt; Fri, 05 Oct 2012 00:00:00 UTC +00:00 </code></pre> <p>Wait, if midnight today is 00:00 on the 3rd, and midnight yesterday is 00:00 on the 3th, but midnight tomorrow is 00:00 on the 5th, where's 00:00 on the 4th?</p> <p>Here it is:</p> <pre><code>1.9.2p320 :010 &gt; 0.days.ago =&gt; Thu, 04 Oct 2012 02:34:58 UTC +00:00 1.9.2p320 :011 &gt; 0.days.ago.midnight =&gt; Thu, 04 Oct 2012 00:00:00 UTC +00:00 </code></pre> <p>but isn't zero days ago <em>today?</em> Apparently not. </p> <p>Is it me, or is this not at all internally consistent? It seem to me that Date.today should be the same as 0.days.ago.</p> <p>I understand that <code>days.ago</code> is actually using the Time object, and that this is a time zone issue:</p> <pre><code>1.9.2p320 :030 &gt; Date.today =&gt; Wed, 03 Oct 2012 1.9.2p320 :021 &gt; Time.now =&gt; 2012-10-03 22:40:09 -0400 1.9.2p320 :023 &gt; 0.days.ago =&gt; Thu, 04 Oct 2012 02:40:22 UTC +00:00 1.9.2p320 :022 &gt; Time.zone.now =&gt; Thu, 04 Oct 2012 02:40:14 UTC +00:00 </code></pre> <p>But it seems as though, given that these are convenience functions, it's kind of mean to throw a timezone assumption into <em>one</em> convenience function and not throw it into <em>another</em> convenience function, both of which, by all accounts, mean the same thing.</p> <p>Even setting that aside, it doesn't seem to explain the fact that <code>Date.today.midnight == Date.yesterday.midnight</code>, which is– quite simply– barking mad.</p> <p>Since I know that I can't be the first to have been bitten by this, I ask <em>what am I missing?</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload