I have a problem with rails timezones.
application.rb
config.time_zone = 'Athens'
controller
@from = (Time.zone.now-1.day).to_date
@to = (Time.zone.now).to_date
Entry.where(:created_at => @from..@to)
This query supposed to list entries which were created yesterday. When i list entries, i see that some entries were created today.
For example:
It is 5:53 am now and i see that the last entry which was created yesterday was created 3 hours ago, it mustn’t be smaller than 6 hours. Any help will be appreciated.
Your database is probably using UTC timestamps (at least it should be) but
Time.zone.nowis in your local timezone. Then you callto_dateand lose the timezone information. Try this:So you call
beginning_of_dayto get back to 00:00:00 and then convert to UTC withutcto get your timestamps into UTC to match the database. You want to keep the time-of-day around all through this so that you don’t lose track of what’s going during the timezone transitions.