Thursday, 15 August 2013

ruby on rails 3 - How to set timezone before saving -



ruby on rails 3 - How to set timezone before saving -

i building rails 3.2 web app , when supply date (as string) object saving date gets saved 1 day before.

so if supply 2014-06-18 date gets saved 2014-06-17. extremely annoying. updated_at saved correctly.

this code:

report = timereport.new report.status = "stop" report.hours = 0 report.created_at = params[:created_at] report.save

how can prepare this?

update

params[:created_at] => 2014-06-18 time.zone => #<activesupport::timezone:0x007fd94fbcb170 @name="utc", @utc_offset=nil, @tzinfo=#<tzinfo::timezoneproxy: etc/utc>, @current_period=nil> '2014-06-18'.to_datetime => wed, 18 jun 2014 00:00:00 +0000

i'd recommend not changing this. rails saves dates utc in database can localized later.

what want instead have activerecord translate dates time zone when reading them database.

you can globally in config/application.rb config.time_zone setting.

# set time.zone default specified zone , create active record auto-convert zone. # run "rake -d time" list of tasks finding time zone names. default utc. # config.time_zone = 'central time (us & canada)'

you can according user's time zone, sounds want take care of globally.

ruby-on-rails-3

No comments:

Post a Comment