Is Time.zone.now.to_date equivalent to Date.today?

ruby, ruby-on-rails

Solution

They are not always the same. Time.zone.now.to_date will use the applications time zone, while Date.today will use the servers time zone. So if the two lie on different dates then they will be different. An example from my console:

ruby-1.9.2-p290 :036 > Time.zone = "Sydney"
 => "Sydney" 
ruby-1.9.2-p290 :037 > Time.zone.now.to_date
 => Wed, 21 Sep 2011 
ruby-1.9.2-p290 :038 > Date.today
 => Tue, 20 Sep 2011 

Problem

Is `Time.zone.now.to_date` equivalent to `Date.today`? Another way to put it: will `Time.zone.now.to_date == Date.today` always be `true`? If not, what's the best way to get a Date object corresponding to "now" in the application time zone?

Original source