Matt Matt - 6 months ago 26
Ruby Question

ActiveRecord mapping date to TimeWithZone instead of DateTime with .000000 suffix

Like in the title, the program map DateTime to TimeWithZone itself.

create_table :db_values do |t|
t.datetime :created_date
end


record from database:
2016-02-01T01:00:00+0000.000000

How can I remove that suffix? '.000000'
I used :

Time::DATE_FORMATS[:db]= lambda { |time| time.strftime("%Y-%m-%dT%H:%M:%S%z") }

Answer

Rails will save all times/timestamps in UTC timezone into your DB. This way it is easier to know exactly to what time it should compare it later when you give it a time in some other timezone. So, don't try to change the DB values to zoned time values. Go with the flow...

When it comes to displaying localized times, just use helper method I18n.l Time.current.

Here are a couple of good articles about timezones in Rails.

Comments