Reputation: 3333
I have
Rails 3.0.9
Activerecord-sqlserver-adapter 3.0.15
TinyTds
MSSQL 2005
I have a problem with using Time.now.
That is what I do in console:
Could someone explain this behavior?
irb(main):026:0> row = Eclaim.where(:id => 1).first
=> #<Eclaim id: 1, id_user: 1, property: "inv", app_number: nil, patent_number:
nil, native_number: nil, title: nil, applicants: nil, receive_date: nil, change_
date: "2012-05-08 10:20:44">
irb(main):027:0> row[:change_date] = Time.now
=> 2012-05-08 13:37:13 +0300
irb(main):028:0> row.save
=> true
irb(main):029:0> row = Eclaim.where(:id => 1).first
=> #<Eclaim id: 1, id_user: 1, property: "inv", app_number: nil, patent_number:
nil, native_number: nil, title: nil, applicants: nil, receive_date: nil, change_
date: "2012-05-08 10:37:13">
irb(main):047:0> Time.zone
=> (GMT+00:00) UTC
Why am I getting the date 2012-05-08 10:37:13 in database instead of being 2012-05-08 13:37:13 +0300?
Upvotes: 1
Views: 1639
Reputation: 3333
I have found the solution:
In application.rb
you should write such settings:
config.time_zone = 'Riga'
config.active_record.default_timezone = :local
Upvotes: 3
Reputation: 3052
i am not an expert in dates, but what happens if you change to utc like this
> row[:change_date] = Time.now.localtime
> row.save
I think that will give the correct result. As to why this happens(my 2 cents):
One should always store the dates in some standard(people seem to differ as to what format that is). However just be aware that when storing as utc time, you will need to display the time as local time(easily done). In your example above rails is automatically converting to utc and storing it. Hope it helps
EDIT
Rails i think still defaults to utc time(not sure how to change this). However from utc time in database you could call localtime.What happens if you call this
Eclaim.first.change_date.localtime
This is the only way i can think off to get the localtime from utc stored in database.
Upvotes: 0
Reputation: 10079
ActiveRecord stores dates in UTC (previously known as GMT). It converts the dates back to the local time zone when you format the date. After Eclaim.where(:id => 1).first, do a row.change_date.
irb(main):029:0> row = Eclaim.where(:id => 1).first
irb(main):029:0> row.change_date
Upvotes: 0