How to set Rails config.time_zone to GMT +6? - ruby-on-rails

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'

Related

Ruby on Rails / Timezone in config/application.rb seems doesn't work

I'm trying to get a time of users action.
However, it seems return default UTC time even I changed timezone as seoul(UTC +9).
Here are what I did.
in
config/application.rb
put codes as follow.(change default time_zone to seoul)
config.time_zone = 'Seoul'
config.active_record.default_timezone = :local
then, run
rails c
Time.now
I expect time as UTC +9, but it shows UTC +0
Saved time of users action in data is also shown UTC +0.
How can I get time as UTC +9?, is there anything that I need to set?
Try
config.time_zone = 'Korea Time Zone (UTC+09:00)'

Rails Date.today shows wrong day on Heroku

I have an app which shows events in a calendar and the current date is highlighted on the calendar. I followed Railcasts calendar (revised) episode. In the helper method, you set the current date using
Date.today
this works on the local machine but when deployed to Heroku it's the day ahead of today. So for today, October 31 is being highlighted not October 30.
Does anyone know the issue here? I've tried changing a few things like
config.time_zone = 'Central Time (US & Canada)'
in my application.rb file but nothing has worked. any suggestions??
You can set it via a config variable eg;
heroku config:add TZ=Europe/London
We have to set the timezone configuration at **environment.rb** file.
#app/config/environment.rb
config.time_zone = "Pacific Time (US & Canada)"

Ruby on Rails, delayed_job serializing incorrect datetime as GMT

I have some code that seems to execute improperly when it is serialized and run with delayed_job. To get the necessaries out of the way, I am running Ubuntu 11.04, Ruby 1.8.7 with Rails 3.0.4.
I have a datetime field in one of my tables that obviously stores the date and time of a particular event. In my RoR application, I use this field through the application calling strftime on it to format it in different ways. The output of this formatting is correct on the web page.
I am also using delayed_job to put this same field into an email that is sent out (triggered via some action). When the email arrives, it appears that it has somehow been formatted as GMT. For example, if the date time was supposed to be 11-12-2011 15:30:00 (3:30 PM) in the database, the email would read 11-12-2011 22:30:00 (10:30 PM).
I looked in the database and found somethings that are interesting:
The datetime in the database is 2011-11-12 22:30:00
The app, when displayed via the web, formats the data properly as 11-12-2011, 3:30 PM
The email, formats the data properly as 11-12-2011, 10:30 PM
When I run a small ruby file that simply prints the date and time
p Time.now
I get this the correct output (local, non GMT time)
Mon Aug 15 10:15:27 -0600 2011
When I look at what is in the serialized yaml for the delayed_jobs table, I can see that the date field is formatted as
2011-11-12 22:30:00 Z
In my application.rb, I have
config.time_zone = 'Mountain Time (US & Canada)'
and in my environment.rb, I have
timezone = "Mountain Time (US & Canada)"
Can any one help me figure out what is going on here? I am relatively new to RoR, so this may be a super obvious, easy question. If you need any more debug information, please let me know and I can post it up. Similarly, if my question is unclear, I can try to clar
In my case, I solved explicitly specifying the method in_time_zone in the view
example: #my_obj.created_at.in_time_zone.
I know it is not elegant, but so far I have not found better solutions.
Try changing the date object to a string before serializing.
You may want to set the timezone in the database. If it's MySQL, see: http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

Getting local time

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

Under what conditions would I want to set a different application-level timezone in rails?

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

Resources