Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix: support DD/MM/YY date format for CSV imports (#3110)

GFRfa8769f79242
diff --git a/app/models/import.rb b/app/models/import.rb
index eb10615cc5..731a0a06d8 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -57,6 +57,14 @@ class Import < ApplicationRecord
"1,234" => { separator: "", delimiter: "," } # Zero-decimal currencies like JPY
}.freeze
+ # 2-digit years are ambiguous (Ruby's %y assumes 1969-2068), so this is kept
+ # out of Family::DATE_FORMATS (the global preference) and only offered when
+ # mapping a CSV file, where the user can see and verify a preview against
+ # their own data (maintainer decision on PR #531).
+ CSV_ONLY_DATE_FORMATS = [
+ [ "DD/MM/YY", "%d/%m/%y" ]
+ ].freeze
+
def self.reasonable_date_range
Date.new(1970, 1, 1)..Date.today.next_year(5)
end
diff --git a/app/views/import/configurations/_account_import.html.erb b/app/views/import/configurations/_account_import.html.erb
index 2b50e4cb7c..68c80d23f5 100644
--- a/app/views/import/configurations/_account_import.html.erb
+++ b/app/views/import/configurations/_account_import.html.erb
@@ -9,7 +9,7 @@
<div class="flex items-center gap-4">
<%= form.select :date_col_label, import.csv_headers, { include_blank: t(".leave_empty"), label: t(".balance_date") } %>
<%= form.select :date_format,
- Family::DATE_FORMATS,
+ Family::DATE_FORMATS + Import::CSV_ONLY_DATE_FORMATS,
{ label: t(".date_format"), prompt: t(".select_format") },
required: @import.date_col_label.present? %>
</div>
diff --git a/app/views/import/configurations/_transaction_import.html.erb b/app/views/import/configurations/_transaction_import.html.erb
index e6b9271010..406483b517 100644
--- a/app/views/import/configurations/_transaction_import.html.erb
+++ b/app/views/import/configurations/_transaction_import.html.erb
@@ -22,7 +22,7 @@
{ label: t(".date_label"), prompt: t(".select_column") },
required: true %>
<%= form.select :date_format,
- Family::DATE_FORMATS,
+ Family::DATE_FORMATS + Import::CSV_ONLY_DATE_FORMATS,
{ label: t(".date_format_label"), prompt: t(".select_format") },
required: true %>
</div>
diff --git a/test/models/import_test.rb b/test/models/import_test.rb
index 8c58201851..3022467d85 100644
--- a/test/models/import_test.rb
+++ b/test/models/import_test.rb
@@ -199,4 +199,19 @@ class ImportTest < ActiveSupport::TestCase
assert_equal "failed", stuck.reload.status
end
+
+ test "CSV-only date formats are not offered as a global family preference" do
+ # 2-digit years are ambiguous (see we-promise/sure#1530 and the rejected
+ # PR #531), so this format must stay out of Family::DATE_FORMATS and only
+ # be offered on the CSV mapping screen, where the user can preview it
+ # against their own data.
+ assert_empty Import::CSV_ONLY_DATE_FORMATS & Family::DATE_FORMATS
+ end
+
+ test "date_format accepts a CSV-only format like DD/MM/YY" do
+ import = imports(:transaction)
+ import.update!(date_format: "%d/%m/%y")
+
+ assert_equal Date.new(2024, 5, 15), Date.strptime("15/05/24", import.date_format)
+ end
end