Rails time broken at midnight?

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?

+6
source share
2 answers

Rails will define relative date calculations such as yesterday , tomorrow and midnight off Date.current that will try to use the configured Time.zone : https://github.com/rails/rails/blob/master/activesupport/lib/active_support/ core_ext / date / calculations.rb # L46

Since your Time.zone set to UTC, you will not get the same results as calculations based on Date.today , which will use the time of your computer if you are not actually sitting in UTC.

So, if the time difference between you and UTC is greater than the time before midnight, Date.yesterday and Date.today really return the same date!

Try setting your Rails time zone using Time.zone = 'Eastern Time (US & Canada)' or any other time zone you are in, and repeat your examples.

+6
source

rossta identified the culprit. You might be lucky with Time.now.to_date and specifying the time zone if necessary:

 > Time.now.in_time_zone("Asia/Tokyo") => Thu, 04 Oct 2012 12:54:43 JST +09:00 > Time.now.in_time_zone("Asia/Tokyo").to_date.midnight => Thu, 04 Oct 2012 00:00:00 JST +09:00 Time.now.in_time_zone("Asia/Tokyo").to_date.yesterday.midnight => Wed, 03 Oct 2012 00:00:00 JST +09:00 > Time.zone = "America/Los_Angeles" => "America/Los_Angeles" > Time.now.in_time_zone => Wed, 03 Oct 2012 20:55:35 PDT -07:00 > Time.now.to_date # using the system time => Wed, 03 Oct 2012 > Time.now.to_date.midnight => Wed, 03 Oct 2012 00:00:00 PDT -07:00 > Time.now.to_date.yesterday.midnight => Tue, 02 Oct 2012 00:00:00 PDT -07:00 
+1
source

Source: https://habr.com/ru/post/926903/


All Articles