I have some strange behavior in my rails3 app.
When I pick a date with jquery UI datepicker. I see this in my log
Parameters: {"commit"=>"Save", "log"=> {"log_date"=>"04/20/2011"} ....blah...b}
AREL (1.0ms) INSERT INTO "logs" ("log_date") VALUES ('2011-04-19 22:00:00.000000')
I’ve left out the irrelevant information.
As you see Rails does not translate the date inputted correct. It changes (in this case) 20.apr to the 19.apr.
When I later call
<%= log.log_date.strftime('%d.%m.%y') %>
I get the corect date, but when I do this query
@log_times = Log.group(:log_date)
The logs are all one day earlier than they sould be.
In the console the log in this example looks like this
irb(main):017:0> Log.last
=> [#<Log id: 246, log_date: "2011-04-19 22:00:00">]
So, its save one date to early, but when I show it in the view it’s correct.
Why?
Could I use a getter and setter to fix this?
Rails is built to handle timezones by default. Times stored in the database are stored in UTC and Rails is supposed to handle translating that into the configured local time automatically.
Do you have
config.time_zoneset in your application.rb file? Also, I’d recommend usingDateTimerather thanTimewhen possible.