Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transform time into local time in Ruby on Rails

Right now I have:

Time.strftime("%I:%M%p") which gives me the hr:min AM/PM format which I need. However it's coming back in UTC and I need it local time zone.

How do I change it to local time zone and keep that same format for time?

like image 374
Ryan Avatar asked Jul 22 '09 16:07

Ryan


2 Answers

Also, remember that as of Rails 2.1, Timezones are supported.

in your config/environment.rb file:

config.time_zone = 'UTC'

You can find other values by running

 rake time:zones:local

And Ryan Bates has a great railscast on it: http://railscasts.com/episodes/106-time-zones-in-rails-2-1

like image 67
Mark S. Avatar answered Sep 29 '22 20:09

Mark S.


Generally that should work. You may have your time-zone set incorrectly, though:

Time.now.strftime("%I:%M%p") # => "12:48PM"
Time.now.utc.strftime("%I:%M%p") # => "04:48PM"
Time.now.utc.getlocal.strftime("%I:%M%p") # => "12:48PM"
Time.now.zone # => "EDT"

This can be affected by the TZ environment variable, your OS locale settings, or your Rails configuration in environment.rb.

like image 26
tadman Avatar answered Sep 29 '22 22:09

tadman