From 1289aff493d4de87c7dd3ac9d6a62c0b942d312c Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Mon, 10 Jun 2024 12:08:56 +0300 Subject: [PATCH] parse date using format --- lib/submitters/normalize_values.rb | 4 +++- lib/time_utils.rb | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/submitters/normalize_values.rb b/lib/submitters/normalize_values.rb index 6762c4063..7c9863b5f 100644 --- a/lib/submitters/normalize_values.rb +++ b/lib/submitters/normalize_values.rb @@ -56,7 +56,9 @@ def normalize_value(field, value) (value.to_f % 1).zero? ? value.to_i : value.to_f elsif field['type'] == 'date' && value.present? && value != '{{date}}' if value.is_a?(Integer) - Time.zone.at(value.to_s.first(10).to_i).to_date + Time.zone.at(value.to_s.first(10).to_i).to_date.to_s + elsif value.gsub(/\w/, '0') == field.dig('preferences', 'format').to_s.gsub(/\w/, '0') + TimeUtils.parse_date_string(value, field.dig('preferences', 'format')).to_s else Date.parse(value).to_s end diff --git a/lib/time_utils.rb b/lib/time_utils.rb index a11622793..f2a0832a4 100644 --- a/lib/time_utils.rb +++ b/lib/time_utils.rb @@ -32,6 +32,14 @@ def timezone_abbr(timezone, time = Time.current) tz_info.abbreviation(time) end + def parse_date_string(string, pattern) + pattern = pattern.sub(/Y+/, YEAR_FORMATS) + .sub(/M+/, MONTH_FORMATS) + .sub(/D+/, DAY_FORMATS) + + Date.strptime(string, pattern) + end + def format_date_string(string, format, locale) date = Date.parse(string.to_s)