Skip to content

Commit

Permalink
parse date using format
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Jun 10, 2024
1 parent 7568f56 commit 1289aff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/submitters/normalize_values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/time_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 1289aff

Please sign in to comment.