Weird Behaviors of datetime(timezone) in Rails application - ruby-on-rails

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.

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

Getting the current month in a timezone in Rails on Heroku

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.

Rails: print UTC time by default

I have a non-ActiveRecord server which is sending data to another service. That server expects to receive instructions including UTC in the format: "%Y-%m-%d %H:%M:%S.%L". I already have an time_format.rb initializer that overrides Time::DATE_FORMATS[:default] Is there a way I can configure my server so that Time.now.to_s will by default print UTC time, without me having to specify Time.now.utc every time I call it? I'm using ruby 2.0.0 and Rails 4.
I tried adding config.time_zone = 'London' to config/application.rb but that did not work.
I don't think ActiveRecord should have anything to do with it, but maybe I'm misunderstanding. Like Sergio said, set your applications timezone to UTC.
Here's my Rails app. My computer is in PST. My Rails app is set to UTC.
> Time.now
=> 2014-01-22 21:52:38 -0800
> Time.zone.name
=> "UTC"
> Time.zone.now
=> Thu, 23 Jan 2014 05:52:43 UTC +00:00
> Time.now.utc
=> 2014-01-23 05:52:45 UTC
I would strongly advise not overriding Time.now. That can only lead to the dark side.
A general purpose solution that works for any Ruby application:
module TimeClassExtensions
def now
super.utc
end
end
Time.singleton_class.prepend(TimeClassExtensions)

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.

Model's datetime field is stored differently on heroku from localhost

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.

Resources