On 7 February 2012 17:12, Erwin <yves_duf...@mac.com> wrote: > all my date ties are stored with the standard :db format and being in > western Europe, I have an UTC offset +1 > >> Time.now.at_beginning_of_day > => 2012-02-07 00:00:00 +0100 > > I'ld like to know if I am right ( or wrong) in my date time based > queries like : > > scope :today, lambda { > where("created_at >= ? AND created_at < ? ", > Time.now.at_beginning_of_day, Time.now.tomorrow.at_beginning_of_day) > } > > which generates: > SELECT `event_logs`.* FROM `event_logs` WHERE (created_at >= > '2012-02-06 23:00:00' AND < '2012-02-07 23:00:00' ) > > -------------- > OR should I use the Time.now.utc to cope with the :db format ? > > scope :today, lambda { > where("created_at >= ? AND created_at < ? ", > Time.now.utc.at_beginning_of_day, > Time.now.utc.tomorrow.at_beginning_of_day) > } > which generates: > SELECT `event_logs`.* FROM `event_logs` WHERE (created_at >= > '2012-02-07 00:00:00' AND created_at < '2012-02-08 00:00:00' ) > > my guess is the 2nd scope , but I am not sure
It depends which you want. Times in the db should always be in UTC, so in the first case it is testing against the start of the day in local time, which is 23:00 UTC. In the second case it is testing against the start of the day UTC. So the decision is yours, if you want your scope to return events from today local time then use the former, if you want today UTC then use the later. It all depends on what you mean by 'today'. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.