Reputation: 75
I am experiencing strange things when handling Datetime in Rails.
In my models, I have a datetime
column using Postgres.
My application's Time Zone is set to Central America
. However, I am developing in Taiwan's Time Zone (in case it makes a difference.)
I am using Eonasdan's bootstrap-datetime picker gem fork for selecting dates in my forms.
As per the gem's documentation, I have overwritten Rails' date_select
and datetime_select
methods like this:
config/initializers/form.rb:
# Add to config/initializers/form.rb or the end of app/helpers/application_helper.rb
module ActionView
module Helpers
class FormBuilder
def date_select(method, options = {}, html_options = {})
existing_date = @object.send(method)
formatted_date = existing_date.to_date.strftime("%F") if existing_date.present?
@template.content_tag(:div, :class => "input-group date") do
text_field(method, :value => formatted_date, :class => "form-control datepicker", :"data-date-format" => "DD/MM/YYYY") +
@template.content_tag(:span, @template.content_tag(:span, "", :class => "fa fa-calendar") ,:class => "input-group-addon")
end
end
def datetime_select(method, options = {}, html_options = {})
existing_time = @object.send(method)
formatted_time = existing_time.to_time.strftime("%F %I:%M %p") if existing_time.present?
@template.content_tag(:div, :class => "input-group date") do
text_field(method, :value => formatted_time, :class => "form-control datetimepicker", :"data-date-format" => "DD/MM/YYYY hh:mm A") +
@template.content_tag(:span, @template.content_tag(:span, "", :class => "fa fa-calendar") ,:class => "input-group-addon")
end
end
end
end
end
In my view's javascript assets I use the datepicker like this, using the class:
$(function () {
$('.datetimepicker').datetimepicker({
format: 'LLL',
locale: 'es',
icons: {
time: "fa fa-clock-o",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down",
previous: 'fa fa-arrow-left',
next: 'fa fa-arrow-right'
}
//useCurrent: false,
//showTodayButton: true
});
});
As you can see, I am using a special locale and format for displaying the date in the field.
Handling date
(using date_select
) isn't causing any issue. But handling datetime
's time is driving me mad.
For example:
I use the datetime_select
(with datetime picker) to select a date: 15/02/2016 04:00 PM
.
I save the record in the database. However, when I check the record using the console, the datetime value stored is 2016-02-15 22:00:00
. This is the same date but different time (10pm). What is going on?
In my view, I render the date using the following:
I18n.localize(mydate.to_s.to_datetime, :format => '%d/%m/%Y %I:%M %p')
Which renders the date exactly how it was entered during input: 15/02/2016 04:00 PM
. What is Going On?!
Upvotes: 0
Views: 545
Reputation: 24337
ActiveRecord defaults to saving datetime columns in the database as UTC. It will convert the datetime records to/from UTC when you save/load a record. If you want to save the timestamps in the same time zone as your application's time zone, you can add config.active_record.default_timezone = :local
to application.rb
Upvotes: 2