How would I add a new time zone to the list of time zones?
If I would have to simply add a time zone to my system, why doesn't it appear on the list:
rake time:zones:all
As mentioned by ranja below, I tried to use ActiveSupport with time zones.
So I open up the console
user#host:/var/www/project$ irb
irb(main):001:0> require 'active_support'
=> true
irb(main):002:0>
But what should I use next? And will the new time zone be lost?
How would I implement a permanent new ruby time zone which would be available on all my aps? Thanks so far!
In your rails console type below command , you can get list of name
ActiveSupport::TimeZone.all.map(&:name)
Please try this in application.rb for rails 3.X and enviroment.rb in rails 2.x
config.time_zone = 'Eastern Time (US & Canada)'
after that do
rake time:zones:all
Thanks
Related
When I pop open the rails application.config to set the timezone, I come across an example like this:
config.time_zone = 'Central Time (US & Canada)'
But I would like to try to set my time to something like GMT+6 unfortunately if I try to use that value I get a Invalid Time Zone error. So what name do I have to use to get GMT+6? I tried looking up the name on TimeandDate.com but that gave me the same error when I tried to put Kyrgyzstan Time
Where exactly is the list of time zones rails uses? Or do I have to use some other format like +0600 (which doesn't work by the way)
Thanks,
If you run
rake time:zones:all OFFSET=+6
you will get a list of cities
* UTC +06:00 *
Almaty
Astana
Dhaka
Ekaterinburg
of which the names can be directly used, like
config.time_zone = 'Astana'
I have a Rails 2.3 app that is full of data timestamped with the local timezone (EST) as was the convention with Rails 2.3. The problem is that now I am upgrading to Rails 3.2 and I want to avoid going and updating all those timestamps to UTC which is the Rails 3.2 convention. What is the best approach? I can't seem to figure out a way to tell Rails that the data in the database is in the EST timezone so it can appropriately accomodate timezones calculations. Surely others have run into this? Thanks!
Got it working! This was the configuration that needed to be added:
application.rb:
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local
Now it assumes the data in the database is in the local timezone specified as 'Eastern Time (US & Canada)'.
I've got this time stored as a string:
2010-07-25 04:16:25
This is the GMT time for some action I took.
Since I live in the Jerusalem time zone, I would like to show it at Jerusalem time, i.e. 07:16 in the morning, not the GMT time of 04:16:25 which is 3 hours before.
How do I properly convert it programmatically with Ruby on Rails? I seem to get lost with the multitude of timezone functions and considerations I need to take when serving users from different locations.
I tried:
Time.parse("2010-07-25 04:16:25")
and it gave me:
"Sun Jul 25 04:16:25 +0300 2010".
I suppose the "+0300" is the difference to where I'm at?
Some light on this, or even a link to a good article that doesn't assume you know much, would help.
You can define your timezone in environment.rb file (if you are using Rails 2.3.*) or application.rb (if you're using Rails 3).
Just look for section about time zones and everything is explained in comment. It will say something like this (this is from Rails 3):
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
Just uncomment that last line and you should be fine.
To configure it easily related to your system local zone you can add this in your application.rb
config.time_zone = Time.now.zone
and you can use something like this to get the localtime
Post.created_at.localtime
In almost every environment.rb, there's a line for config.time_zone = 'UTC'.
What exactly does this line do, and under what circumstances would I want to change it (to, e.g., config.time_zone = 'EST')?
Setting config.time_zone changes the default time zone for your Rails application. This is the time zone all times will be displayed in to your users. It is also the time zone it assumes when setting attributes.
However, Rails will always store the times in UTC in the database. The translation happens behind the scenes so (most of the time) you don't have to worry about it.
It's common to change this time zone to one that most of your users will be in. You can run this rake task to see all time zones you can choose from.
rake time:zones:all
It is also very easy to change the current time zone on a per-request basis allowing each user to configure which time zone they are in. here's a before filter example you might add to the application controller.
before_filter :set_user_time_zone
private
def set_user_time_zone
Time.zone = current_user.time_zone if logged_in?
end
See this Railscasts episode for more information.
Just to add one point to Ryan's excellent answer. If you wanted to set it to Eastern Time, it wouldn't be
config.time_zone = 'EST'
it would be
config.time_zone = 'Eastern Time (US & Canada)'
Use one of the following time zones to get the list of available options:
rake time:zones:all
rake time:zones:local
rake time:zones:us
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?
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
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.