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.
Related
In our rails 3.2 application, we have configured,
config.time_zone = 'London'
config.active_record.default_timezone = :local
And in postgresql also configured timezone as "Europe/London".
For the past one week, our application datetime field is not working properly with timezone.
For an example, If we create an reminder start_date at 2015-08-18 10AM. it creates `2015-08-18 10:00:00 in postgres database.
when displaying in template,
reminder start date: 2015-08-18 10AM (2015-08-18 10:00:00 +0100 )
BUT,(not always) now frequently its showing UTC time.
reminder start date: 2015-08-18 9AM (2015-08-18 09:00:00 UTC )
it could not be reproduced in development.
If we restart the unicorn server then it is not occurring for 4 hours.
anyone faced this kind of problem?
I fixed this issue, by adding around filter in ApplicationController.
around_filter :use_time_zone
private
def use_time_zone(&block)
Time.use_zone('London', &block)
end
So, whenever the default timezone changed to UTC, it will override and set as BST.
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)'
I get the current month number using the following simple code in a Controller:
#month = DateTime.now.month
In my application_controller.rb:
around_filter :use_time_zone
def use_time_zone(&block)
Time.use_zone('Pacific Time (US & Canada)', &block)
end
And it seems to work great. Though I did see some kind of Rails 4 deprecation warning somewhere, I think in my production logs. Maybe that's the cause of my issue?
What happens is when I deploy to Heroku, the #month I'm guessing is using UTC time instead of Pacific. It's 5:30pm March 31 right now and #month is 4 (April). But only in production on Heroku, locally (using SQLite3 if that matters), #month is correctly 3 (March).
How do I get the correct month for a particular timezone on Heroku?
Use DateTime.now.in_time_zone('Pacific Time (US & Canada)').month or DateTime.now.in_time_zone.month. In the latter case the time zone returned by Time.zone is used.
Unlike Time.zone.now and DateTime.current that use the Rails time zone, Time.now and DateTime.now, ruby classes, use the system's time zone by default.
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)"
Steps:
On my localhost server I select the time Jan 18, 2013 10 am - 11 am in my browser located in the Pacific time zone. I then select the exact same time on my production server on heroku.
As you can see the outputs of what is saved to the database are different. I want production to behave as localhost and take into account the timezone when storing the time as UTC. What is causing this? The heroku server is in EST, but that does not account for this behavior.
Controller:
def create
puts params[:event][:starts_at]
#event = Event.create!(params[:event])
puts #event.starts_at
end
Output Localhost:
Fri Jan 18 2013 10:00:00 GMT-0800 (PST)
2013-01-18 18:00:00 UTC
Output Heroku (production):
Fri Jan 18 2013 10:00:00 GMT-0800 (PST)
2013-01-18 10:00:00 UTC
Schema:
t.datetime "starts_at"
Env:
Ruby on Rails 3.2.11
Ruby 1.9.3
Postgres
Background on how PostgreSQL deals with date/time
I think your issue is covered in this answer from another SO question:
Ignoring timezones altogether in Rails and PostgreSQL
Specifically this answer:
https://stackoverflow.com/a/9576170/33204
Use this query for time in UTC format:
SELECT * FROM tbl WHERE time_col > (now() AT TIME ZONE 'UTC')::time
Use this query for time in local timezone:
SELECT * FROM tbl WHERE time_col > now()::time
Rails & ActiveRecords & date/time
This rails/rails github issue also discusses it:
ActiveRecord converts time to default zone on write; assumes UTC on read
There is a comment by pixeltrix which says the following:
#deathbob sorry for the late reply - the accepted values for
config.active_record.default_timezone are either :utc or :local. If
you set it to something other than :utc it will assume :local so what
you're seeing is the expected behavior.
To fix the time in ActiveRecords so that it is always dealt with as UTC you can set the following variable:
config.active_record.default_timezone = :utc
The solution by #slm of config.active_record.default_timezone = :utc did not work for me. Rails was still writing with local and reading with UTC on Heroku.
So I then tried:
config.time_zone = 'Sydney'
config.active_record.default_timezone = :local
Which was still working fine in development but caused the datetime values to now be read in a UTC format from postgres on Heroku.
My temporary workaround solution was to set the default timezone on Heroku with:
heroku config:add TZ="Australia/Sydney"
This is only a temporary solution as I would like my app to use UTC. I have posted to the rails/github issue mentioned by #slm for clarification. I will update my answer when I find a more concrete solution.