Rails Date.today shows wrong day on Heroku - ruby-on-rails

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)"

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)'

How to set Rails config.time_zone to GMT +6?

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'

Ruby on Rails time zone issue

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.

Upgrading from Rails 2.3 to Rails 3 - Timezone issue

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)'.

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

Resources