I have an application which uses the following config:
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.time_zone = 'Amsterdam'
config.active_record.default_timezone = :utc
end
I'm offering the user a form that asks for a separate date (datepicker) and time field (drop down). On the before method I'm concatenating the date & time into a single datetime field. It seems though that the Time object is somehow in UTC, while the Date object doesn't care about that.
When storing this into the database, somehow the timezone gets corrected, but the time is 2 hours off. Is this due to the fact that the time is still in UTC? How can I compensate for that in a way also daylight savings is respected?
Code sample
The form:
= simple_form_for @report do |f|
.row
.col.s6
= f.association :report_category
.col.s6
= f.association :account
.row
.col.s6.l4
= f.input :day, as: :string, input_html: {class: 'datepicker current-date'}
.col.s6.l4
= f.input :time, as: :string, input_html: {class: 'timepicker current-date'}
The callback in the model
def create_timestamp
self.date = day.to_datetime + time.seconds_since_midnight.seconds
end
After saving, there's a 2 hour difference between de time I've chosen in the form.
This is how it looks like after creating a new record where time is just the same time as the record was created, to see the difference:
date: Sat, 20 May 2017 16:10:00 CEST +02:00,
created_at: Sat, 20 May 2017 14:10:33 CEST +02:00,
updated_at: Sat, 20 May 2017 14:10:33 CEST +02:00,
deleted_at: nil,
time: 2000-01-01 14:10:00 UTC,
day: Sat, 20 May 2017,
As you van see, the time is stored in UTC, while it is actually 14:10 CET +0200 where I created the record. So that could be causing the discrepancy. Which you can see in date, where the time is +2 hours from the created_at.
Thanks