From 722eaed7216270d8e89f50c92abb1cfaf9bf1503 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 14 Aug 2015 15:42:28 +0000 Subject: Adds support for custom date format when importing a CSV file (#950). git-svn-id: http://svn.redmine.org/redmine/trunk@14495 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/import.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'app/models/import.rb') diff --git a/app/models/import.rb b/app/models/import.rb index b7064d932..df29d1788 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -27,6 +27,14 @@ class Import < ActiveRecord::Base validates_presence_of :filename, :user_id validates_length_of :filename, :maximum => 255 + DATE_FORMATS = [ + '%Y-%m-%d', + '%d/%m/%Y', + '%m/%d/%Y', + '%d.%m.%Y', + '%d-%m-%Y' + ] + def initialize(*args) super self.settings ||= {} @@ -201,6 +209,14 @@ class Import < ActiveRecord::Base end end + def row_date(row, key) + if s = row_value(row, key) + format = settings['date_format'] + format = DATE_FORMATS.first unless DATE_FORMATS.include?(format) + Date.strptime(s, format) rescue s + end + end + # Builds a record for the given row and returns it # To be implemented by subclasses def build_object(row) -- cgit v1.2.3