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)'.
Related
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)"
I am working with a legacy Rails 2.3.11 incident reporting application and I'm having some issues with the time zone. For some reason when a user selects a date/time from a datepicker it seems to store the value fine (I check the database and the value is correct), but when the value is retrieved from the database it is 5 hours in the future. Does anyone know why this might be?
I have checked the time on both the app server and the database server and they seem to be correct: Mon Jun 10 07:52:18 CDT 2013
Here are some values that might be important:
environment.rb
config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local
config.active_record.time_zone_aware_attributes = false
incident.rb (model class)
def self.format_time(time)
if (time)
time.strftime("%Y-%m-%d %I:%M:%S %p")
else
return "Not Available"
end
end
def self.time_format
"%Y-%m-%d %I:%M:%S %p"
end
Edit: Also note that I cannot replicate this issue on my local development machine. It seems to only be an issue when the app is pushed to the server.
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
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
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.