Reputation: 79
In my rails application I'm getting data and saving it by its date for all the records that has the today's date for a later call.
The problem is the data comes in this format "8:05 PM ET"
When I parse it to get it in the date format I get this:
"Mon, 24 Aug 2015 20:05:00 EDT -04:00"
,which is the right date. The thing is when I save it in the database is saved as
"2015-08-25 00:05:00 UTC"
When I request to the db give me all the data from today it doesn't retrieve this record because the date says that record is 25 "tomorrow"** and not **24 "today".
How can I save that record in my database as today "24" and not as tomorrow "25"?
My configuration:
application.rb
class Application < Rails::Application
Dir["#{Rails.root}/lib/**/**/*.rb"].each { |f| require f }
config.time_zone = 'Eastern Time (US & Canada)'.freeze
config.active_record.default_timezone = :local
config.active_record.raise_in_transactional_callbacks = true
end
and before save it:
string = '7:05 PM ET'
datetime = DateTime.now
Time.use_zone('Eastern Time (US & Canada)') do
datetime = Time.zone.parse(string)
end
baz.create(time: datetime, foo: 'bar')
when i try to acces it
baz.where(time:Date.today.to_time.beginning_of_day..Date.today.to_time.end_of_day)
Upvotes: 0
Views: 3574
Reputation: 34318
In your database, dates are saved in UTC
.
But, if you want to retrieve the dates in your own timezone format, you can specify your timezone in the config/application.rb
filelike this:
config.time_zone = 'Eastern Time (US & Canada)'.freeze
Then, you can do:
time_now = DateTime.now
time_now_in_my_zone = time_now.in_time_zone(Rails.application.config.time_zone).to_datetime
See ActiveSupport::TimeWithZone
To keep things consistent and saving time in local timezone to database, this has to be set in application.rb
config.active_record.default_timezone = :local
Default timezone is :utc
.
See Configuring Active Record for more options.
So, you can use config.active_record.default_timezone = :local
setting to save the date in your local format and retrieve it in your format too, and keep things consistent. I think, this answers your question. Let me know if not!
Change:
Time.use_zone('Eastern Time (US & Canada)') do
datetime = Time.zone.parse(string)
end
to:
datetime = Time.now.in_time_zone(Rails.application.config.time_zone).to_datetime
baz.create(time: datetime, foo: 'bar')
Upvotes: 2
Reputation: 1264
A shorter way to achieve that:
config.time_zone
in your application.rb
file.Time.zone.now
to save your records.Example:
Setting 'Buenos Aires' timezone in a Rails application.rb file.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
config.time_zone = 'Buenos Aires'
Then you can use this to store the properly Time:
Time.zone.now # => Mon, 24 Aug 2015 13:06:21 ART -03:00
If you want to know what time_zone do you need, you can run this: rake time:zones:all
Hope this help :)
Upvotes: 0
Reputation: 6250
You might want to consider studying a little more about time zones.
in your case Mon, 24 Aug 2015 20:05:00 EDT -04:00
is the same date as 2015-08-25 00:05:00 UTC
UTC means the timezone +00:00
Upvotes: 0