While working in a rails project which involved several date and time fields, I hit some formating issues. After some research on the net and following a few forum threads related to dates and times in RoR, I come up with a partial solution to my problems in this post:
The article explains the problem for dates and I needed a solution for both, dates and times. I also wanted to simplify the solution so I dug on to the root of the problem and came up with a different solution.
I am describing in this post how I fixed the problem for dates and times when we want other than the default format in a rails application. I separated the issues into two groups Output and Input. Output is when rails gets a date or datetime object from database and converts it to string to show it on a view. Input is when rails reads the string representing a date or time from a text field and converts it to a date or time object. However both are interrelated and at some point both involve the same objects and method calls.
Output formating problem
If we have the date 20/06/2007 on a date field in your database and want to show it textually on a view it is presented in the format “2007-06-20″, similarly if you have a datetime field you get something like “Wed Jun 20 20:30:00 +0100 2007″
This may not be the format we want when displaying dates and times, so we look at ways to change the default format. The default format can be changed by setting the preferred date display format into
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS. merge!(default => '%d/%m/%Y')
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS. merge!(default => '%d/%m/%Y %H:%M')
This will make Rails to show dates on the format you have chosen. However as Rails uses the default date format (which we just changed) to store dates to the DB, if your DB does not understand the format you set then null values will be stored.
The problem in this case is (at version 1.15.3 of activerecord) located in the file
lib/active_record/connection_adapters/abstract/quoting.rb and can be fixed by dropping the [quoting_patch.rb] file into your application lib directory and add the following to your
# Apply patch for date and date times quoting ActiveRecord::ConnectionAdapters::Quoting. send(:include, QuotingPatch)
This patch corrects both, date and datetime conversion to string and quoting for the database.
Input formating problem
If we use a text field to input a date and enter “10/06/2007″ we get into the database “2007-10-06″. So the parser has interpreted the string as an American date format and set the month to be 10. If we write into the field “20/06/2007″ The parser returns null as 20 is not a good enough month and the database gets a null or fails if null are not permitted. A similar problem arises if we introduce times such as “10/06/2007 20:44″
Rails date handling when reading from a field calls
ActiveRecord::ConnectionAdapters::Column.string_to_date(string) which in turn calls
ParseDate.parsedate(string), but the parser does not have the current display format into account. The method tries to guess the date as best as it can but some formats are highly ambiguous. Analogously, when a time is being handled, a similar problem appears as rails calls
To solve these issues we need to change the behavior of the methods
so they first try to match the date with the display format we have set as mentioned above. I do this by parsing the date using the default format with the
This solution alone, however, creates a new problem: The method
string_to_time are called twice on the column class, one when the date is loaded from the database and another when it is loaded from a form field. Therefore the first call will carry a string in the form
"yyyy-mm-dd" for a date field or
"yyyy-mm-dd hh:mm:ss" in case of a datetime field which is the format the database returns. But in the second case the string will be on the default format mentioned above
"dd/mm/yyyy hh:mm" as the user is expected to input it on the field.
To avoid this problem, both patched methods fall back to the guessing procedure when the date or time fails to be matched to the default format so returning correct values in both cases.
To override the string_to_date method just drop the patch file [column_patch.rb] into your lib directory and require it from your
# Apply patch for date and date input require 'column_patch'
This have been tested with
"%d/%m/%Y %H:%M" formats, let me know the results of your experiences with other formats.