0

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:

  1. I use the datetime_select (with datetime picker) to select a date: 15/02/2016 04:00 PM.

  2. 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?

  3. 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?!

Andres
  • 75
  • 8
  • Jaime, how did you solve this problem? I'm facing this at the moment and it drives me crazy. When it gets to the formatted_time it's already screwed up thanks to the existing_time format, so I can't do anything useful in the formatted_time line. I was thinking about using moment.js/american_date gem, but I would rather avoid that. – Sean Magyar Jan 06 '16 at 23:30
  • Hi @SzilardMagyar . In the end I realized that I actually do not need to store the datetimes in non UTC format. Since Rails will make the appropriate conversion to the right Time format when retrieving the values. My problem was that in DatePicker plugin configuration had some errors which rendered an incorrect `datetime` value. – Andres Jan 11 '16 at 01:59

1 Answers1

2

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

infused
  • 24,000
  • 13
  • 68
  • 78
  • I tried adding that code right under `config.time_zone` but seems like it still doesn't work. The Time shown from the rails console is still different. You mentioned "timestamps", but I am using `datetime`. Not sure if that makes a difference.... – Andres Dec 23 '15 at 07:34