Look at the date:
1.9.2p320 :008 > Date.today => Wed, 03 Oct 2012 1.9.2p320 :009 > Time.now => 2012-10-03 22:32:55 -0400
Now, considering that when is midnight?
1.9.2p320 :005 > Date.today.midnight => Wed, 03 Oct 2012 00:00:00 UTC +00:00
Has the meaning. But what about yesterday?
1.9.2p320 :006 > Date.yesterday.midnight => Wed, 03 Oct 2012 00:00:00 UTC +00:00
Uh, this is not entirely clear. Today is midnight today the same as midnight yesterday? You cannot be serious!
1.9.2p320 :026 > Date.today.midnight == Date.yesterday.midnight => true 1.9.2p320 :033 > 1.day.ago.midnight == Date.yesterday.midnight => true 1.9.2p320 :034 > 1.day.ago.midnight == Date.today.midnight => true
Oh, you are serious. How about tomorrow?
1.9.2p320 :007 > Date.tomorrow.midnight => Fri, 05 Oct 2012 00:00:00 UTC +00:00
Wait, if today is midnight today 00:00 on the 3rd, and midnight yesterday is 00:00 on the 3rd, but midnight tomorrow is 00:00 on the 5th, where is 00:00 on the 4th?
There he is:
1.9.2p320 :010 > 0.days.ago => Thu, 04 Oct 2012 02:34:58 UTC +00:00 1.9.2p320 :011 > 0.days.ago.midnight => Thu, 04 Oct 2012 00:00:00 UTC +00:00
but not today? Obviously not.
Is it me, or is it not internally consistent at all? It seems to me that Date.today should be the same as 0.days.ago.
I understand that days.ago actually uses a Time object and that this is a time zone problem:
1.9.2p320 :030 > Date.today => Wed, 03 Oct 2012 1.9.2p320 :021 > Time.now => 2012-10-03 22:40:09 -0400 1.9.2p320 :023 > 0.days.ago => Thu, 04 Oct 2012 02:40:22 UTC +00:00 1.9.2p320 :022 > Time.zone.now => Thu, 04 Oct 2012 02:40:14 UTC +00:00
But it seems that, given that these are convenient functions, it seems to mean throwing the time zone assumption into one convenient function, rather than throwing it into another convenience function, both of which, for both accounts, mean the same thing.
Even leaving this aside, this does not seem to explain the fact that Date.today.midnight == Date.yesterday.midnight , which is - just plain - barks crazy.
Since I know that I cannot be the first to be bitten by this, I ask, what am I missing?